I am not able to get why the following code is not working properly. Can anybody pls help.
%let x = dav;
data xx;
if &x = div then call symput('holdid', 'div' );
else if &x = dav then call symput('holdid', 'dav');
run;
%put x= &x;
%put holdid= &holdid
Why the result is coming as follows and what might be alternative solution?
SYMBOLGEN: Macro variable X resolves to dav
713
714
715 %put x= &x;
x= dav
716 %put holdid= &holdid;
SYMBOLGEN: Macro variable HOLDID resolves to div
holdid= div
Certainly LinusH is correct in that you could always use:
%let holdid = &x;
Probably your question is a simplification of a more complicated scenario. The reason you are getting your results goes back to this DATA step statement:
if dav = div then call symput (.....);
That's the code that SAS sees, after macro language finishes resolving &X. In a DATA step, DAV and DIV will both be numeric variables with a missing value, so the comparison will be true. One way to get around it, if you really need to do that for a more complex scenario, is this:
if "&x" = "div" then call symput (.....);
That way, after resolving &X, SAS compares:
if "dav" = "div" then call symput (.....);
Good luck.
What are you trying to do?
This piece of code doesn't do anything...?
If you are trying to assign the macro variable holid depending on the value of X, the you should use pure macro logic, no use for a data step if this is the case.
Ok, then can you tell me why I am not getting the expected result.
data xx;
x=dav;
if x = div then call symput('holdid', 'div' );
else if x = dav then call symput('holdid', 'dav');
run;
%put holdid= &holdid;
I am getting the following result:
SYMBOLGEN: Macro variable HOLDID resolves to div
807
808
809 %put holdid= &holdid;
holdid= div
But you are getting the expected result.
X is a numeric variable, with a missing value.
DAV is a numeric variable, with a missing value.
DIV is a numeric variable with a missing value.
So this comparison is true:
if x = div then call symput ....;
Therefore, the ELSE statement is ignored.
Thanks Astounding. I mixed up the datastep processing concepts with macro processing concepts.
Certainly LinusH is correct in that you could always use:
%let holdid = &x;
Probably your question is a simplification of a more complicated scenario. The reason you are getting your results goes back to this DATA step statement:
if dav = div then call symput (.....);
That's the code that SAS sees, after macro language finishes resolving &X. In a DATA step, DAV and DIV will both be numeric variables with a missing value, so the comparison will be true. One way to get around it, if you really need to do that for a more complex scenario, is this:
if "&x" = "div" then call symput (.....);
That way, after resolving &X, SAS compares:
if "dav" = "div" then call symput (.....);
Good luck.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.