sas MACRO ampersand -
%let test = one; %let 1 = two; %put &test; %put &&test; %put &&&test; %put &&&&test; %put &&&&&test;
well. i'm totally beaten these ampersands. don't understand why need many ampersands before macro variable. there trick master usage of ampersand? btw, 5 results, correspondingly?
with single set of ampersands, pretty boring; after one, odd number of ampersands leads resolving twice, number of ampersands resolves once. use 1 ampersand resolve once , 3 ampersands resolve twice, unless have stock in company owns rights ampersand.
more interesting following test, shows why numbers of ampersands have value:
%let test = one; %let testtwo = one; %let 1 = 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 following:
- resolve single ampersand plus text macro variable reference.
- resolve pairs of ampersands 1 ampersand.
those done simultaneously , iteratively until ampersands gone, , each result kept next iteration , not affect current iteration. so, &test&one
becomes onetwo
because &test
-> 1 , &one
-> two. steps remaining:
&&test&one
->&testtwo
->one
.&&|test|&one
. double&&
before test becomes&
, test remains, ,&one
resolvestwo
. leaves&testtwo
second pass resolvesone
.&&&test&one
->&onetwo
-> not resolve.&&|&test|&one
->&|one|two
-> dnr.&&&&test&one
->&&testtwo
->&testtwo
-> one.&&|&&|test|&one
->&&|testtwo
->&testtwo
-> one. 2 pairs each resolve down one, making 1 pair, resolves one, leaves&testtwo
resolve.&&&&&test&one
similar 3 ampersand case, 1 pair.&&&&&&test&one
resolves&&&testtwo
resolves&one
resolves two.&&|&&|&&|test|&one
->&&|&testtwo
->&one
-> two. odd number of pairs means 1 more set of resolves.
at end of day, need remember:
- 1 ampersand resolves macro variable once , that's it.
- 2 ampersands useful composite macro variables, ie, prefix plus macro-driven suffix (
&&prefix&suffix
). - 3 ampersands useful going 2 deep in resolving single macro variable (
&&&var
->&var2
->var3
). - 6 ampersands useful resolving two-deep composite macro variable (ie, combining 2 , 3) ([
&prefix=var
,&suffix=2
]&&&&&&prefix&suffix
->&&&var2
->&var3
->4
).
beyond that, 4 or more (other 6) useful particularly complex combinations; levels used delay resolution until particular times.
Comments
Post a Comment