to CITY20. If N has the value 8, the reference would be to CITY8. If the value of N is 3,
the reference would be to CITY3.
Although for this example the type of reference that you want is CITY&N, the following
example will not produce the value of &N appended to CITY:
%put &city&n; /* incorrect */
This code produces a warning message saying that there is no macro variable CITY
because the macro facility has tried to resolve &CITY and then &N and concatenate
those values.
When you use an indirect macro variable reference, you must force the macro processor
to scan the macro variable reference more than once. This process will resolve the
desired reference on the second, or later, scan. To force the macro processor to rescan a
macro variable reference, you use more than one ampersand in the macro variable
reference. When the macro processor encounters multiple ampersands, its basic action is
to resolve two ampersands to one ampersand. For example, for you to append the value
of &N to CITY and then reference the appropriate variable name, do the following:
%put &&city&n; /* correct */
If &N contains 6, when the macro processor receives this statement, it performs the
following steps:
1. resolves && to &
2. passes CITY as text
3. resolves &N into 6
4. returns to the beginning of the macro variable reference, &CITY6, starts resolving
from the beginning again, and prints the value of CITY6
Generating a Series of Macro Variable References with a Single
Macro Call
Using indirect macro variable references, you can generate a series of references with a
single macro call by using an iterative %DO loop. The following example assumes that
the macro variables CITY1 through CITY10 contain the respective values Cary, New
York, Chicago, Los Angeles, Austin, Boston, Orlando, Dallas, Knoxville, and Asheville:
%macro listthem;
%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.
34 Chapter 3 • Macro Variables