DATA Step, Macro, Functions and more

Unable to return value of Macro variable created using call symput

Reply
Occasional Contributor
Posts: 19

Unable to return value of Macro variable created using call symput

Hi Experts, I am trying to return the value of macro variable that has been created using call symput but I am unable to do so.

Below is the piece of code I am trying:

 

%macro add_sym(a = , b = );
data _null_;
call symputx('d',%eval(&a + &b));
run;
&d.
%mend add_sym;

 

data _null_;
sum = %add_sym(a = 1, b = 4);
put sum = ;
run;

 

Please provide your expert help.

 

Thanks,

Gurpreet Kaur

Super User
Posts: 10,209

Re: Unable to return value of Macro variable created using call symput

Posted in reply to gurpreetkaur

The macro PREprocessor is just a program text generator, so your code looks like this after the macro has been resolved:

data _null_;
sum = data _null_;
call symputx('d',%eval(&a + &b));
run;
&d.
put sum = ;
run;

which will fail, for very obvious reasons.

Remove the data step from the macro to make it work:

%macro add_sym(a = , b = );
%let d=%eval(&a + &b);
&d.
%mend add_sym;

data _null_;
sum = %add_sym(a = 1, b = 4);
put sum = ;
run;

But if you are searching for a language element that helps you simplify calculations within a data step, look into user-defined functions.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 9,599

Re: Unable to return value of Macro variable created using call symput

Posted in reply to gurpreetkaur

Why exactly?  Macro language is a text only generation tool.  It is not a language to be doing data processing in.  Your doing several implicit conversions here which may or may not be as intended, your also comparing two text values, which don't necessarily convert to numeric, for example consider:

data _null_;
  sum = %add_sym(a = xyz, b = 4);
run;

Its a valid call, but your code will fail, in fact your code will fail in all but exactly integer values.  

 

Ask a Question
Discussion stats
  • 2 replies
  • 55 views
  • 1 like
  • 3 in conversation