Sas macro ampersand

%let test = one; %let one = two; %put &test; %put &&test; %put &&&test; %put &&&&test; %put &&&&&test; 

Well. With these ampersands, I ALWAYS WAS FULLY. I don’t understand why they need much less ampersands before a macro variable. Is there a trick to mastering an ampersand? By the way, what are the five results, respectively?

+8
sas sas-macro ampersand
source share
1 answer

With one set of ampersands, what you get is pretty boring; after one, the odd number of ampersands resolves twice, even the number of ampersands resolves once. Thus, you use 1 ampersand to resolve one and three ampersands to resolve twice, if you do not have shares in the company that owns the rights to the ampersand.

More interesting is the following test, which shows why even the number of ampersands matters:

 %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 passes, SAS does the following:

  • Allow any ampersand character plus text in the macro definition link.
  • Allow any pairs of ampersands per ampersand.

This is performed simultaneously and iteratively until all ampersands disappear and each result is saved 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 rest:

  • &&test&one β†’ &testtwo β†’ one . &&|test|&one . Double && before the test becomes & , the test remains, and &one goes to two . This leaves &testtwo for the second pass, which resolves to one .
  • &&&test&one β†’ &onetwo β†’ not allowed. &&|&test|&one β†’ &|one|two β†’ DNR.
  • &&&&test&one β†’ &&testtwo β†’ &testtwo β†’ one. &&|&&|test|&one β†’ &&|testtwo β†’ &testtwo β†’ one. Two pairs resolve to one, making one pair, which then resolves one, which leaves &testtwo for resolution.
  • &&&&&test&one is like three ampersands, but with one extra pair.
  • &&&&&&test&one enables &&&testtwo allows &one allows up to two. &&|&&|&&|test|&one β†’ &&|&testtwo β†’ &one β†’ two. An odd number of pairs means that we get another set of permissions.

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

  • 1 ampersand allows a macro once and that it.
  • 2 ampersands are useful for compound macro variables, i.e. prefix plus suffix with macro definition ( &&prefix&suffix ).
  • The 3 ampersand is useful for moving to two depths in the resolution of one macro variable ( &&&var β†’ &var2 β†’ var3 ).
  • 6 ampersands is useful for solving a two-dimensional complex macro variable (i.e., combining 2 and 3) ([ &prefix=var , &suffix=2 ] &&&&&&prefix&suffix β†’ &&&var2 β†’ &var3 β†’ 4 ).

In addition, 4 or more (except 6) are only useful for particularly complex combinations; additional levels will be used to delay resolution until a certain time.

+12
source share

All Articles