This week’s author tip is from Michele Burlew and her new book SAS Macro Programming Made Easy, Third Edition. Burlew chose this tip because she says it’s important to understand how SAS determines where a macro variable reference starts and stops, and often a delimiter is needed to tell SAS when to stop.
We hope you find this tip helpful. You can also read an excerpt from Burlew’s book online.
When you combine macro variable references with text or with other macro variable references, you can create new macro variable references. These new macro variable references are resolved before the SAS language statements in which they are placed are tokenized.
A concatenation operator is not needed to combine macro variables with text. However, periods (.) act as delimiters of macro variable references and might be needed to delimit a macro variable reference that precedes text.
When placing text before a macro variable reference or when combining macro variable references, you do not have to separate the references and text with a delimiter.
%let mosold=4; %let level=12; data book&mosold&level; set books.ytdsales(where=(month(datesold)=&mosold)); attrib over&level length=$3 label="Cost > $&level"; if cost > &level then over&level='YES'; else over&level='NO'; run;
When you follow a macro variable reference with text, you must place a period at the end of the macro variable reference to terminate the reference. The macro processor recognizes that a period signals the end of a macro variable name and determines that the name of the macro variable is the text between the ampersand and the period. While not required unless you follow a macro variable reference with text, all macro variable references can be terminated with periods.
%let prefix=QUESTION; proc freq data=books.survey; tables &prefix1 &prefix2 &prefix3 &prefix4 &prefix5; run;
After resolving the macro variable references, the program becomes:
proc freq data=books.survey; tables &prefix1 &prefix2 &prefix3 &prefix4 &prefix5; run;
The program is revised below. This newer version contains the necessary delimiters that tell the macro processor when the macro variable references end. Now the macro variable references resolve as desired, and the text that follows the references is concatenated to the results of the resolution.
%let prefix=QUESTION; proc freq data=books.survey; tables &prefix.1 &prefix.2 &prefix.3 &prefix.4 &prefix.5; run;
The macro processor substitutes QUESTION for the &PREFIX macro variable reference. After macro variable resolution, the program becomes:
proc freq data=books.survey; tables QUESTION1 QUESTION2 QUESTION3 QUESTION4 QUESTION5; run;
The excerpt is from SAS Press author Michele Burlew’s book “SAS Macro Programming Made Easy, Third Edition” Copyright © 2014, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED. (please note that results may vary depending on your version of SAS software).