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
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.