Reputation: 588
Why does the following piece of code in SAS give the output x=1?. I am confused.
data strange;
x=.;
if x < 10 then x=1;
else if x >= 10 then x=2;
else x=3;
run;
Upvotes: 2
Views: 4191
Reputation: 63434
As noted in Order of Missing Values and in SAS Operators In Expressions:
Within SAS, a missing value for a numeric variable is smaller than all numbers; if you sort your data set by a numeric variable, observations with missing values for that variable appear first in the sorted data set. For numeric variables, you can compare special missing values with numbers and with each other.
As such, testing for 'less than' will include missing values. You would need to add
if x < 10 and not missing(x) then x=1;
or similar.
There is however one case this is not true: in using the ifn
(or ifc
) functions. Those support three valued logic:
y = ifc(x,'Nonzero','Zero','Missing');
However, that doesn't work in your case, as:
y = ifn(x<10,1,2,3);
will never evaluate to missing (since x<10
evaluates to true
for missing).
Upvotes: 6