ELSE Command (DO IF command)
ELSE
executes
one or more transformations when none of the logical expressions on DO IF
or any ELSE
IF
commands is true.
- Only one
ELSE
command is allowed within aDO IF—END IF
structure. -
ELSE
must follow allELSE IF
commands (if any) in the structure. - If the logical expression on
DO IF
or anyELSE IF
command is true, the program ignores the commands followingELSE
.
Example
DO IF (X EQ 0).
COMPUTE Y=1.
ELSE.
COMPUTE Y=2.
END IF.
- Y is set to 1 for all cases with value 0 for X, and Y is 2 for all cases with any other valid value for X.
- The value of Y is not changed by this structure if X is missing.
Example
DO IF (YearHired GT 87).
COMPUTE Bonus = 0.
ELSE.
IF (Dept87 EQ 1) Bonus = .12*Salary87.
IF (Dept87 EQ 2) Bonus = .14*Salary87.
IF (Dept87 EQ 3) Bonus = .1*Salary87.
IF (Dept87 EQ 4) Bonus = .08*Salary87.
END IF.
- If an individual was hired after 1987 (YearHired is greater than 87), Bonus is set to 0 and control passes out of
the structure. Otherwise, control passes to the
IF
commands followingELSE
. - Each
IF
command evaluates every case. The value of Bonus is transformed only when the case meets the criteria specified onIF
. Compare this structure with the second example for the ELSE IF command, which performs the same task more efficiently.
Example
* Test for listwise deletion of missing values.
DATA LIST / V1 TO V6 1-6.
BEGIN DATA
123456
56
1 3456
123456
123456
END DATA.
DO IF NMISS(V1 TO V6)=0.
+ COMPUTE SELECT='V'.
ELSE
+ COMPUTE SELECT='M'.
END IF.
FREQUENCIES VAR=SELECT.
- If there are no missing values for any of the variables V1 to V6,
COMPUTE
sets the value of SELECT equal to V (for valid). Otherwise,COMPUTE
sets the value of SELECT equal to M (for missing). -
FREQUENCIES
generates a frequency table for SELECT. The table gives a count of how many cases have missing values for one or more variables, and how many cases have valid values for all variables. Commands in this example can be used to determine how many cases are dropped from an analysis that uses listwise deletion of missing values.