Missing Values in DO IF Structures
Missing values can affect the results from DO IF
structures
because if the expression evaluates to missing, then control passes
immediately to the END IF
command at that point.
To avoid this type of problem, you should attempt to deal with missing
values first in the DO IF
structure before evaluating
any other conditions.
*create sample data with missing data.
DATA LIST FREE (",") /a.
BEGIN DATA
1, , 1 , ,
END DATA.
COMPUTE b=a.
* The following does NOT work since the second condition is never evaluated.
DO IF a=1.
- COMPUTE a1=1.
ELSE IF MISSING(a).
- COMPUTE a1=2.
END IF.
* On the other hand the following works.
DO IF MISSING(b).
- COMPUTE b1=2.
ELSE IF b=1.
- COMPUTE b1=1.
END IF.
EXECUTE.
- The first
DO IF
will never yield a value of 2 for a1, because if a is missing, thenDO IF a=1
evaluates as missing, and control passes immediately toEND IF.
So a1 will either be 1 or missing. - In the second
DO IF
, however, we take care of the missing condition first; so if the value of b is missing,DO IF MISSING(b)
evaluates as true and b1 is set to 2; otherwise, b1 is set to 1.
In this example, DO IF MISSING(b)
will always
evaluate as either true or false, never as missing,
thereby eliminating the situation in which the first condition might
evaluate as missing and pass control to END IF
without
evaluating the other condition(s).
a | b | a1 | b1 |
---|---|---|---|
1 | 1 | 1 | 1 |
missing | missing | missing | 2 |
1 | 1 | 1 | 1 |
missing | missing | missing | 2 |