DATA Step, Macro, Functions and more

Macro variable to call a code

Accepted Solution Solved
Reply
Super Contributor
Posts: 371
Accepted Solution

Macro variable to call a code

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

 

 

 


Accepted Solutions
Solution
‎05-13-2016 09:04 AM
Super User
Posts: 5,081

Re: Macro variable to call a code

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


All Replies
Super User
Posts: 17,819

Re: Macro variable to call a code

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. 

PROC Star
Posts: 1,231

Re: Macro variable to call a code

[ Edited ]

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.

Solution
‎05-13-2016 09:04 AM
Super User
Posts: 5,081

Re: Macro variable to call a code

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.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 211 views
  • 1 like
  • 4 in conversation