BookmarkSubscribeRSS Feed
gurpreetkaur
Fluorite | Level 6

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

2 REPLIES 2
Kurt_Bremser
Super User

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.  

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 924 views
  • 1 like
  • 3 in conversation