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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

834 LOOP—END LOOP<br />

Example<br />

* Factorial routine.<br />

DATA LIST FREE / X.<br />

BEGIN DATA<br />

1 2 3 4 5 6 7<br />

END DATA.<br />

COMPUTE FACTOR=1.<br />

LOOP #I=1 TO X.<br />

COMPUTE FACTOR=FACTOR * #I.<br />

END LOOP.<br />

LIST.<br />

• The loop structure computes FACTOR as the factorial value of X.<br />

Example<br />

* Example of nested loops: compute every possible combination of values<br />

for each variable.<br />

INPUT PROGRAM.<br />

-LOOP #I=1 TO 4. /* LOOP TO NUMBER OF VALUES FOR I<br />

+ LOOP #J=1 TO 3. /* LOOP TO NUMBER OF VALUES FOR J<br />

@ LOOP #K=1 TO 4. /* LOOP TO NUMBER OF VALUES FOR K<br />

@ COMPUTE I=#I.<br />

@ COMPUTE J=#J.<br />

@ COMPUTE K=#K.<br />

@ END CASE.<br />

@ END LOOP.<br />

+ END LOOP.<br />

-END LOOP.<br />

END FILE.<br />

END INPUT PROGRAM.<br />

LIST.<br />

• The first loop iterates four times. The first iteration sets the indexing variable #I equal to<br />

1 and then passes control to the second loop. #I remains 1 until the second loop has completed<br />

all of its iterations.<br />

• The second loop is executed 12 times, three times for each value of #I. The first iteration<br />

sets the indexing variable #J equal to 1 and then passes control to the third loop. #J remains<br />

1 until the third loop has completed all of its iterations.<br />

• The third loop results in 48 iterations (4 × 3 × 4). The first iteration sets #K equal to 1. The<br />

COMPUTE statements set the variables I, J, and K each to 1, and END CASE creates a case.<br />

The third loop iterates a second time, setting #K equal to 2. Variables I, J, and K are then<br />

computed with values 1, 1, 2, respectively, and a second case is created. The third and<br />

fourth iterations of the third loop produce cases with I, J, and K, equal to 1, 1, 3 and 1, 1,<br />

4, respectively. After the fourth iteration within the third loop, control passes back to the<br />

second loop.<br />

• The second loop is executed again. #I remains 1, while #J increases to 2, and control returns<br />

to the third loop. The third loop completes its iterations, resulting in four more cases<br />

with I equal to 1, J to 2, and K increasing from 1 to 4. The second loop is executed a third

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

Saved successfully!

Ooh no, something went wrong!