27.03.2013 Views

SPSS® 12.0 Command Syntax Reference

SPSS® 12.0 Command Syntax Reference

SPSS® 12.0 Command Syntax Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

496 DO IF<br />

Example<br />

DATA LIST FREE / X(F1).<br />

NUMERIC #QINIT.<br />

DO IF NOT #QINIT.<br />

+ PRINT EJECT.<br />

+ COMPUTE #QINIT = 1.<br />

END IF.<br />

PRINT / X.<br />

BEGIN DATA<br />

1 2 3 4 5<br />

END DATA.<br />

EXECUTE.<br />

• This example shows how to execute a command only once.<br />

• The NUMERIC command creates scratch variable #QINIT, which is initialized to 0.<br />

• The NOT logical operator on DO IF reverses the outcome of a logical expression. In this<br />

example, the logical expression is a numeric variable that takes only 0 (false) or 1 (true)<br />

as its values. The PRINT EJECT command is executed only once, when the value of<br />

scratch variable #QINIT equals 0. After the COMPUTE command sets #QINIT to 1, the DO<br />

IF structure is skipped for all subsequent cases. A scratch variable is used because it is<br />

initialized to 0 and is not reinitialized after each case.<br />

ELSE <strong>Command</strong><br />

ELSE executes one or more transformations when none of the logical expressions on DO IF<br />

or any ELSE IF commands is true.<br />

• Only one ELSE command is allowed within a DO IF—END IF structure.<br />

• ELSE must follow all ELSE IF commands (if any) in the structure.<br />

• If the logical expression on DO IF or any ELSE IF command is true, the program ignores<br />

the commands following ELSE.<br />

Example<br />

DO IF (X EQ 0).<br />

COMPUTE Y=1.<br />

ELSE.<br />

COMPUTE Y=2.<br />

END IF.<br />

• Y is set to 1 for all cases with value 0 for X, and Y is 2 for all cases with any other valid<br />

value for X.<br />

• The value of Y is not changed by this structure if X is missing.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!