Reputation: 237
I am a bit puzzled why the output is a certain way on the following program.
data a;
input name$ lv @@;
cards;
Frank 1 Joan 2 Sui 3 Burt 4 Kelly . Juan 1
;
data b;
set a;
if lv=.
then expertise='?';
else if lv=1
then expertise='L';
else if lv=2 or 3
then expertise ='M';
else expertise ='H';
run;
proc print data=b;
run;
In the program above, I am expecting that the output for the observation containing Burt has expertise of value H, but for some reason it is M.
I was thinking that the if statement should be lv=(2 or 3) but when I do that now those that I am thinking that the lv= 2 and lv=3 whose expertise should equal M now becomes H as well.
*This is probably explained by the fact that the syntax is inappropriate, and lv is never (2 or 3) /I am not sure why this does not cause an error/ thus the ELSE statement is executed.
I have a feeling that I am not understanding how the ELSE is really working.
However, according to that logic then if the lv=2 part is recognized but the others are not, I am not sure why the lv= 3 and lv=4 does not produce an H.
My goal is to understand why the program runs as if there is no syntax error and why the output is what it is.
Upvotes: 1
Views: 72
Reputation: 51611
Your syntax is incorrect, but not illegal, so SAS did what you asked for instead of what you wanted. The condition lv=2 or 3
is evaluated from left to right. So lv=2
will result in either 1
or 0
depending whether the value of lv
is 2
or not. But both 1 or 3
and 0 or 3
will be true since 3
is always considered true.
What you really want is the in
operator.
if missing(lv) then expertise='?';
else if lv=1 then expertise='L';
else if lv in (2 3) then expertise ='M';
else expertise ='H';
Upvotes: 2
Reputation: 21274
Your mistake is here
If lv = 4 or 0
You think this means
If lv = 4 or lv = 0
But it doesn’t. What actually happens is
If (lv = 4) or 0
In SAS 0 is false and any number greater than 1 is true. Not sure about decimals. Anyways. The or condition makes this one false. But in your first example, it makes it always true.
Upvotes: 2