BookmarkSubscribeRSS Feed
thanikondharish
Calcite | Level 5
%let I=1;
%let zone_1=SOUTH;

%let SOUTH_STATE_1=DL;

%PUT && ZONE_&I_STATE_1;

I am trying to get the 'DL' value but the macro not resolving.
How to write % put to get value 'DL'





5 REPLIES 5
Kurt_Bremser
Super User

Simple answer: don't. If any other coder has to maintain such code, you're dead. Even if you emigrated to another continent. They'll track you down just to make an example of you.

 

Just to show you what kind of abomination results out of such mindless abuse of the macro preprocessor, here's the solution:

%let I=1;
%let zone_1=SOUTH;

%let SOUTH_STATE_1=DL;
%let third_part=_state_1;

%put &&&&&&zone_&i.&&third_part.;

Log excerpt:

33         %put &&&&&&zone_&i.&&third_part.;
DL

Note that I had to make up another macro variable to satisfy the necessary levels of indirection.

thanikondharish
Calcite | Level 5
How to do without third_part
Kurt_Bremser
Super User

@thanikondharish wrote:
How to do without third_part

No idea.

But you can try to do it in a data step (which I recommend for complicated string operations anyway):

%let I=1;
%let zone_1=SOUTH;

%let SOUTH_STATE_1=DL;

data _null_;
name1 = resolve('&' !! resolve('&zone_' !! "&i") !! '_state_1');
call symputx('result',name1);
run;

%put &result;

Log excerpt:

37         %put &result;
DL
Astounding
PROC Star

Try it this way:

 

%let I=1;
%let zone_1=SOUTH;

%let SOUTH_STATE_1=DL;

%put &&&&&&zone_&i.._state_1;
Tom
Super User Tom
Super User

I would agree that this level of complexity probably needs to be fixed earlier in the process.  

 

Try doing it piece by piece to see how to get there.  For example you could start with just getting the value of macro variable I resolved.

776   %PUT ZONE_&I._STATE_&I.;
ZONE_1_STATE_1

Now you want to have the ZONE_1 part resolved.  So add a double &&. Make sure to add another period so it doesn't try to resolve ZONE_1_STATE_1 instead.

777   %PUT &&ZONE_&I.._STATE_&I.;
SOUTH_STATE_1

So now you have the name of the macro variable you want to resolve. So again you need to add &&, but since you already are using that trick once to get ZONE_1 to resolve you need to double each of the new &'s.

778   %PUT &&&&&&ZONE_&I.._STATE_&I.;
DL

But it is probably easier to just build up the name into a new macro variable and then use triple & to show the value of variable whose name is in a variable.

So make ZONE_1

%let mvar=zone_&i;

Then get the value of ZONE_1.

%let mvar=&&&mvar;

Then tack on STATE_1.

%let mvar=&mvar._state_&i;

Now use the value of MVAR as the NAME of the macro variable you want to display.

785   %put &=mvar;
MVAR=SOUTH_state_1
786   %put &&&mvar;
DL

 

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1132 views
  • 4 likes
  • 4 in conversation