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

 Hi

 

I want to create a variable by using "if then else" statement and specify three conditions about the variables in my dataset. I want that "new" variable must be equal to "1" only when all three specified conditions are met and if any one of the conditions is missing, it must be equal to "0".

 

For example, my code is:

data want;
set have;
if a>0 AND b='buy' AND c<0 then new=1;
else if a<0 AND b='sell' AND c>0 then new=1;
else new=0;
run;

The resulting "new" variable is equal to "1" when all three conditions are met. But it is also equal to "1" when first variable i.e. "a" is having a missing value in the data, and in this case only last two conditions (i.e. variable b and c) are considered. Whereas, I want new=1 only when all three conditions are met.

 

Kindly guide me where am I doing the mistake.

 

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Missing is always considered "less than" any thing. Try using

 

else if  . < a <0 AND b='sell' AND c>0 then new=1;

View solution in original post

2 REPLIES 2
ballardw
Super User

Missing is always considered "less than" any thing. Try using

 

else if  . < a <0 AND b='sell' AND c>0 then new=1;

Saba1
Quartz | Level 8

I am doing the following and it seems working:

  

data want;
set have;
if .<a>0 AND b='buy' AND c<0 then new=1;
else if .<a<0 AND b='sell' AND c>0 then new=1;
else new=0;
run;

Thanks.

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
  • 2 replies
  • 3713 views
  • 0 likes
  • 2 in conversation