BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

I want to ask if option1 and option2 are doing same or there is a difference?

Thanks

 

/***option1***/
IF x=5 OR (X=2 AND W=3) OR (X=2 AND W=8) AND Z=9 then Ind=1;
/***option2***/
IF (x=5 OR (X=2 AND W=3) OR (X=2 AND W=8)) AND Z=9 then Ind=1;
3 REPLIES 3
Astounding
PROC Star
Different...
Option 1 sets IND to 1 whenever X is 5. AND gets evaluated before OR without parentheses.

Option 2 requires Z to be 9 to set IND to 1.
Ronein
Onyx | Level 15

May anyone explain the calculation of Ind_a

I don't understand how it works,

why  X=5 W=100 Z=90 get ind_a=1??

Data ttt;
input x w z;
cards;
5 100 9
5 100 90
8 90 9
;
Run;

Data wanted;
set ttt;
IF x=5 OR (X=2 AND W=3) OR (X=2 AND W=8) AND Z=9 then Ind_a=1;
IF (x=5 OR (X=2 AND W=3) OR (X=2 AND W=8)) AND Z=9 then Ind_b=1;
Run; 

 

Kurt_Bremser
Super User

As already mentioned, AND is evaluated first, so this:

(X=2 AND W=8) AND Z=9

is calculated first; it is equivalent to

X=2 AND W=8 AND Z=9

all three conditions need to be satisfied for this part to become true. In your data, this is never the case.

Similarly, the other AND is evaluated (the brackets are not necessary, but they help in keeping the code clear).

Then come the ORs, and since X=5 is satisfied in the first two observations, the whole condition will become true.

In more readable form, your condition is this:

if
  x=5
  or (
    X=2 and W=3
  ) 
  or (
    X=2 and W=8 and Z=9
  )
then Ind_a=1;
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 988 views
  • 0 likes
  • 3 in conversation