How does IF in SAS treat a missing value??

Every programming language has an IF-THEN statement that branches according to whether a Boolean expression is true or false. In SAS, the IF-THEN (or IF-THEN/ELSE) statement evaluates an expression and braches according to whether the expression is nonzero (true) or zero (false). The basic syntax is

if numeric-expression then
 do-computation;
 else
 do-alternative-computation;

One of the interesting features of the SAS language is that it is designed to handle missing values. This brings up the question: What happens if SAS encounters a missing value in an IF-THEN expression? Does the IF-THEN expression treat the missing value as “true” and execute the THEN statement, or does it treat the missing value as “false” and execute the alternative ELSE statement (if it exists)?

data A;
input x @@;
if x then Expr="True "; 
     else Expr="False";
datalines;
1 0 .
;

proc print noobs; 
run;

 temp2

Ah-ha! SAS interprets a missing value as “false.”

and also check the result for the below program:

data A;
input x @@;
if not x then Expr="True "; 
     else Expr="False";
datalines;
1 0 .
;

proc print noobs; 
run;

temp

Ah-ha! SAS interprets a missing value as “true.”  

More correctly, here is an excerpt from the SAS documentation:

SAS evaluates the expression in an IF-THEN statement to produce a result that is either non-zero, zero, or missing.
A non-zero and nonmissing result causes the expression to be true; a result of zero or missing causes the expression to be false.

If you do not want missing values to be treated as “false,” then do not reference a variable directly, but instead use a Boolean expression in the IF-THEN statement.

For example, in the following statement a missing value results in the THEN statement being executed, whereas all other numerical values continue to behave as expected.

 

Tagged: , ,

Leave a comment