Hello,
Following is the typical call symput procedure:
data a2;
set a1;
call symput("mvar",xyz);
run;
I want to do the following:
%macro abcd (data=,mvar=);
data a2;
set &data;
call symput("&mvar",xyz)
%mend;
%abcd (data=a0, mvar=mvar0)
%abcd (data=a1, mvar=mvar1);
However, I'm getting error. Can I assign macro variable name within call symput through a local macro? Is there any efficient way to do this?
Thanks,
P
Hi pmesh,
Your current code defines the new macro variables as local macro variables. To use them outside of the macro, just declare them as global macro variables. Changing your code as shown below fixes the problem.
%macro abcd(data=,mvar=);
data a2;
set &data;
%global &mvar;
call symput("&mvar",xyz);
run;
%mend;
%abcd (data=a0, mvar=mvar0);
%abcd (data=a1, mvar=mvar1);
This is a untested code, please try
%macro abcd (data=,mvar=);
data a2;
set &data;
call symput("mvar"||strip(&mvar),xyz)
%mend;
%abcd (data=a0, mvar=0);
%abcd (data=a1, mvar=1);
Thanks,
Jag
Hi Jag,
Thanks for your prompt reply. Unfortunately, it is not working and I'm getting an error saying that the symbolic variable name mvar. must contain only letters, digits, and underscores.
Any possibility of renaming the macro variable created by call symput?
Thanks,
P
Hi pmesh,
Your current code defines the new macro variables as local macro variables. To use them outside of the macro, just declare them as global macro variables. Changing your code as shown below fixes the problem.
%macro abcd(data=,mvar=);
data a2;
set &data;
%global &mvar;
call symput("&mvar",xyz);
run;
%mend;
%abcd (data=a0, mvar=mvar0);
%abcd (data=a1, mvar=mvar1);
Thanks a lot Stephen! That worked perfectly!
Or use the third parameter 'G'.
%macro abcd(data=,mvar=);
data a2;
set &data;
call symputx("&mvar",age,'G');
run;
%mend;
%abcd (data=sashelp.class, mvar=mvar0)
%put _global_ ;
Xia Keshan
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.