Macro Variables.


The SAS Macro Facility

The SAS Macro Facility is a is a text processing system that provides a rather powerful method to automate many aspects of programming SAS. There are several advanced capabilities of the macro facility that are covered in more details in the advanced course that allow conditional generation of SAS code.


In this module I'll only introduce macro variables. Macro variables are part of the macro facility that allows one to have named pieces of text that can be re-used. Macro variables make up only a small part of the macro facility, however are perhaps the most often used portion of this facility. Even for macro variables there is quite a bit of capability that I do not cover here. However the simple uses of macro variables I show here should often be useful and save you some time in coding.



User Defined Macro Variables, %let

Since user defined macro variables are the ones I use most often I will start with them. To illustrate user defined macro variables I use an example program that has the same text appearing multiple times.


    data tmp (drop=gender);
	set s5238.nh1s5238(keep=gender age dbp bmi);
	where gender="Female";
run;
	
proc print data=tmp(obs=10);
	var age dbp bmi;
run;

proc univariate data=tmp cibasic;
	var age dbp bmi;
run;

proc corr data=tmp;
	var age dbp bmi;
run;
     


Example program


The program can be improved with a user-defined macro variable.


User Defined Macro Variables, %let

Use of a single macro variable makes this program much easier to modify. The advantage of the macro variable is that it requires only the change in the macro variable definition to make the change everywhere.


Displaying macro variables is accomplished using the %put statement. (The percent symbol, %, is also a macro trigger). The value of the macro variable is displayed in the SAS log.


Displaying Macro Variables, %put

The second type of macro variable is automatic macro variables.


Automatic Macro Variables


Automatic Macro Variables

An example of using Automatic Macro Variables

Embedding Automatic Macro Variables in Titles and Footnotes


Summary -- Things to Remember


Exercise

One often uses simulation to examine the sampling distribution of statistics. The following program simulates the sampling distribution of the mean from a normal population when the sample size is 9. The random number stream is initialized using the function streamint, 1,000 samples of size 9 are drawn, the mean for each sample calculated, and the 1,000 resulting means examined using proc univariate. How might user defined macro variables be used so that if one wanted to 10,000 samples of size four and a different random number stream be used it could be done simply by changing the text defining the macro variables?

 data simnorms;
	call streaminit(57321);
	do samp=1 to 1000;
	mn_x=0;
	do obs=1 to 9;
		x=rand("normal",0,1);
		mn_x+x;
	end;
	mn_x=mn_x/9;
	output;
	end;
run;
ods select basicmeasures histogram goodnessoffit;
proc univariate data=simnorms;
var mn_x;
histogram mn_x/normal;	
run;