Donald Miller SAS FAQ
Donald Miller SAS FAQ
Commenting out a large section of code
You have a large block of code you would like to comment out, but it has embedded * and /* */ comments in it. Try this:
%macro junk;And simply never call the macro "%junk".
{your code here}
%mend;
Running SAS from command prompt, Running multiple SAS jobs at the same time
- It is sometimes helpful in Windows to be able to run SAS easily from a
command prompt. Create a "sasjob.bat" file with this one line in it:
call "C:\Program Files\SAS Institute\SAS\V8\sas.exe" "%1"
Make sure this "sasjob.bat" file is in your path (like C:\WINDOWS or C:\WINNT).
From a DOS prompt, you can do this to run 'programname.sas':sasjob programname.sas
- If you find yourself running several programs in a row a lot of times, it is
frequently easier to write a SAS program that runs other SAS programs. Let's say
you have sasprog1.sas, sasprog2.sas, sasprog3.sas, and sasprog4.sas, and you
have a tendency to run them together. In Windows, you could do this in a SAS
program:
x "sasjob C:\projects\test\sasprog1.sas";
Or you could do it as a loop:
x "sasjob C:\projects\test\sasprog2.sas";
x "sasjob C:\projects\test\sasprog3.sas";
x "sasjob C:\projects\test\sasprog4.sas";%macro loopprog;
%do i=1 %to 4;
x "sasjob C:\projects\test\sasprog&i..sas";
%end;
%mend;
%loopprog; - You can do a similar thing in Unix:
%macro loopprog;
%do i=1 %to 4;
x "sas sasprog&i..sas &";
/* get rid of the ampersand if you require them to be sequential */
%end;
%mend;
%loopprog;
OPTIONS: Remove formatting errors, Mirror macro code to a file
- If you have a dataset with formats, but the formats could not be found, your
dataset will not open correctly. Use this option:
options nofmterr;
This will let you open the dataset. SAS will remove formats that it cannot find.
- If you're having trouble following/debugging some macro code, and "options
mprint" isn't helping, try this:
options mfile mprint;
This will write all of the lines generated by the macro language to a text file you can look at later.
filename mprint "C:\projects\test\mfiletest.txt";
CALL LABEL statement
This statement is very helpful in creating codebooks and the like:
call label(var1,var2);This puts the label of var1 into var2.
Reading data directly off web with filename statement (URL engine)
If there's a particular text file that changes often on the web, and you have a SAS program that needs it, you can access the internet directly from SAS, like this:
filename demo URL "http://www.pop.psu.edu:80/~miller/sastricks/sastricks1.txt";
/* if you need username/password, put user="###" pass="###" (with ### being username/password) at end */
data demodat;
infile demo dlm='09'x;
input a b c;
run;
Putting "ERROR" in the log without seeing the put statement itself
If you're like me, I like to check for certain data problems that may occur,
and put an "ERROR:" in the log with a put statement. However, when searching for
"ERROR" in the log, you keep running into this put statement, even though there
is no error.
Replace:
if {whatever} then put "ERROR: This didn't work";
with:
if {whatever} then put "E%str()RROR: This didn't work";
Know if a global macro exists
Sometimes it's useful to know if a global macro exists. This is most elegant way I've seen to check:
%macro test;The "%global mvar" statement does not hurt the value of mvar if it already exists, the command is simply ignored by SAS.
%global mvar;
%if %length(&mvar.)=0 %then %put macro variable mvar did not exist before;
%if %length(&mvar.)>0 %then %put macro variable mvar already existed!;
%mend;
%test;
I use this sort of test when I have a SAS program like this:
%let mvalue=6;If I want another SAS program to "%include" this program, and have an "mvalue" of it's own (ignoring the "6"), you can do this:
{bunch of code using mvalue}
%macro test;
%global mvalue;
%if %length(&mvar.)=0 %then %let mvalue=6;
/* if running this program by itself, use 6, otherwise, leave the value alone. */
%mend;
%test;
Useful SAS links
There's dozens of SAS tricks pages out there. Here's a few I've found useful:
- David Franklin - Useful SAS Code
- UCLA SAS FAQs
- University of Texas SAS FAQs
- SAS Users Group International Online Proceedings
Clearing the log and output
If you're using PC-SAS, you can clear the log and output windows
automatically (each time you run the program) by placing this at the top of your
SAS code:
dm 'clear log';
dm 'clear output';
X command
This allows you to run a command prompt statement. As an example, this would
rename file "file1.txt" to "file1renamed.txt":
x 'mv file1.txt file1renamed.txt';
%include command
This lets you run another SAS job within another SAS job. It would look like
this (where "subroutine.sas" is the name of the included SAS program):
%include "subroutinename.sas";When SAS comes to this statement, everything in the "subroutine.sas" program is run before SAS moves on.
Array of macro variables
This rather long-winded example that I gave at the Macros II workshop makes a list of macro variables, one for each variable in your dataset: So if your dataset had five variables (A, B, C, D, E), there will be five macro variables:
&var1. with the value of "A"Why would you want this? Well, you can then use %IF-%DO loops to do operations on all of your variables, without cumbersome SAS array definition in every datastep. To set this up, run the following six lines of code (with "datasetname" replaced by your dataset's name):
&var2. with the value of "B"
&var3. with the value of "C"
&var4. with the value of "D"
&var5. with the value of "E"
You also get a &varnum. macro variable with a value of "5" (the number of variables in the dataset)
proc contents data=datasetname out=datasetcontents;After you run this code, you'll have one macro variable for each variable name in your dataset, plus an overall count of number of variables (see example above). This is useful because you can then use these macro variables in procedures and other datasteps, like this silly example:
run;
data datasetcontents;
set datasetcontents;
call symput(compress('var'||_N_),NAME);
call symput('varnum',_N_);
run;
%do i=1 %to &varnum.;This will print a list of each variable's values, one at a time, to the output window. Note that this code works regardless of how many variables are in the dataset, even if you don't know their names ahead of time. This can save you a lot of time if you're trying to explore different values in your dataset.
proc print data=datasetname;
var &&var&i.;
run;
%end;

