## IF condition not evaluating as expected

Solved
Super Contributor
Posts: 328

# IF condition not evaluating as expected

[ Edited ]
 Obs ID time timesecond 1 MC_001 0 4.67 2 MC_001 6 4.00 3 MC_002 12 4.17 4 MC_002 18 4.67 5 MC_003 24 5.50 6 MC_003 30 4.67 7 MC_004 36 3.40 8 MC_004 42 3.67

The data set above and code below.

data longICP1;

set longICP;

if SID="MR_001" and 0<time<=900 and timesecond=4.67 then t=1; else t=0;

if SID="MR_002" and 0<time<=900 and timesecond=467. then t=1; else t=0;

if SID="MR_003" and 0<time<=900 and timesecond=5.50 then t=1; else t=0;

if SID="MR_004" and 0<time<=900 and timesecond=8.00 then t=1; else t=0;

run;

Accepted Solutions
Solution
‎04-11-2016 07:18 PM
Super User
Posts: 8,129

## Re: what's wrong with this code

Posted in reply to desireatem

The way you have setup the logic the value of T only depends on the last set of IF/THEN/ELSE statements.

Perhaps you meant something more like this?

``````t=0;
if SID="MR_001" and 0<time<=900 and timesecond=4.67 then t=1;
else if SID="MR_002" and 0<time<=900 and timesecond=467. then t=1;
else if SID="MR_003" and 0<time<=900 and timesecond=5.50 then t=1;
else if SID="MR_004" and 0<time<=900 and timesecond=8.00 then t=1; ``````

All Replies
Solution
‎04-11-2016 07:18 PM
Super User
Posts: 8,129

## Re: what's wrong with this code

Posted in reply to desireatem

The way you have setup the logic the value of T only depends on the last set of IF/THEN/ELSE statements.

Perhaps you meant something more like this?

``````t=0;
if SID="MR_001" and 0<time<=900 and timesecond=4.67 then t=1;
else if SID="MR_002" and 0<time<=900 and timesecond=467. then t=1;
else if SID="MR_003" and 0<time<=900 and timesecond=5.50 then t=1;
else if SID="MR_004" and 0<time<=900 and timesecond=8.00 then t=1; ``````

🔒 This topic is solved and locked.

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

Discussion stats