21.8.09

Macro Variable Resolution Using Multiple Ampersands

Prepared by Meena R S (meena@kreara.com)


The SAS macro consists of two basic parts: macros and macro variables. The names of macro variables are prefixed with an ampersand (&) while the names of macros are prefixed with percent sign (%). Prior to the execution of the SAS code the macro variables are resolved. The resolved values are then substituted back into the code.


The macro variable references that have been described with one ampersand preceding the macro variable name are direct reference to a macro variable. In indirect referencing, more than one ampersand precedes a macro variable reference. The macro processor follows specific rules in resolving references with multiple ampersands.


The rules that the macro processor uses to resolve macro variable reference that contain multiple ampersands follow

  • Macro variable references are resolved from left to right
  • Two ampersands (&&) resolve to one ampersand (&)
  • Multiple leading ampersands cause the macro processor to rescan the reference until no more ampersands can be resolved.


Consider the example below.


Options symbolgen;

%let section4 =operating system;

%let n=4;

%put &&section&n;


For the above code, on the first pass the two ampersands are resolved to one and &n is resolved to 4, yielding &section4. On the second pass the macro variable reference &section4 resolves to operating system.


The following figure shows the process of resolving the macro variable reference in the program.





%let a =freight;

%let b=passenger;

%let c=special;

%let code=a;

%put &code;

%put &&code;

%put &&&code;


The following demonstrates how the macro variables with multiple ampersands are resolved.



No comments: