sas MACRO ampersand

With a single set of ampersands, what you get is pretty boring; after one, odd number of ampersands leads to resolving twice, even number of ampersands resolves once. So you use 1 ampersand to resolve once and 3 ampersands to resolve twice, unless you have stock in the company that owns rights to the ampersand.

More interesting is the following test, which shows why even numbers of ampersands have value:

%let test = one;
%let testtwo = one;
%let one = two;
%let two=three;

%put &test&one;
%put &&test&one;
%put &&&test&one;
%put &&&&test&one;
%put &&&&&test&one;
%put &&&&&&test&one;

Basically, each pass through, SAS does the following:

  • Resolve any single ampersand plus text to a macro variable reference.
  • Resolve any pairs of ampersands to one ampersand.

Those are done simultaneously and iteratively until all ampersands are gone, and each result is kept for the next iteration and does not affect the current iteration. So, &test&one becomes onetwo because &test-> one and &one -> two. Steps for the remaining:

  • &&test&one -> &testtwo -> one. &&|test|&one. The double && before test becomes &, test remains, and &one resolves to two. That leaves &testtwo for the second pass which resolves to one.
  • &&&test&one -> &onetwo -> does not resolve. &&|&test|&one -> &|one|two -> DNR.
  • &&&&test&one -> &&testtwo -> &testtwo -> one. &&|&&|test|&one -> &&|testtwo -> &testtwo -> one. Two pairs each resolve down to one, making one pair, which then resolves to one, which leaves &testtwo to resolve.
  • &&&&&test&one is similar to three ampersand case, but with one extra pair.
  • &&&&&&test&one resolves to &&&testtwo resolves to &one resolves to two. &&|&&|&&|test|&one -> &&|&testtwo -> &one -> two. The odd number of pairs means we get one more set of resolves.

At the end of the day, what you need to remember:

  • 1 ampersand resolves the macro variable once and that’s it.
  • 2 ampersands is useful for composite macro variables, ie, a prefix plus a macro-driven suffix (&&prefix&suffix).
  • 3 ampersands is useful for going two deep in resolving a single macro variable (&&&var -> &var2 -> var3).
  • 6 ampersands is useful for resolving a two-deep composite macro variable (ie, combining 2 and 3) ([&prefix=var, &suffix=2] &&&&&&prefix&suffix -> &&&var2 -> &var3 -> 4).

Beyond that, 4 or more (other than 6) are useful only for particularly complex combinations; the extra levels would be used to delay resolution until particular times.

Leave a Comment