Differences between revisions 1 and 2
Revision 1 as of 2023-06-12 15:33:27
Size: 1805
Comment:
Revision 2 as of 2023-06-12 15:42:38
Size: 1831
Comment:
Deletions are marked like this. Additions are marked like this.
Line 51: Line 51:
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 and unconditionally. This includes [[SPSS/ValueLabels|VALUE LABELS]], [[SPSS/MissingValues|MISSING VALUES]], [[SPSS/Weight|WEIGHT]], [[SPSS/Host|HOST]], and [[SPSS/Insert|INSERT]]. 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 [[SPSS/ValueLabels|VALUE LABELS]], [[SPSS/MissingValues|MISSING VALUES]], [[SPSS/Weight|WEIGHT]], [[SPSS/Host|HOST]], and [[SPSS/Insert|INSERT]].

SPSS Loop

The LOOP command loops over a logical construct. See here for options and alternatives.


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.

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.


See also

PSPP manual for LOOP


CategoryRicottone

SPSS/Loop (last edited 2023-06-14 19:31:36 by DominicRicottone)