SAS Mfile Capture open code between macro calls - sas

We can capture macro(%test) executed code with the below snippet
filename mprint 'output-file-name.sas';
options MPRINT MFILE;
%test;
options NOMPRINT NOMFILE;
Is there a way to capture the SAS executed code with MFILE option when we have datasteps/proc calls between the macro calls.
Example:
%test;
data ...
set ...
...
run;
%test2;
proc sort data=...
run;
%test3;
Using some sort of wrapper code to put the above code in a macro and then use MFILE ?
Is there any automated standard approach ? I have to implement the solution at enterprise level.

If you have control over the code itself, then you can do it easily using either:
Alternate logging methods for the entire SAS log (proc printto, altlog startup option)
Wrap the entire code in a macro, then use options mfile
That requires you being able to edit the code, though. Given you've thought of the second, and are asking how, I assume you probably don't have control directly.
If you do not have control over the code but have control over the SAS system (for example, you're a SAS Administrator), then you have a few options, all related to capturing the entire log.
You can, again, use altlog option
Better, though, is the more advanced logging options using the SAS logging facility. This can be used to put pretty detailed logs from the workspace server, or even from base SAS itself if you're in an environment where users run base SAS interactively.
If you are using a SAS Workspace Server (Enterprise Guide, SAS Studio), then the latter option is very easy: you just need to change the Workspace Server's logging level to Debug, and modify the name of the file it outputs to such that you can make use of it (add to the name things like the username).
Do note that options mfile can be overridden by the user, unless you add it to the restricted options table.

Related

SAS Enterprise Guide export as step in process path selection

As part of the SAS Enterprise Guide export as a step in process the outhput file location is very tedious to select. Is there any faster way to do it than clicking through all the folders on my harddrive for every export.
The path is shown but I am not able to edit the path or paste a path here:
And when i press browse I am still not able to paste a path:
Am I doing something wrong, missing a setting or is there some kind of workaround.
Temporary question to Joe:
Like this?
Here is the most basic example of a macro, that can export your tables as excel files:
%macro ExportExcel(path,file_name,tab_name);
proc export data=&tab_name
outfile="&path.&file_name..xlsx"
dbms=xlsx
replace;
run;
%mend;
You can just paste your path, or better yet, make it a macro variable using %let statement.
But depending on your needs you can make this macro way more complicated. You can put more than one table into a single .xlsx file on different sheets, using a sheet statement. You can export every table from a whole library. It really depends on what you want.
You should be able to paste a full path into the filename box, just as with any other "save" dialog. This certainly works with the current version (Enterprise Guide 8.3), but should work with older versions as well.

Copying files from one directory to another in SAS EG is not working

I am trying to copy files from one directory to another in SAS EG, but it was not working.
The basic idea in here is to convert .xlsm file to .xlsx file.
%sysexec( copy "&path.\excel1.xlsm"
"&path.\excel1.xlsx" ) ;
I didn't get any error in SAS EG, but nothing happen (no file copied), does anyone know the reason?
Make sure that your SAS session has the XMCD option turned on.
XCMD Enables the X command in SAS.
You can use PROC OPTIONS or GETOPTION() function to see the current setting. This option must be set when the session starts so you might need to modify the server you are connecting to with Enterprise Guide to one with the option turned on (or modify the configuration of the server).
Also make sure that the command and path you are using will work on the server where your SAS code is running. You can look at some of the automatic macro variables like SYSSCP, SYSHOSTNAME and SYSHOSTINFO to see the operating system where SAS is running.
22 %put &=sysscp &=syshostname &=syshostinfolong ;
SYSSCP=WIN SYSHOSTNAME=XXXXXX SYSHOSTINFOLONG=X64_10PRO WIN 10.0.18362 Workstation
If you still have issues you can also use another method to run your command where you will be able to more easily see the messages that the operating system might generate. For example using the PIPE filename engine.
data _null_;
infile %sysfunc(quote(copy "&path.\excel1.xlsm" "&path.\excel1.xlsx" 2>&1)) pipe;
input;
put _infile_;
run;

SAS to Export File with Conditional Formatting

Is it possible using SAS to export an Microsoft Excel formatted file that uses Excel's built in conditional formatting? I know I can use Proc Report computational sets to change formats, and I can use the ExcelXP tagset to control colors, but I need to use Excel's built in Conditional formatting, not just setting the format in SAS.
If you use a pre-formatted excel sheet you can export to it and it will maintain the conditional formatting. Proc export or the macro here is useful http://sascommunity.org/mwiki/images/f/f4/1793-2014.sas
Or if you need some sort of dynamic control could write a VB script that then gets called via DDE but also way too complicated IMO.
Short of Reeza's suggestion (ie. actually using excel to do the conditional formatting) there is no supported way to do this.
We had to do this for our web-based reporting, and we ended up writing our own conditional formatter (in jQuery). It applies excel-like conditional formatting because despite our best efforts we couldn't figure out exactly how the excel conditional formatting algorithm works (it has some quirks). I don't think anyone would ever notice the difference though.
I've entered this as a suggestion in the SAS software ballot. I'll edit this post and share the link if the idea gets 'approved'.
EDIT : You can vote here . Voting will require a login to the SAS website.

Password protection of SAS Code

Is there a way to password protect SAS CODE within SAS? Or, if not, what is the easiest / quickest way to apply some kind of password protection?
I am referring to scripts run from local Windows PCs (ie not batch). The scripts are stored on a standard network drive...
(Ideally the solution would not involve a Microsoft product such as Word or Excel!!)
You could try using SCL code copied with the NOSOURCE and NOEDIT options.
This approach of course relies on you having SAS/AF installed.
Like this:
Create a new local SCL Entry. Your INIT section asks for or checks a password. If the password is correct, control is handed to another label with the LINK command, if not, do something else.
Put your SAS code in the label you linked to in step 1, if it's BASE SAS, put it in a submit block, in which case you'll also want to set the NOSOURCE and NOSOURCE2 options to prevent your program to show up in the SAS log.
Compile, save and close your SCL entry.
Copy your SCL to its final destination using PROC COPY with the NOSOURCE and NOEDIT options.
Users can now start a SAS session that calls the SCL. If the correct password is provided, the SCL code runs the embedded SAS code. Users can't snoop the password, nor the program code essentials from the program due to the NOSOURCE option.
Note that this is not state of the art, high security password protection!
It's a quick and dirty way to keep the "wrong" people from running a specific piece of hidden SAS code.
I think the answer to your question is "no".
SAS doesn't have a good solution for this. Windows doesn't have a good solution for this.
3rd party software for password protection may be your only option.

In SAS, what are good techniques/options for catching syntax errors?

In the enhanced editor, the coloring might give you a hint. However, on the mainframe I don't believe there is anything, in the editor, that will help you.
I use
OPTIONS OBS=0 noreplace;
The obs=0 option specifies that 0 observarions are read in from the input
dataset and NOREPLACE tells SAS not to overwite an existing SAS dataset with one of the
same name. If you are creating a new datastet, it will be created with all the attributes,
but with 0 observations. (Be sure to reset the options, if needed, to Options Obs=max replace ; when no more syntax errors are found).
I'd be interested in any other techniques.
Thanks
Explanation about options came from here.
I use the cancel option on the run statement. It will check the syntax of the data step then terminate it without actually executing it. It's the data step analog to the noexec option in proc sql.
data something;
<stuff here>
run cancel;
Lots more details in this SUGI pdf
I write all of my code on my PC with SAS on my PC and the enhanced, color coded editor. I then use SAS/CONNECT to process it on the mainframe. If the datasets are on DASD, I use SAS/CONNECT and Enterprise Guide to directly run the code onthe mainframe (no JCL!) If there is a data tape involved and therefore must be a batch run, I use SAS/CONNECT and the SAS ftp engine to send the code to the mainframe batch queue. I use the SAS email engine to email me back my output and my log. I put and ODS sandwich aound my code to have the mainframe generate a WORD document for output. I use a PROC download to download the output to my server so I can open it in WORD.
This advice is language agnostic.
I would argue that a preferable technique for catching syntax (and logic) errors is to perform a close read (or inspection) of your own code (which should catch the majority of syntax errors), followed by unit tests on small datasets (which will catch any remaining syntax errors, as well as many logic errors if your tests are well-designed).
I agree there's some worth to syntax checking in isolation, but to read and understand your code thoroughly enough before the first compile so that you know it will compile is a good ideal to strive for. Steve McConnell touches on this idea in Code Complete (see page 827 of the 2nd Edition).
P.S. You mentioned syntax highlighting in your original post; there are other editors (such as VIM) that will perform syntax highlighting on SAS files.