Storing Only One Copy of a Long Macro Variable Value
Because macro variables can have very long values, the way you store macro variables
can affect the efficiency of a program. Indirect references using three ampersands enable
you to store fewer copies of a long value.
For example, suppose your program contains long macro variable values that represent
sections of SAS programs:
%let pgm=%str(data flights;
set schedule;
totmiles=sum(of miles1-miles20);
proc print;
var flightid totmiles;);
You want the SAS program to end with a RUN statement:
%macro check(val);
/* first version */ &val
%if %index(&val,%str(run;))=0 %then %str(run;);
%mend check;
First, the macro CHECK generates the program statements contained in the parameter
VAL (a macro variable that is defined in the %MACRO statement and passed in from
the macro call). Then, the %INDEX function searches the value of VAL for the
characters run;. (The %STR function causes the semicolon to be treated as text.) If the
characters are not present, the %INDEX function returns 0. The %IF condition becomes
true, and the macro processor generates a RUN statement.
To use the macro CHECK with the variable PGM, assign the parameter VAL the value
of PGM in the macro call:
%check(&pgm)
As a result, SAS sees the following statements:
data flights;
set schedule;
totmiles=sum(of miles1-miles20);
proc print;
var flightid totmiles;
run;
The macro CHECK works properly. However, the macro processor assigns the value of
PGM as the value of VAL during the execution of CHECK. Thus, the macro processor
must store two long values (the value of PGM and the value of VAL) while CHECK is
executing.
To make the program more efficient, write the macro so that it uses the value of PGM
rather than copying the value into VAL:
%macro check2(val); /* more efficient macro */ &&&val
%if %index(&&&val,%str(run;))=0 %then %str(run;);
%mend check2;
%check2(pgm)
The macro CHECK2 produces the same result as the macro CHECK:
data flights;
Writing Efficient Macros 149
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset