Here's an example:
%let n=1;
%let count1 = subcount1;
%let subcount1 = 100;
data test;
set mor_filelist_ma;
n="&n."; *Resolves to 1 as expected;
count1="&count1."; *resolves to subcount1 as expected;
subcount1="&subcount1."; *resolves to 100 as expected;
test1="&count&n."; *resolves to subcount1 as expected;
test2="&&&count&n."; *resolves to subcount1 - expecting 100;
run;
How do I get test2 to resolve to 100 instead of subcount1? I'm thinking this is easy and I'm just missing something...
If you are using a data step anyway you can make it easier by using SYMGET() and SYMGETN() functions instead of trying to use macro code to resolve the references.
599 %let n=1;
600 %let count1 = subcount1;
601 %let subcount1 = 100;
602
603 data test;
604 length name1 name2 $32 value 8 ;
605 name1=cats('count',symgetn('n'));
606 name2=symget(name1);
607 value=symgetn(name2);
608 put (_all_) (=);
609 run;
name1=count1 name2=subcount1 value=100
Please post the log from the data step. I have an inkling that there's a clue in there.
Easy? Maybe.
%let n=1;
%let count1 = subcount1;
%let subcount1 = 100;
test2="&&&&&&count&n.";
Indirect referencint (nothing "nested" here) can be problematic in many ways, often involving way more references than you think are needed:
Consider this output:
227 %let n=1; 228 %let count1 =subcount1; 229 %let subcount1 =100; 230 231 %let test2=&&&&&&count&n.; 232 233 %put &test2; 100
2,3,4 and 5 & at the beginning are insufficient, 6 works on my system.
Which why you really want to determine if the indirect reference is really needed.
Don't do that. https://www.amazon.com/Doctor-hurts-when-Jokes-Cartoons/dp/150043230X
Since you need 3 to get value of COUNT1 then you will probably need 6 to get the value macro variable named in COUNT1.
529 %let n=1; 530 %let count1 = subcount1; 531 %let subcount1 = 100; 532 %put &=n &=count1 &=subcount1 ; N=1 COUNT1=subcount1 SUBCOUNT1=100 533 %put &&&count&n; subcount1 534 %put &&&&&&count&n; 100
But it is just much easier on your brain if you do the steps explicitly instead.
535 %let var1=count&n; 536 %let var2=&&&var1; 537 %let var3=&&&var2; 538 %put &=var1 &=var2 &=var3; VAR1=count1 VAR2=subcount1 VAR3=100
How do the six ampersands work?
In the first pass, double ampersands are converted to single ampersands, and single ampersands are resolved:
&& && && count &n
resolves to
& & & count 1
(blanks inserted to make it easier to read)
In the next pass, the same happens:
&& &count1
resolves to
& subcount1
and that is resolved in the third pass to 100.
If you are using a data step anyway you can make it easier by using SYMGET() and SYMGETN() functions instead of trying to use macro code to resolve the references.
599 %let n=1;
600 %let count1 = subcount1;
601 %let subcount1 = 100;
602
603 data test;
604 length name1 name2 $32 value 8 ;
605 name1=cats('count',symgetn('n'));
606 name2=symget(name1);
607 value=symgetn(name2);
608 put (_all_) (=);
609 run;
name1=count1 name2=subcount1 value=100
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.