Differences between revisions 2 and 8 (spanning 6 versions)
Revision 2 as of 2021-10-01 15:06:09
Size: 917
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:
One method of looping that is '''only valid for data transformation commands''' (i.e. `COMPUTE`) is the `DO REPEAT ... END REPEAT` command.

As demonstrated below, `DO REPEAT` can iterate over...
 * arbitrary sequences
 * variable ranges (either existing or new)
 * integer ranges

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 23: 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 21:
To print the evaluated loop syntax, use `END REPEAT PRINT`. ----



== Integer Ranges ==

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.
}}}

----



== Variable Lists ==

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.

{{{
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 [[SPSS/Loop|LOOP]] command can also be made loop indefinitely, if no `IF` option is specified.

{{{
loop.
  compute foo = replace(foo,' ',' ').
end loop.
}}}
Line 36: Line 104:
Advanced looping can be handled through macro programming. See [[SPSS/Macros#Loops]] for examples. Advanced looping can be handled through macro programming. See [[SPSS/Macro/Looping|here]] for examples.

SPSS Looping

SPSS offers a couple options for looping, but overall is less flexible as compared to other languages.


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.


CategoryRicottone

SPSS/Looping (last edited 2023-06-12 15:43:48 by DominicRicottone)