DATA Step, Macro, Functions and more

if then else statement with three conditions

Accepted Solution Solved
Reply
Contributor
Posts: 38
Accepted Solution

if then else statement with three conditions

[ Edited ]

 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.


Accepted Solutions
Solution
a week ago
Super User
Posts: 13,941

Re: if then else statement with three conditions

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


All Replies
Solution
a week ago
Super User
Posts: 13,941

Re: if then else statement with three conditions

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

 

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

Contributor
Posts: 38

Re: if then else statement with three conditions

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.

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 91 views
  • 0 likes
  • 2 in conversation