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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With