Examples (IF command)
IF with Numeric Values
IF (AGE > 20 AND SEX = 1) GROUP=2.
- The numeric variable GROUP is set to 2 for cases where AGE is greater than 20 and SEX is equal to 1.
- When the expression is false or missing, the value of GROUP remains unchanged. If GROUP has not been previously defined, it contains the system-missing value.
IF with String Values
IF (SEX EQ 'F') EEO=QUOTA+GAIN.
- The logical expression tests the string variable SEX for the value F.
- When the expression is true (when SEX equals F), the value of the numeric variable EEO is assigned the value of QUOTA plus GAIN. Both QUOTA and GAIN must be previously defined numeric variables.
- When the expression is false or missing (for example, if SEX equals F), the value of EEO remains unchanged. If EEO has not been previously defined, it contains the system-missing value.
Conditional Expressions with Arithmetic Operations
COMPUTE V3=0.
IF ((V1-V2) LE 7) V3=V1**2.
-
COMPUTE
assigns V3 the value 0. - The logical expression tests whether V1 minus V2 is less than or equal to 7. If it is, the value of V3 is assigned the value of V1 squared. Otherwise, the value of V3 remains at 0.
Conditional Expressions with Arithmetic Operations and Functions
IF (ABS(A-C) LT 100) INT=100.
-
IF
tests whether the absolute value of the variable A minus the variable C is less than 100. If it is, INT is assigned the value 100. Otherwise, the value is unchanged. If INT has not been previously defined, it is system-missing.
Testing for Missing Values
* Test for listwise deletion of missing values.
DATA LIST /V1 TO V6 1-6.
STRING SELECT(A1).
COMPUTE SELECT='V'.
VECTOR V=V1 TO V6.
LOOP #I=1 TO 6.
IF MISSING(V(#I)) SELECT='M'.
END LOOP.
BEGIN DATA
123456
56
1 3456
123456
123456
END DATA.
FREQUENCIES VAR=SELECT.
-
STRING
creates the string variable SELECT with anA1
format andCOMPUTE
sets the value of SELECT to V. -
VECTOR
defines the vector V as the original variables V1 to V6. Variables on a single vector must be all numeric or all string variables. In this example, because the vector V is used as an argument on theMISSING
function ofIF
, the variables must be numeric (MISSING
is not available for string variables). - The loop structure executes six times: once for each
VECTOR
element. If a value is missing for any element,SELECT
is set equal to M. In effect, if any case has a missing value for any of the variables V1 to V6,SELECT
is set to M. -
FREQUENCIES
generates a frequency table forSELECT
. The table gives a count of how many cases have missing values for at least one variable and how many cases have valid values for all variables. This table can be used to determine how many cases would be dropped from an analysis that uses listwise deletion of missing values.
Example
IF YRHIRED LT 1980 RATE=0.02.
IF DEPT='SALES' DIVISION='TRANSFERRED'.
- The logical expression on the first
IF
command tests whether YRHIRED is less than 1980 (hired before 1980). If so, the variable RATE is set to 0.02. - The logical expression on the second
IF
command tests whether DEPT equals SALES. When the condition is true, the value for the string variable DIVISION is changed to TRANSFERRED but is truncated if the format for DIVISION is not at least 11 characters wide. For any other value of DEPT, the value of DIVISION remains unchanged. - Although there are two
IF
statements, each defines a separate and independent condition. TheIF
command is used rather than theDO IF—END IF
structure in order to test both conditions on every case. IfDO IF—END IF
is used, control passes out of the structure as soon as the first logical condition is met.
Example
IF (STATE EQ 'IL' AND CITY EQ 13) COST=1.07 * COST.
- The logical expression tests whether STATE equals IL and CITY equals 13.
- If the logical expression is true, the numeric variable COST is increased by 7%.
- For any other value of STATE or CITY, the value of COST remains unchanged.
Example
STRING GROUP (A18).
IF (HIRED GE 1988) GROUP='Hired after merger'.
-
STRING
declares the string variable GROUP and assigns it a width of 18 characters. - When HIRED is greater than or equal to 1988, GROUP is assigned the value Hired after merger. When HIRED is less than 1988, GROUP remains blank.
Example
IF (RECV GT DUE OR (REVNUES GE EXPNS AND BALNCE GT 0))STATUS='SOLVENT'.
- First, the program tests whether REVNUES is greater than or equal to EXPNS and whether BALNCE is greater than 0.
- Second, the program evaluates if RECV is greater than DUE.
- If either of these expressions is true, STATUS is assigned the value SOLVENT.
- If both expressions are false, STATUS remains unchanged.
- STATUS is an
existing string variable in the active dataset. Otherwise, it would
have to be declared on a preceding
STRING
command.