BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

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
Meteorite | Level 14

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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 504 views
  • 0 likes
  • 3 in conversation