# SPSS Statistics

View Only

## what can I use the loop index variable for?

• #### 1.  what can I use the loop index variable for?

Posted Mon November 08, 2021 01:10 PM
Hi everyone!

I was trying to shorten my Syntax a little by using loops. However, when I tried, SPSS gave me an error warning.

This is the code that works:
compute v1_agg = 0.
if (any(1, v1_a to v1z)) v1_agg = 1.
if (any(2, v1_a to v1z)) v1_agg = 2.
if (any(3, v1_a to v1z)) v1_agg = 3.
if (any(4, v1_a to v1z)) v1_agg = 4.
recode v1_agg (0=sysmis).
EXECUTE.
delete variables v1_a to v1_z.
EXECUTE.​
This is the code with the loop, that didn't work:
compute v1_agg = 0.
loop #i 1 to 4.
if (any(#i, v1_a to v1_z)) v1_agg = #i.
end loop.
recode v1_agg (0=sysmis).
EXECUTE.
delete variables v1_a to v1_z.
EXECUTE.​

To me it seemed pretty straight forward to substitute the digits in my code with the index variable of a loop, but it didn't work. Instead I got this error message:
(translating this from German into English so it might be slightly different)

error 4840 in column 6. text: #i
the command LOOP can include two optional clauses - one index clause in the form "variable = expression TO expression BY expression" and one conditional clause in the form of an if clause. The text found does not match either of the two forms.

I assume you can't use the index variable as a placeholder for integers like that, but I am a little confused as to what is possible and what not or if I just made a different mistake altogether.

I appreciate any help or tip :)

best,
Christoph

------------------------------
Christoph Kulicke
------------------------------

• #### 2.  RE: what can I use the loop index variable for?

Posted Mon November 08, 2021 01:20 PM
LOOP #i=1 to 4.​

You forgot the equals sign.

------------------------------
Rick Marcantonio
Quality Assurance
IBM
------------------------------

• #### 3.  RE: what can I use the loop index variable for?

Posted Thu November 11, 2021 03:35 AM
Oh boy, sometimes it is really that simple huh?

Thanks for the help and sorry to bother with such a dumb mistake ^^'

------------------------------
Christoph Kulicke
------------------------------

• #### 4.  RE: what can I use the loop index variable for?

Posted Thu November 11, 2021 07:32 AM
Not a problem.

------------------------------
Rick Marcantonio
Quality Assurance
IBM
------------------------------