DO IF
The DO IF-END IF
structure conditionally executes one or more
transformations on subsets of cases based on one or more logical expressions. The
ELSE
command can be used within the structure to execute one or more
transformations when the logical expression on DO IF
is not true. The ELSE
IF
command within the structure provides further control.
DO IF [(]logical expression[)]
transformation commands
[ELSE IF [(]logical expression[)]]
transformation commands
[ELSE IF [(]logical expression[)]]
.
.
.
[ELSE]
transformation commands
END IF
This command does not read the active dataset. It is stored, pending execution with the next command that reads the dataset. See the topic Command Order for more information.
The following relational operators can be used in logical expressions:
Symbol | Definition |
---|---|
EQ or = | Equal to |
NE or ~= or ¬ = or <> | Not equal to |
LT or < | Less than |
LE or <= | Less than or equal to |
GT or > | Greater than |
GE or >= | Greater than or equal to |
The following logical operators can be used in logical expressions:
Symbol | Definition |
---|---|
AND or & | Both relations must be true |
OR or | | Either relation can be true |
NOT | Reverses the outcome of an expression |
Example
DO IF (YearHired GT 87).
COMPUTE Bonus = 0.
ELSE IF (Dept87 EQ 3).
COMPUTE Bonus = .1*Salary87.
ELSE IF (Dept87 EQ 1).
COMPUTE Bonus = .12*Salary87.
ELSE IF (Dept87 EQ 4).
COMPUTE Bonus = .08*Salary87.
ELSE IF (Dept87 EQ 2).
COMPUTE Bonus = .14*Salary87.
END IF.