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.

LOOP—END LOOP 833<br />

• The program sets the indexing variable to the initial value and increases it by the specified<br />

increment each time the loop is executed for a case. When the indexing variable reaches<br />

the specified terminal value, the loop is terminated for that case.<br />

• By default, the program increases the indexing variable by 1 for each iteration. The keyword<br />

BY overrides this increment.<br />

• The indexing variable can have any valid variable name. Unless you specify a scratch<br />

variable, the indexing variable is treated as a permanent variable and is saved on the working<br />

data file. If the indexing variable is assigned the same name as an existing variable,<br />

the values of the existing variable are altered by the LOOP structure as it is executed, and<br />

the original values are lost.<br />

• The indexing clause overrides the maximum number of loops specified by SET MXLOOPS.<br />

• The initial and terminal values of the indexing clause can be numeric expressions. Noninteger<br />

and negative expressions are allowed.<br />

• If the expression for the initial value is greater than the terminal value, the loop is not executed.<br />

For example, #J=X TO Y is a zero-trip loop if X is 0 and Y is −1.<br />

• If the expressions for the initial and terminal values are equal, the loop is executed once.<br />

#J=0 TO Y is a one-trip loop when Y is 0.<br />

• If the loop is exited via BREAK or a conditional clause on the END LOOP statement, the<br />

iteration variable is not updated. If the LOOP statement contains both an indexing clause<br />

and a conditional clause, the indexing clause is executed first, and the iteration variable is<br />

updated regardless of which clause causes the loop to terminate.<br />

Example<br />

LOOP #I=1 TO 5. /*LOOP FIVE TIMES<br />

COMPUTE X=X+1.<br />

END LOOP.<br />

• The scratch variable #I (the indexing variable) is set to the initial value of 1 and increased<br />

by 1 each time the loop is executed for a case. When #I increases beyond the terminal<br />

value 5, no further loops are executed. Thus, the value of X will be increased by 5 for<br />

every case.<br />

Example<br />

LOOP #I=1 TO 5 IF (Y GT 10). /*Loop to X=5 only if Y GT 10<br />

COMPUTE X=X+1.<br />

END LOOP.<br />

• Both an indexing clause and an IF clause are specified on LOOP. X is increased by 5 for<br />

all cases where Y is greater than 10.<br />

Example<br />

LOOP #I=1 TO Y. /*Loop to the value of Y<br />

COMPUTE X=X+1.<br />

END LOOP.<br />

• The number of iterations for a case depends on the value of the variable Y for that case. For<br />

a case with value 0 for the variable Y, the loop is not executed and X is unchanged. For a<br />

case with value 1 for the variable Y, the loop is executed once and X is increased by 1.

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

Saved successfully!

Ooh no, something went wrong!