Overview (LOOP-END LOOP command)
The LOOP-END LOOP
structure performs repeated transformations specified by the commands
within the loop until they reach a specified cutoff. The cutoff can
be specified by an indexing clause on the LOOP
command, an IF
clause on the END LOOP
command,
or a BREAK
command within the
loop structure (see BREAK
). In
addition, the maximum number of iterations within a loop can be specified
on the MXLOOPS
subcommand on SET
. The default MXLOOPS
is 40.
The IF
clause
on the LOOP
command can be used
to perform repeated transformations on a subset of cases. The effect
is similar to nesting the LOOP-END LOOP
structure within a DO IF-END IF
structure, but using IF
on LOOP
is simpler and more efficient. You
have to use the DO IF-END IF
structure,
however, if you want to perform different transformations on different
subsets of cases. You can also use IF
on LOOP
to specify the cutoff,
especially when the cutoff may be reached before the first iteration.
LOOP
and END LOOP
are usually used within an input
program or with the VECTOR
command.
Since the loop structure repeats transformations on a single case
or on a single input record containing information on multiple cases,
it allows you to read complex data files or to generate data for a
active dataset. For more information, see INPUT PROGRAM-END INPUT PROGRAM
and VECTOR
.
The loop structure repeats transformations on single
cases across variables. It is different from the DO REPEAT-END REPEAT
structure, which replicates transformations
on a specified set of variables. When both can be used to accomplish
a task, such as selectively transforming data for some cases on some
variables, LOOP
and END LOOP
are generally more efficient and
more flexible, but DO REPEAT
allows
selection of nonadjacent variables and use of replacement values with
different intervals.
Options
Missing Values. You can prevent cases with missing values for any of the variables used in the loop structure from entering the loop. See the topic Missing Values (LOOP-END LOOP command) for more information.
Creating Data. A loop structure within an input program can be used to generate data. See the topic Creating Data (LOOP-END LOOP command) for more information.
Defining Complex File Structures. A loop structure within an input program can be used to define complex files that cannot be handled by standard file definition facilities.
Basic Specification
The basic
specification is LOOP
followed
by at least one transformation command. The structure must end with
the END LOOP
command. Commands
within the loop are executed until the cutoff is reached.
Syntax Rules
- If
LOOP
andEND LOOP
are specified before an active dataset exists, they must be specified within an input program. - If both an indexing
and an
IF
clause are used onLOOP
, the indexing clause must be first. - Loop structures can be nested within other loop
structures or within
DO IF
structures, and vice versa.
Operations
- The
LOOP
command defines the beginning of a loop structure and theEND LOOP
command defines its end. TheLOOP
command returns control toLOOP
unless the cutoff has been reached. When the cutoff has been reached, control passes to the command immediately followingEND LOOP
. - Commands
that take effect immediately without reading the data or executing
pending commands (e.g.,
VALUE LABELS
,MISSING VALUES
,VARIABLE LABEL
), commands that don't require an active dataset (e.g.,DISPLAY
,HOST
,INSERT
,OMS
,SET
) and theGET
command are processed unconditionally and immediately each time they are encountered in a loop structure. The specified condition is ignored for these commands. - Commands that
require a data pass, such as statistical or charting procedures, cannot
be included in a loop structure and will cause the
LOOP
command to fail. - An indexing clause (e.g.,
LOOP #i=1 to 1000
) will override theSET MXLOOPS
limit, but a loop with anIF
condition will terminate if theMXLOOPS
limit is reached before the condition is satisfied.