ELSE IF Command (DO IF command)

ELSE IF executes one or more transformations when the logical expression on DO IF is not true.

  • Multiple ELSE IF commands are allowed within the DO IF—END IF structure.
  • If the logical expression on DO IF is true, the program executes the commands immediately following DO IF up to the first ELSE IF. Then control passes to the command following the END IF command.
  • If the result of the logical expression on DO IF is false, control passes to ELSE IF.

Example

STRING Stock(A9).
DO IF (ITEM EQ 0).
COMPUTE Stock='New'.
ELSE IF (ITEM LE 9).
COMPUTE Stock='Old'.
ELSE.
COMPUTE Stock='Cancelled'.
END IF.
  • STRING declares string variable Stock and assigns it a width of nine characters.
  • The first COMPUTE is executed for cases with value 0 for ITEM, and then control passes out of the structure. Such cases are not reevaluated by ELSE IF, even though 0 is less than 9.
  • When the logical expression on DO IF is false, control passes to the ELSE IF command, where the second COMPUTE is executed only for cases with ITEM less than or equal to 9. Then control passes out of the structure.
  • If the logical expressions on both the DO IF and ELSE IF commands are false, control passes to ELSE, where the third COMPUTE is executed.
  • The DO IF—END IF structure sets Stock equal to New when ITEM equals 0, to Old when ITEM is less than or equal to 9 but not equal to 0 (including negative numbers if they are valid), and to Cancelled for all valid values of ITEM greater than 9. The value of Stock remains blank if ITEM is missing.

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.
  • For cases hired after 1987, Bonus is set to 0 and control passes out of the structure. For a case that was hired before 1987 with value 3 for Dept87, Bonus equals 10% of salary. Control then passes out of the structure. The other three ELSE IF commands are not evaluated for that case. This differs from the second example for the ELSE command, where the IF command is evaluated for every case. The DO IF—ELSE IF structure shown here is more efficient.
  • If Department 3 is the largest, Department 1 the next largest, and so forth, control passes out of the structure quickly for many cases. For a large number of cases or a command file that will be executed frequently, these efficiency considerations can be important.