Size: 2530
Comment:
|
← Revision 8 as of 2023-06-12 15:43:48 ⇥
Size: 1971
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 3: | Line 3: |
SPSS offers a couple options for looping, but overall is less flexible as compared to other languages. | SPSS offers a couple options for '''looping''', but overall is less flexible as compared to other languages. |
Line 11: | Line 11: |
== Do Repeat == | == Arbitrary Lists == |
Line 13: | Line 13: |
The '''`DO REPEAT ... END REPEAT`''' command loops over... * arbitrary sequences * variable ranges (either existing or new) * integer ranges This command is ''only valid for data transformation commands'' (i.e. `COMPUTE`). Multiple sequences/ranges can be iterated over in parallel as long as each has the same number of elements. |
The [[SPSS/DoRepeat|DO REPEAT]] command can loop over arbitrary, fixed-length lists. |
Line 24: | Line 16: |
do repeat X=A B C D E / Y=new1 to new5 / Z=1 to 5. if (original=Z) Y=ltrim(rtrim(X)). |
do repeat X='A' 'B' 'C' 'D' 'E'. if ltrim(rtrim(foo))=X bar=1. |
Line 28: | Line 20: |
To print the evaluated loop syntax, use `END REPEAT PRINT`. Because only transformation commands are valid inside `DO REPEAT`, it isn't possible to do nested looping with it. |
|
Line 37: | Line 25: |
== Loop == | == Integer Ranges == |
Line 39: | Line 27: |
The '''`LOOP ... END LOOP`''' command can arbitrarily loop. | The [[SPSS/DoRepeat|DO REPEAT]] command can loop over an integer range. {{{ do repeat Y=1 to 5. if foo=X bar=1. end repeat. }}} The [[SPSS/Loop|LOOP]] command can be used similarly. {{{ loop #y=1 to 5 by 1. if foo=#y bar=1. end loop. }}} ---- |
Line 43: | Line 47: |
=== Indefinite Looping === | == Variable Lists == |
Line 45: | Line 49: |
The `LOOP` command on its own will loop indefinitely (but actually until a maximum depth is reached). A warning will be emitted. | The [[SPSS/DoRepeat|DO REPEAT]] command can loop over variables. If the variables are not pre-existing, a range is extrapolated from the variable names. {{{ do repeat Z=new1 to new5. compute Z=foo. end repeat. }}} A similar effect can be acheived by using the [[SPSS/Loop|LOOP]] command with a [[SPSS/Vectors|vector]]. {{{ vector new(4). loop #z = 1 to 4. compute new(#z) = (foo=#z). end loop. }}} ---- == Conditional Looping == The [[SPSS/Loop|LOOP]] command can loop a variable number of times, based on either a 'while' or a 'break' condition. Specify the `IF` option on the `LOOP` command for the former behaviuor, or on the `END LOOP` command for the latter. |
Line 49: | Line 76: |
compute sentence = replace(sentence,' ',' '). | compute foo = replace(foo,' ',' '). end loop if char.index(foo,' ') = 0. loop if char.index(foo,' ') > 0. compute foo = replace(foo,' ',' '). |
Line 53: | Line 84: |
---- | |
Line 55: | Line 87: |
=== While Loop === | |
Line 57: | Line 88: |
There are two options for a conditional while loop. | == Indefinite Looping == |
Line 59: | Line 90: |
First, use the `if` subcommand on `END LOOP`. Note that this will still run once, as the condition won't be checked until then. | The [[SPSS/Loop|LOOP]] command can also be made loop indefinitely, if no `IF` option is specified. |
Line 63: | Line 94: |
compute sentence = replace(sentence,' ',' '). end loop if char.index(sentence,' ') = 0. }}} Second, use the `if` subcommand on `LOOP`. {{{ loop if char.index(sentence,' ') > 0. compute sentence = replace(sentence,' ',' '). end loop. }}} === Integer Range === An explicit number of repetitions can be specified. {{{ loop X=1 to 3. compute sentence = replace(sentence,' ',' '). end loop. }}} The variable `X` is created by the `LOOP` command and it will persist in the dataset. In the above example it will have a final value of 4. To avoid persistence, consider using a [[SPSS/ScratchVariables|scratch variable]] (i.e. `#i`). The full specification is `LOOP <VARIABLE> = <START> TO <END> BY <STEP>`. === Variable Range === The integer range method can be combined with [[SPSS/Vectors|vectors]] to achieve looping over variables. {{{ vector new(4). loop #i = 1 to 4. compute new(#i) = (original=#i). |
compute foo = replace(foo,' ',' '). |
SPSS Looping
SPSS offers a couple options for looping, but overall is less flexible as compared to other languages.
Contents
Arbitrary Lists
The DO REPEAT command can loop over arbitrary, fixed-length lists.
do repeat X='A' 'B' 'C' 'D' 'E'. if ltrim(rtrim(foo))=X bar=1. end repeat.
Integer Ranges
The DO REPEAT command can loop over an integer range.
do repeat Y=1 to 5. if foo=X bar=1. end repeat.
The LOOP command can be used similarly.
loop #y=1 to 5 by 1. if foo=#y bar=1. end loop.
Variable Lists
The DO REPEAT command can loop over variables. If the variables are not pre-existing, a range is extrapolated from the variable names.
do repeat Z=new1 to new5. compute Z=foo. end repeat.
A similar effect can be acheived by using the LOOP command with a vector.
vector new(4). loop #z = 1 to 4. compute new(#z) = (foo=#z). end loop.
Conditional Looping
The LOOP command can loop a variable number of times, based on either a 'while' or a 'break' condition. Specify the IF option on the LOOP command for the former behaviuor, or on the END LOOP command for the latter.
loop. compute foo = replace(foo,' ',' '). end loop if char.index(foo,' ') = 0. loop if char.index(foo,' ') > 0. compute foo = replace(foo,' ',' '). end loop.
Indefinite Looping
The LOOP command can also be made loop indefinitely, if no IF option is specified.
loop. compute foo = replace(foo,' ',' '). end loop.
Macros
Advanced looping can be handled through macro programming. See here for examples.