I'm having a difficult time 'understanding' macros. What is their purpose and the value in putting a collective of values into a macro?
Thanks!
SAS macros are primarily SAS code generators. For example I have a group of macros that generate in excess of 10,000 lines of SAS SQL code yet the macros themselves are about 300 lines of code. That means I'm saving myself writing all of that SQL manually. Not only that I'm saving myself from doing any maintenance on those > 10K code lines.
There are some advantages of using macros in sas programming:
1) As @SASKiwi wrote - saving writing by calling same macro code.
It is like calling a function or a procedure and just change the arguments when need.
2) Generating different code according to situation defined by argument(s).
It is like a traffic light of a train to take the right track.
Argument are actually macro variables.
To use a macro variable in code it should be preceded by "&".
You can assign a value to a macro variable by different ways:
1) %let name = value;
It should be defined out of any data step or procedure but it can be used inside a macro program.
Refer to it in the code by &name.
2) call symput('name',<value>);
It is a sas statement in a data step.
Value can be given as a literal or as a variable or as an expression;
In next example I shall use MP for macro program and MV for macro variable:
3) %macro MPname (MVname1, MVname2, ... MVnamex=<MV value>, ... );
where MVname1, MVname2 should be supplied in same order as in %macro statement,
while MVnamex= is a named argument, should be supplied last.
if you have more then one named argument you can supply them in any order.
There is much more to learn about macro programming.
I hope this answers your question.
Here's one example of a use of macros:
I have a program that runs every month. The program figures out that today's date is 02DEC2020. Then, it has to extract the data from a database from the last 12 months (or specifically, December 2019 to November 2020) for analysis. The macro (and macro variables) determine the time frame to use when extracting the data automatically, no human involvement needed. If I didn't use a macro, I would have to change the code every month to pick the proper 12 months, so human involvement would be needed.
This is just one example of the use of macros. Macros can make your code dynamic, so that it changes as the situation changes.
I would not call it dumb. Our current problems with macro usage (as reflected by hundreds of threads here on the communities) comes from the fact that macros are taught MUCH too early in the Base SAS Programming courses.
When I started, I spent 3 (as in three - THREE!) weeks in Heidelberg learning DATA and PROC steps, reading external files, sorting, merging, doing summaries, creating output, before, in the 4th week, we got to SQL and then macro programming. So, at that stage, I had sufficient knowledge about the code I would create with the macro processor.
@crawfjor wrote:
I'm having a difficult time 'understanding' macros. What is their purpose and the value in putting a collective of values into a macro?
Thanks!
A macro symbol (or macro variable) is a way to re-use the same string of characters many places in your program without re-typing them over and over. So if you wanted use the same list of variable names in multiple statements you could put the list in a macro variable and then reference the macro variable where ever you needed that list in your code. It can also serve as a simple way to make your program dynamic so that if you wanted to do the same thing another day but with a different list of variables (or whatever type of string the macro variable holds) you only need to modify that one line of the program.
%let varlist=AGE HEIGHT ;
proc means data=sashelp.class ;
var &varlist;
run;
proc glm data=sashelp.class;
class sex;
model weight=sex &varlist;
run;
An actual macro allows you to use conditional logic and generate much more complex text than the simple text expansion of a macro variable.
%macro analysis(data=,varlist=);
proc means data=&data ;
var &varlist;
run;
proc glm data=&data;
class sex;
model weight=sex &varlist;
run;
%mend analysis;
%analysis(data=sashelp.class,varlist=age height);
But the key thing to remember when using either macro variables or macros is that the goal of them it to help you generate valid SAS code that SAS can understand and run.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.