Lisa Sanbonmatsu, Consultant, Somerville, MA: Moving From Macro Variables To Macros
Lisa Sanbonmatsu, Consultant, Somerville, MA: Moving From Macro Variables To Macros
Lisa Sanbonmatsu, Consultant, Somerville, MA: Moving From Macro Variables To Macros
VARIABLE DESCRIPTION EXAMPLE If the last data set we created is WORK.TEMP, the
SYSDATE date SAS session 30SEP00 SAS macro facility would substitute
started “WORK.TEMP” wherever “&SYSLAST” appears.
At run time, the facility will generate the equivalent
SYSDATE9 date in DATE9. 30SEP2000
of:
format
SYSDAY day session Saturday proc print data=WORK.TEMP (obs =
started 10);
SYSLAST last data set WORK.TEMP footnote “Obs from WORK.TEMP”;
created footnote2 "Printed on 30SEP00.";
run;
SYSSCP host system WIN
SYSTIME time session 10:09
started And produces the following footnotes:
SYSVER SAS Version 7.00
Observations from WORK.TEMP
SYSUSERID userid USER
Printed on 30SEP00.
Automatic variables are easy to spot in code because they
In footnote2 we needed to use a double-period after
begin with “&AF” and “&SYS”. To see the values currently
&SYSDATE: the first period is interpreted by the
assigned to all automatic macro variables, type the following
macro facility as the end of the macro name and the
line of code:
second period is treated as text. No period is
necessary after the other automatic variables in the
%put _automatic_;
above code because they are followed by a space,
double quote, or other punctuation that makes the
This will send a list of the current values (including empty end of the variable name clear.
values) to your log:
Macro variables are an easy way to add flexibility to your Values that are likely to change and appear multiple
code by reducing “hard coding.” Typing specific information times in your code should generally be assigned with
into your program is “hard coding” it. In the example below, a %Let statement. Interest rates, selection criteria,
all the specifics such as the month, the type of books, and the file names, dates and other specifics that are likely to
number of top books are hard coded: change are good candidates for macro variables.
Within the Macro At the start of this section, we set out to print our top
PRINT_IT DATANAME work.sales06 book list for each of five different types of books.
PRINT_IT NUM_OBS 5
GLOBAL MONTH 06
Applying the steps above, we can create a TOPLIST
... macro and easily call it five times:
Within the Macro
PRINT_IT DATANAME work.sort06 %MACRO TOPLIST(month = 06,
PRINT_IT NUM_OBS 10 type = Fiction,
GLOBAL MONTH 06 criteria=Volume,
... topnum= 10);
Outside of the Macro
GLOBAL MONTH 06 proc sort data = sales&month
(where = (type = "&type") )
out = sort&month;
by descending &criteria;
run;
5. CONDITIONALLY EXECUTING DATA
proc print data = sort&month STEPS
(obs = &topnum) noobs; Macros allow you to conditionally execute entire
var book author &criteria;
data steps or sections of code. One way of
title1 "Top &topnum &type
Books for &month.00"; conditionally executing code within a macro is to use
title2 "Based on &criteria"; a %IF statement. Suppose that some of the time we
run; want to create the data set (i.e., create
%MEND; SORT06.SD2), but not print out the list of books.
We could add a parameter to our macro called
%TOPLIST (type = Fiction) “PRINTLIST” to indicate whether or not we want to
%TOPLIST (type = Sci-Fi) print. We would also need to enclose our proc print
%TOPLIST (type = Reference) within an %IF block. The format for %IF is:
%TOPLIST (type = Mystery)
%TOPLIST (type = Non-Fiction)
%IF <condition> %THEN %DO;
...
Clearly, this is a lot neater than if we had cut and pasted the SAS Code
program five times! Note that the “%TOPLIST” calls must ...
be placed in the program AFTER (but not necessarily %END;
immediately after) the definition of the macro (“%MACRO..
%MEND”). A macro must be compiled (submitted) before it
is called in your program.
We could also use the macro calls to specify different Adding %IF to our code:
numbers of “top” books to print depending on the type of
book: %MACRO TOPLIST(month = 06,
type = Fiction,
%TOPLIST (type = Fiction, topnum=25) criteria=Volume,
%TOPLIST (type = Sci-Fi, topnum = 3) topnum= 10,
printlist=Y);
%let i=3;
%MACRO TOPLIST(saledata=,
%let type1=Fiction;
%let type2=Sci-Fi;
%let type3=Mystery;
CONCLUSION
The macro facility is a text substitution mechanism. Through
macro variables and macros, the macro facility offers many
opportunities for flexible programming of complex and
repetitive tasks. Macro variables are easy to use. With macro
variables, even a beginning programmer can add flexibility to
his or her programs. Once macro variables have been
mastered, it is a short step to writing actual macros. Moving
from writing basic macros to writing complex macros,
however, takes more time.
REFERENCES
CONTACT INFORMATION
Lisa Sanbonmatsu
Harvard University
Malcolm Wiener Center for Social Policy
79 JFK Street
Cambridge, MA 02138
Work Phone: 617-495-5131
Email: [email protected]