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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.