Referencing Macro Variables Indirectly

Using an Expression to Generate a Reference

The macro variable references shown so far have been direct macro references that begin with one ampersand: &name. However, it is also useful to be able to indirectly reference macro variables that belong to a series so that the name is determined when the macro variable reference resolves. The macro facility provides indirect macro variable referencing, which enables you to use an expression (for example, CITY&N) to generate a reference to one of a series of macro variables. For example, you could use the value of macro variable N to reference a variable in the series of macro variables named CITY1 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 results that you expect, which is 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 and 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.
%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.
TIP: In some cases, using indirect macro references with triple ampersands increases the efficiency of the macro processor. For more information, see Writing Efficient and Portable Macros.