Chapter Contents |
Previous |
Next |
SAS Macro Language: 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 allows 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 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 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, to append the value of &N to CITY and then reference the appropriate variable name, you use:
%put &&city&n; /* correct */
Assuming that &N contains 6, when the macro processor receives this statement, it performs the following steps:
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. For example,
%let var=city; %let n=6; %put &&&var&n;
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."
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.