SPSS Loop
The LOOP command loops over a logical construct. See here for options and alternatives.
Contents
Usage
To loop until a 'break' condition is met, try:
loop. compute foo = replace(foo,' ',' '). end loop if char.index(foo,' ') = 0.
To loop for as long as a 'while' condition is met, try:
loop if char.index(foo,' ') > 0. compute foo = replace(foo,' ',' '). end loop.
By excluding any IF option at all, LOOP can be made to loop indefinitely.
The LOOP command can also be made to loop over a numeric range, with an optional step.
loop #x = 1 to 4. compute foo = #x. end loop. loop #y = 1 to 4 by 1. compute bar = #y. end loop.
The dummy variable is reinitialized every time a case is read. When using nested LOOP commands, this is generally not a desired behavior. To avoid this, specify a scratch variable or use the LEAVE command.
Break
Aside from using a 'break' condition, the BREAK command can be used to end the innermost loop.
Limitations
Any command requiring that no transformations be pending cannot be used in a DO IF structure. This includes most statistics and graphic commands.
Any command that executes immediately and does not read the active dataset (or does not need an active dataset at all) will be executed immediately, unconditionally, and repeatedly in each loop. This includes VALUE LABELS, MISSING VALUES, WEIGHT, HOST, and INSERT.
Data Model
The LOOP command is queued as a pending transformation.