%do n=1 %to 10; &&city&n
%end;
%mend listthem;
%put %listthem;
This program writes the following to the SAS log:
Cary New York Chicago Los Angeles Austin Boston
Orlando Dallas Knoxville Asheville
Using More Than Two Ampersands
You can use any number of ampersands in an indirect macro variable reference, although
using more than three is rare. Regardless of how many ampersands are used in this type
of reference, the macro processor performs the following steps to resolve the reference.
%let var=city;
%let n=6;
%put &&&var&n;
1. It resolves the entire reference from left-to-right. If a pair of ampersands (&&) is
encountered, the pair is resolved to a single ampersand, then the next part of the
reference is processed. In this example, &&&VAR&N becomes &CITY6.
2. It returns to the beginning of the preliminary result and starts resolving again from
left-to-right. When all ampersands have been fully processed, the resolution is
complete. In this example, &CITY6 resolves to Boston, and the resolution process is
finished.
Note: A macro call cannot be part of the resolution during indirect macro variable
referencing.
T I P In some cases, using indirect macro references with triple ampersands increases
the efficiency of the macro processor. For more information, see Chapter 11,
“Writing Efficient and Portable Macros,” on page 143.
TIP:
Manipulating Macro Variable Values with Macro
Functions
When you define macro variables, you can include macro functions in the expressions to
manipulate the value of the variable before the value is stored. For example, you can use
functions that scan other values, evaluate arithmetic and logical expressions, and remove
the significance of special characters such as unmatched quotation marks.
To scan for words in macro variable values, use the %SCAN function:
%let address=123 maple avenue;
%let frstword=%scan(&address,1);
The first %LET statement assigns the string 123 maple avenue to macro variable
ADDRESS. The second %LET statement uses the %SCAN function to search the source
(first argument) and retrieve the first word (second argument). Because the macro
Manipulating Macro Variable Values with Macro Functions 37
processor executes the %SCAN function before it stores the value, the value of
FRSTWORD is the string 123.
For more information about %SCAN, see “%SCAN and %QSCAN Functions” on page
268. For more information about macro functions, see Chapter 12, “Macro Language
Elements,” on page 157.
38 Chapter 3 Macro Variables
..................Content has been hidden....................

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