= SPSS Loop = The '''`LOOP`''' command loops over a logical construct. See [[SPSS/Looping|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 [[SPSS/Set#MxLoops|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 [[SPSS/ScratchVariables|scratch variable]] or use the [[SPSS/Leave|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 [[SPSS/ValueLabels|VALUE LABELS]], [[SPSS/MissingValues|MISSING VALUES]], [[SPSS/Weight|WEIGHT]], [[SPSS/Host|HOST]], and [[SPSS/Insert|INSERT]]. ---- == Data Model == The `LOOP` command is queued as a pending transformation. ---- == See also == [[https://www.gnu.org/software/pspp/manual/html_node/LOOP.html|PSPP manual for LOOP]] ---- CategoryRicottone