BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sarojkumar
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

5 REPLIES 5
LinusH
Tourmaline | Level 20

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.

Data never sleeps
Sarojkumar
Calcite | Level 5

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

Astounding
PROC Star

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.


Sarojkumar
Calcite | Level 5

Thanks Astounding. I mixed up the datastep processing concepts with macro processing concepts. Smiley Happy

Astounding
PROC Star

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1188 views
  • 0 likes
  • 3 in conversation