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

Hi Everyone,

 

 

I have a macro variable which is used to dictate what operation to be conducted.

 

%let CODE=1;

 

and I want to get something like the below. There is no variable in the dataset has the name code.

...

 

data a; set intermediate_file;

if &code=2 then do;

  number=1;end;

else

if &code=2 then do

 number=0;end;

run;

 

Could you please help me with that?

 

Thank you,

HHC

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

While unusual, your approach is fine.  The strange part is making the same comparison both times:

 

if &code=2 then do ...

 

Shouldn't one of those be changed to:

 

if &code=1 then do ....

 

Fixing the missing semicolon can't hurt, but it's a lucky accident that the code will work anyway.

View solution in original post

3 REPLIES 3
Reeza
Super User

Other than your IF condition being the same and not quite making sense your code should work as presented. 

 

 Both your conditions compare to the #2, which isn't what you intended. 

 

You can compare strings to strings and numbers to numbers so your macro variables don't have to resolve to a variable. 

Quentin
Super User

The code you have written is valid.  What is the problem you are having?

 

Note you may have a typo, currently IF condition and the ELSE IF condition are the same.  So the else if would never execute.

 

As written your code will resolve to:

 

data a; set intermediate_file;
if 1=2 then do;
  number=1;end;
else
if 1=2 then do
 number=0;end;
run;

 

It is unusual to have an IF statement that evaulates an expresssion with no variables, but it's valid code.  Since both the IF expression and the ELSE IF expression are false, the assignment statement will never be executed.

 

EDIT: noticed you are missing a semicolon after the second DO.

Astounding
PROC Star

While unusual, your approach is fine.  The strange part is making the same comparison both times:

 

if &code=2 then do ...

 

Shouldn't one of those be changed to:

 

if &code=1 then do ....

 

Fixing the missing semicolon can't hurt, but it's a lucky accident that the code will work anyway.

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
  • 3 replies
  • 1554 views
  • 1 like
  • 4 in conversation