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

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.

BASUG is hosting free webinars Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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