IBM Support

DO IF, ELSE with COMPUTE leaving some variable values missing



I am attempting to execute syntax similar to: DO IF (var1 =1 and var2 = 2). COMPUTE newvar = 1. ELSE. COMPUTE newvar = 0. END IF. Some cases still have missing values after I execute this syntax. What is wrong?

Resolving The Problem

Nothing is wrong. SPSS is evaluating the cases as it should.

Why is this happening? SPSS evaluates case criteria as "true" or "false." In the above example, SPSS looks at a case and asks "VAR1=1 and VAR2=2: true or false?" If VAR1=1 and VAR2=2, then this statement is "true" and SPSS performs the subsequent COMPUTE command. If it is not the case where VAR1=1 and VAR=2, then SPSS returns a value of "false" and goes to the ELSE command. If either VAR1 or VAR2 is system-missing, then SPSS cannot determine if this selection criteria is either true or false, so SPSS returns a system-missing value for the NEWVAR variable.

Even the order of commands can affect the outcome of the target variable. Take this example:

DO IF (var1=1).
COMPUTE new_var=1.
ELSE IF (var1 = 1 and var2=2).
COMPUTE new_var = 2.
ELSE IF (var1 = 2).
COMPUTE new_var = 3.
COMPUTE new_var=4.

Suppose that for our first case VAR1=1 and VAR2 is system-missing. SPSS will process the DO IF command for this case and say "VAR1=1: true" and go to the COMPUTE command so NEW_VAR=1. The case then falls out of the DO IF structure and SPSS goes to the next case. But in this example:

DO IF (var1=1 and var2=2).
COMPUTE new_var=2.
ELSE IF (var1 = 1).
COMPUTE new_var = 1.
ELSE IF (var1 = 2).
COMPUTE new_var = 3.
COMPUTE new_var=4.

SPSS looks at the first case where VAR1=1 and VAR2 is system-missing. SPSS then decides "VAR1=1 and VAR2=2: I can't determine." The case is neither true or false, so the case falls out of the DO IF structure without a NEWVAR value being assigned

NOTE: this was meant as an illustration of how a DO IF-ELSE IF structure handles missing values. There's an easier solution to the original problem. If you need newvar to be set equal to 1 when you know var1 = 1 (i.e., var1 not missing) and var2 = 2 (i.e., var2 not missing) and you want newvar set to 0 in all other cases, then the simplest way is to first initialize newvar to 0, and then flip it to 1 only when var = 1 and var2 = 2:

COMPUTE newvar = 0.

IF (var1 = 1 AND var2 = 2) newvar = 1.


This way if either var1 or var2 is missing, then no action is taken by the IF command, and newvar remains 0.

[{"Product":{"code":"SSLVMB","label":"IBM SPSS Statistics"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Component":"Not Applicable","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"Not Applicable","Edition":"","Line of Business":{"code":"LOB10","label":"Data and AI"}}]

Historical Number


Document Information

Modified date:
16 April 2020