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.

498 DO IF<br />

• If the logical expression on DO IF is true, the program executes the commands immediately<br />

following DO IF up to the first ELSE IF. Then control passes to the command<br />

following the END IF command.<br />

• If the result of the logical expression on DO IF is false, control passes to ELSE IF.<br />

Example<br />

STRING STOCK(A9).<br />

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

COMPUTE STOCK=’New’.<br />

ELSE IF (ITEM LE 9).<br />

COMPUTE STOCK=’Old’.<br />

ELSE.<br />

COMPUTE STOCK=’Cancelled’.<br />

END IF.<br />

• STRING declares string variable STOCK and assigns it a width of nine characters.<br />

• The first COMPUTE is executed for cases with value 0 for ITEM, and then control passes<br />

out of the structure. Such cases are not reevaluated by ELSE IF, even though 0 is less than 9.<br />

• When the logical expression on DO IF is false, control passes to the ELSE IF command,<br />

where the second COMPUTE is executed only for cases with ITEM less than or equal to 9.<br />

Then control passes out of the structure.<br />

• If the logical expressions on both the DO IF and ELSE IF commands are false, control passes<br />

to ELSE, where the third COMPUTE is executed.<br />

• The DO IF—END IF structure sets STOCK equal to New when ITEM equals 0, to Old when<br />

ITEM is less than or equal to 9 but not equal to 0 (including negative numbers if they are<br />

valid), and to Cancelled for all valid values of ITEM greater than 9. The value of STOCK<br />

remains blank if ITEM is missing.<br />

Example<br />

DO IF (YRHIRED GT 87).<br />

COMPUTE BONUS = 0.<br />

ELSE IF (DEPT87 EQ 3).<br />

COMPUTE BONUS = .1*SALARY87.<br />

ELSE IF (DEPT87 EQ 1).<br />

COMPUTE BONUS = .12*SALARY87.<br />

ELSE IF (DEPT87 EQ 4).<br />

COMPUTE BONUS = .08*SALARY87.<br />

ELSE IF (DEPT87 EQ 2).<br />

COMPUTE BONUS = .14*SALARY87.<br />

END IF.<br />

• For cases hired after 1987, BONUS is set to 0 and control passes out of the structure. For<br />

a case that was hired before 1987 with value 3 for DEPT87, BONUS equals 10% of salary.<br />

Control then passes out of the structure. The other three ELSE IF commands are not evaluated<br />

for that case. This differs from the example on p. 497, where the IF command is<br />

evaluated for every case. The DO IF—ELSE IF structure shown here is more efficient.<br />

• If Department 3 is the largest, Department 1 the next largest, and so forth, control passes<br />

out of the structure quickly for many cases. For a large number of cases or a command file<br />

that will be executed frequently, these efficiency considerations can be important.

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

Saved successfully!

Ooh no, something went wrong!