BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
theponcer
Quartz | Level 8

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...

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

View solution in original post

6 REPLIES 6
Astounding
PROC Star

Easy?  Maybe.

 

%let n=1;
%let count1 = subcount1;
%let subcount1 = 100;

test2="&&&&&&count&n."; 
ballardw
Super User

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.

 

Tom
Super User Tom
Super User

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

 

Kurt_Bremser
Super User

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.

Tom
Super User Tom
Super User

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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1405 views
  • 7 likes
  • 5 in conversation