%let test = one;
%let one = two;
%put &test;
%put &&test;
%put &&&test;
%put &&&&test;
%put &&&&&test;
Well. I'm TOTALLY BEATEN by these ampersands. I don't understand why they need SO MANY ampersands before a macro variable. Is there any trick to master the usage of ampersand? BTW, what are the five results, correspondingly?
SAS | How to read character using Ampersand(&) We can use ampersand (&) to notify SAS to read the variable until there are two or more spaces encounter as a delimiter. This technique is always useful when the variable contains two or more words. Example 1: There are 2 spaces before 25, 32 and 30 in example code below.
You can use these references anywhere in a SAS program. To resolve a macro variable reference that occurs within a literal string, enclose the string in double quotation marks. Macro variable references that are enclosed in single quotation marks are not resolved.
Macro is a group of SAS statements that is referred by a name and to use it in program anywhere, using that name. It starts with a %MACRO statement and ends with %MEND statement.
%MEND macro-name; The macro-name specified in the %MEND statement must match the macro-name specified in the %MACRO statement. Note: While specifying the macro-name in the %MEND statement is not required, it is recommended. It makes matching %MACRO and %MEND statements while debugging easier.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With