Examples (DO IF command)
Simple, One-Condition Example
DO IF (YearHired LT 87).
RECODE Ethnicity(1=5)(2=4)(4=2)(5=1).
END IF.
- The
RECODE
command recodes Ethnicity for those individuals hired before 1987 (YearHired is less than 87). The Ethnicity variable is not recoded for individuals hired in 1987 or later. - The
RECODE
command is skipped for any case with a missing value for YearHired.
Conditional Execution Based on a Logical Expression
DATA LIST FREE / X(F1).
NUMERIC #QINIT.
DO IF NOT #QINIT.
+ PRINT EJECT.
+ COMPUTE #QINIT = 1.
END IF.
PRINT / X.
BEGIN DATA
1 2 3 4 5
END DATA.
EXECUTE.
- This example shows how to execute a command only once.
- The
NUMERIC
command creates scratch variable #QINIT, which is initialized to 0. - The
NOT
logical operator onDO IF
reverses the outcome of a logical expression. In this example, the logical expression is a numeric variable that takes only 0 (false) or 1 (true) as its values. ThePRINT EJECT
command is executed only once, when the value of scratch variable #QINIT equals 0. After theCOMPUTE
command sets #QINIT to 1, theDO IF
structure is skipped for all subsequent cases. A scratch variable is used because it is initialized to 0 and is not reinitialized after each case.