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
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.
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.
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.