DO REPEAT is a command for running other commands repetitively. SPSS DO REPEAT is often used for looping over (possibly new) variables. VECTOR with LOOP is an alternative way for doing so.
Introduction
- Suppose we have a variable containing values 1 through 4 and we'd like to dichotomize it for a regression analysis. (For some more background, see Creating Dummy Variables in SPSS.)
- A very basic dichotomization is to run four COMPUTE commands. These are illustrated by the second block in the syntax below. (The first block creates a tiny test dataset for demonstration purposes.)
- For some more on the structure of these COMPUTE commands, see Compute A = B = C.
SPSS Syntax Example
*1. Create mini test data.
data list free/original.
begin data
1 2 3 4
end data.
*2. Basic dichotimization.
compute d1 = original = 1.
compute d2 = original = 2.
compute d3 = original = 3.
compute d4 = original = 4.
exe.
*3. After visual inspection, delete new variables.
delete variables d1 to d4.
data list free/original.
begin data
1 2 3 4
end data.
*2. Basic dichotimization.
compute d1 = original = 1.
compute d2 = original = 2.
compute d3 = original = 3.
compute d4 = original = 4.
exe.
*3. After visual inspection, delete new variables.
delete variables d1 to d4.
- Now note that the four COMPUTE commands are identical except for the variables (d1 through d4) and the numbers (1 through 4).
- This allows us to replace these by placeholders. We'll call those x and y but basically any name will do. We can now loop over a single command four times.
- On the first run, we replace the placeholders (x and y) by (d1 and 1).
- On the second run, placeholders (x and y) will be replaced by (d2 and 2).
- Like so, we'll walk through the list of variables and the list of numbers. These two lists are processed in parallel.
- A little know fact is that END REPEAT PRINT. will show the generated syntax in the output window.
- These points are illustrated by the syntax below. It uses the test data from the first syntax example.
SPSS Do Repeat - Syntax Example 1
*Dichotomize variable with do repeat.
do repeat x = d1 d2 d3 d4 / y = 1 2 3 4.
compute x = original = y.
end repeat print.
exe.
do repeat x = d1 d2 d3 d4 / y = 1 2 3 4.
compute x = original = y.
end repeat print.
exe.
Reconstructing the Original Variable
- Note that the dichotomization process can be easily reversed as well by using DO REPEAT.
- The syntax below demonstrates how to do this. It uses the variables created in the previous example.
SPSS Do Repeat - Syntax Example 2
*Reconstruct original variable from dichotomous variables.
do repeat x = d1 to d4 / y = 1 to 4.
if x = 1 reconstructed = y.
end repeat.
exe.
do repeat x = d1 to d4 / y = 1 to 4.
if x = 1 reconstructed = y.
end repeat.
exe.
SPSS Do Repeat - Basic Rules
- As we've seen, a placeholder may refer to a list of integers, new variables, existing variables or a mix of those. However, each list must have the same number of items. This is because the lists are processed in parallel.
- Only transformations can be used within DO REPEAT.
- The syntax below builds on the previous examples. It demonstrates violations of the aforementioned rules.
SPSS Do Repeat - Incorrect Syntax Examples
*1. Violation 1: lists contain different numbers of items.
do repeat a = v1 to v4 / b = 1 to 3.
compute a = b.
end repeat.
exe.
*2. Violation 2: non transformation command in do repeat.
do repeat c = d_1 to d_4.
variable labels c "Dummy variable".
end repeat.
exe.
do repeat a = v1 to v4 / b = 1 to 3.
compute a = b.
end repeat.
exe.
*2. Violation 2: non transformation command in do repeat.
do repeat c = d_1 to d_4.
variable labels c "Dummy variable".
end repeat.
exe.
SPSS Do Repeat - Final Notes
- This tutorial demonstrated how dichotomizing variables can be done using DO REPEAT. A limitation here is that value labels or variable labels have to be applied manually to the dummy variables. For having these applied automatically, use the Create Dummy Variables tool.
- Virtually everything can be done with DO REPEAT can also be done by combining VECTOR with LOOP. The latter is often faster and more versatile but slightly more difficult to learn too.
- You can't use DO REPEAT within DO REPEAT. Instead, use LOOP for such nested loops.
- The transformations you loop over in DO REPEAT do not have to use the placeholder. The example given below replaces all double/triple/quadruple (and so on up to 8-fold) spaces in a string by single spaces. A cleaner way for doing this was shown in SPSS LOOP Command. It does not build on the previous examples.
SPSS Do Repeat - Syntax Example 4
*1. Create mini test dataset.
data list free/sentence(a45).
begin data
'a b c d e f g h i'
end data.
*2. Replace double/triple (and so on) spaces by single ones.
do repeat x = 1 to 3.
compute sentence = replace(sentence,' ',' ').
end repeat.
exe.
data list free/sentence(a45).
begin data
'a b c d e f g h i'
end data.
*2. Replace double/triple (and so on) spaces by single ones.
do repeat x = 1 to 3.
compute sentence = replace(sentence,' ',' ').
end repeat.
exe.
THIS TUTORIAL HAS 8 COMMENTS:
By Ruben Geert van den Berg on May 20th, 2018
Hi Veve, thanks for the compliment!
Exactly what would you like me to do with Python? You rarely need it for SPSS tables because most tables naturally take multiple variables.
Thanks!
By Paul on April 10th, 2019
Hi
Can you use do rep and ten var together to rename a large set of variables.
By Ruben Geert van den Berg on April 11th, 2019
No. You can't run RENAME VARIABLES in DO REPEAT because it's not a transformation command. However, you can do something like
RENAME VARIABLES (ALL = v1 TO v128).
for any dataset holding 128 variables -and so on for other numbers of variables.