Overview (IF command)
IF
conditionally
executes a single transformation command based upon logical conditions
found in the data. The transformation can create a new variable or
modify the values of an existing variable for each case in the active
dataset. You can create or modify the values of both numeric and string
variables. If you create a new string variable, you must first declare
it on the STRING
command.
IF
has three
components: a logical expression that sets up the logical criteria, a target
variable (the one to be modified or created), and an assignment expression. The target variable’s
values are modified according to the assignment expression.
IF
is most efficient
when used to execute a single, conditional, COMPUTE
-like transformation. If you need multiple IF
statements to define the condition, it
is usually more efficient to use the RECODE
command or a DO IF—END IF
structure.
Basic Specification
The basic specification is a logical expression followed by a target variable, a required equals sign, and the assignment expression. The assignment is executed only if the logical expression is true.
Syntax Rules
- Logical expressions
can be simple logical variables or relations, or complex logical tests
involving variables, constants, functions, relational operators, and
logical operators. Both the logical expression and the assignment
expression can use any of the numeric or string functions allowed
in
COMPUTE
transformations. - Parentheses can be used to enclose the logical expression. Parentheses can also be used within the logical expression to specify the order of operations. Extra blanks or parentheses can be used to make the expression easier to read.
- A relation can compare variables, constants, or more complicated
arithmetic expressions. Relations cannot be abbreviated. For example,
(A EQ 2 OR A EQ 5)
is valid, while(A EQ 2 OR 5)
is not. Blanks (not commas) must be used to separate relational operators from the expressions being compared. - A relation cannot compare a
string variable to a numeric value or variable, or vice versa. A relation
cannot compare the result of the logical functions
SYSMIS
,MISSING
,ANY
, orRANGE
to a number. - String values used in expressions must be specified in quotes and must include any leading or trailing blanks. Lowercase letters are considered distinct from uppercase letters.
- String variables that
are used as target variables must already exist. To declare a new
string variable, first create the variable with the
STRING
command and then specify the new variable as the target variable onIF
.