Examples (LOOP-END LOOP command)

Example

SET MXLOOPS=10.
LOOP. /*Loop with no limit other than MXLOOPS
COMPUTE X=X+1.
END LOOP.
  • This and the following examples assume that an active dataset and all of the variables mentioned in the loop exist.
  • The SET MXLOOPS command limits the number of times the loop is executed to 10. The function of MXLOOPS is to prevent infinite loops when there is no indexing clause.
  • Within the loop structure, each iteration increments X by 1. After 10 iterations, the value of X for all cases is increased by 10, and, as specified on the SET command, the loop is terminated.

Example

*Assume MXLOOPS set to default value of 40.
COMPUTE newvar1=0.
LOOP IF newvar1<100.
COMPUTE newvar1=newvar1+1.
END LOOP.

PRESERVE.
SET MXLOOPS 500.
COMPUTE newvar2=0.
LOOP IF newvar2<100.
COMPUTE newvar2=newvar2+1.
END LOOP.
RESTORE.

COMPUTE newvar3=0.
LOOP #i=1 to 1000.
COMPUTE newvar3=newvar3+1.
END LOOP.
EXECUTE.
  • In the first loop, the value of newvar1 will reach 40, at which point the loop will terminate because the MXLOOPS limit has been exceeded.
  • In the second loop, the value of MXLOOPS is increased to 500, and the loop will continue to iterate until the value of newvar2 reaches 100, at which point the IF condition is reached and the loop terminates.
  • In the third loop, the indexing clause overrides the MXLOOPS setting, and the loop will iterate 1,000 times.