Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to multiply two lists of variables

Tags:

loops

list

spss

I have two sets of 8 variables each. I want to multiply the first(second, third etc.) element of each list and sum up the products. So far my attempt is this:

DO REPEAT
  set1 = var1 to var8.
  set2 = varA to VarH.
  compute sum_of_products = SUM(set1 * set2).
END REPEAT.
EXECUTE.

The Error I get is (roughly translated from german):

Error code 4285 in row 38. String: set2

Wrong variable name: either name is longer than 64 chars or he is not defined by a previous command.

Execution of this command has been stopped.

I assume the problem is, that my DO REPEAT does not properly wrap around the commands, but I was not able to manage the right solution with the documentary, as well as with google search. These DO REPEATS and LOOPS really give me a headache.

like image 441
Dekay Avatar asked Dec 03 '25 17:12

Dekay


2 Answers

You are very close. Below is mock example dataset with solution:

* Generate mock data*.
INPUT PROGRAM.
NUMERIC ID S1_1 TO S1_8 S2_1 TO S2_8 (F3.1). 
LOOP ID=1 TO 10.
  DO REPEAT S1=S1_1 TO S1_8 /S2=S2_1 TO S2_8.
    COMPUTE S1=RND(RV.F(3,10)).
    COMPUTE S2=RND(RV.F(3,10)).
  END REPEAT.
END CASE.
END LOOP.
END FILE.
END INPUT PROGRAM.


DO REPEAT S1=S1_1 TO S1_8 /S2=S2_1 TO S2_8 /S=#S_1 TO #S_8.
    COMPUTE S=(S1*S2).
END REPEAT.
COMPUTE PS=SUM(#S_1 TO #S_8).
like image 85
Jignesh Sutar Avatar answered Dec 06 '25 06:12

Jignesh Sutar


See correct syntax for the DO REPEAT command:

compute sum_of_products=0.
DO REPEAT set1 = var1 to var8 /set2 = varA to VarH.
  compute sum_of_products = sum(sum_of_products , (set1 * set2)).
END REPEAT.
EXECUTE.
like image 23
eli-k Avatar answered Dec 06 '25 06:12

eli-k