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 anyELSE IF
commands. - The
END IF
command must follow anyELSE IF
andELSE
commands. - A logical expression must be specified on the
DO IF
andELSE IF
commands. Logical expressions are not used on theELSE
andEND 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 theSTRING
command. -
DO IF—END IF
structures can be nested to any level permitted by available memory. They can be nested withinLOOP—END LOOP
structures, and loop structures can be nested withinDO 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.
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.
numvar | odd |
---|---|
1 | Yes |
2 | No |
3 | Yes |
4 | No |
5 | Yes |