%let test = one;
%let one = two;
%put &test;
%put &&test;
%put &&&test;
%put &&&&test;
%put &&&&&test;
<
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:
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:
&&prefix&suffix
).&&&var
-> &var2
-> var3
).&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.