Intro to SAS Macro Programming (Part 2)

Feb 17, 2016

Intro to SAS Macro Programming (Part 2)


We've seen a first introduction, in part 1 of introduction to SAS macro programming.

In this part we will try to see how to write a complete macro statement and how to use multiple variables in a single macro statement.

Adding parameters to a SAS macro statement:

Syntaxe:
%Macro_name (Value_1,…….,Value_n);

Example 1:

In the example bellow, we will add a single parameter to our SAS macro that calculates basic descriptive statistics using proc means:
In this case, the parameter will be the table name:

%Macro AVG (Table);
Proc means data =&table;
run;
%mend;

And now we will see the result of our macro:
 
%AVG(SASHELP.Shoes);

This is the output we get from the macro we've just created and executed :



Adding multiple parameters to a SAS macro :



Example 2:

In this example bellow, now we will add 2 parameters to our SAS macro:

%Macro Analysis (Table,Variable);
Proc means data =&table mean;
Var &Variable;
run;
Proc Univariate data =&table ;
Histogram &variable ;
Run ;
%mend;

Let's test our macro :
        
      %Analysis(SASHELP.Shoes,Sales);

Bellow  the output of our macro, I haven't included all the output of the Univariate procedure because it would take more space in the page, but you get the point I think !





This was a quick introduction to SAS Macro. It is enough for anyone to get introduced with the basic concepts of the Macro programming in SAS. More advanced topics will be covered in futur posts.