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
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.
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.
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.
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.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.