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;
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;
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.