Syntax Rules (DO IF command)

  • The ELSE IF command is optional and can be repeated as many times as needed.
  • The ELSE command is optional. It can be used only once and must follow any ELSE IF commands.
  • The END IF command must follow any ELSE IF and ELSE commands.
  • A logical expression must be specified on the DO IF and ELSE IF commands. Logical expressions are not used on the ELSE and END IF commands.
  • String values used in expressions must be specified in quotation marks and must include any leading or trailing blanks. Lowercase letters are distinguished from uppercase letters.
  • To create a new string variable within a DO IF—END IF structure, you must first declare the variable on the STRING command.
  • DO IF—END IF structures can be nested to any level permitted by available memory. They can be nested within LOOP—END LOOP structures, and loop structures can be nested within DO IF structures.

Example

DATA LIST FREE /var1.
BEGIN DATA
1 2 3 4 5
END DATA.
DO IF (var1 > 2) & (var1 < 5).
- COMPUTE var2=1.
ELSE IF (var1=2).
- COMPUTE var2=2.
ELSE.
- COMPUTE var2=3.
END IF.
Table 1. DO IF results
var1 var2
1 3
2 2
3 1
4 1
5 3

Example

INPUT PROGRAM.
+ STRING odd (A3).
+ LOOP numvar=1 TO 5.
+  DO IF MOD(numvar, 2)=0.
+   COMPUTE odd='No'.
+  ELSE.
+   COMPUTE odd='Yes'.
+  END IF.
+  END CASE.
+ END LOOP.
+ END FILE.
END INPUT PROGRAM.
Table 2. DO IF results for input program
numvar odd
1 Yes
2 No
3 Yes
4 No
5 Yes