DATA Step, Macro, Functions and more

Call symput within a local macro?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

Call symput within a local macro?

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


Accepted Solutions
Solution
‎12-29-2014 01:02 PM
SAS Employee
Posts: 19

Re: Call symput within a local macro?

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);

View solution in original post


All Replies
Trusted Advisor
Posts: 1,137

Re: Call symput within a local macro?

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

Thanks,
Jag
Occasional Contributor
Posts: 13

Re: Call symput within a local macro?

Posted in reply to Jagadishkatam

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

Solution
‎12-29-2014 01:02 PM
SAS Employee
Posts: 19

Re: Call symput within a local macro?

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);

Occasional Contributor
Posts: 13

Re: Call symput within a local macro?

Posted in reply to StatsGeek

Thanks a lot Stephen! That worked perfectly!

Super User
Posts: 10,020

Re: Call symput within a local macro?

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

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 352 views
  • 3 likes
  • 4 in conversation