Phishypenguin
Phishypenguin

Reputation: 35

SAS else if clause confusion

I'm running the following code:

data new;  
set old;    
    if visits=. then band='Poor';  
    else if visits=1 or visits=2 then band='Low';  
    else band='High';  
run;

My confusion is when the else if statement is changed to:

else if visits=1 or 2 then band='Low';

Why does the value Low appear as the band for cases where visits are greater than 2 instead of High?

Upvotes: 1

Views: 106

Answers (1)

Martin
Martin

Reputation: 16433

This is because your if statement is faulty in this case:

else if visits=1 or 2 then band='Low';

You are mistakenly assuming that this is effectively:

if visits is 1, or visits is 2 then ...

In fact, this is actually:

if visits is 1, or 2 is true then ...

So you are saying does 2 = true, which it does (all non-zero values are implicitly true). Effectively your final else statement (for High) is always ignored because the else if will always be true.

Stick with your original statement, which does exactly what you expect it to:

else if visits=1 or visits=2 then band='Low';

Upvotes: 3

Related Questions