4

I need to compute 61 new variables using a simple math equation based on 4 sets of 61 existing variables. I know I can write 61 compute statements. Is there a more elegant way of creating these variables? Here's how the 61 statements would look:

COMPUTE score_1 = factor_1 * (a_1 + b_1) + c_1.
...
COMPUTE score_61 = factor_61 * (a_61 + b_61) + c_61.
EXECUTE.

Thanks in advance.

recode accept to and numbers my new variables (recode raw1 to raw61 (1=0) (2=1) into a_1 to a_61.) Can I do the same here?

horace_vr
  • 3,026
  • 6
  • 26
  • 48
janette
  • 43
  • 4
  • If @horace_vr's answer solves your problem please mark it as a solution (and consider upvoting) – eli-k Jul 14 '19 at 06:03

1 Answers1

3

You can use a do repeat structure

DO REPEAT score=score_1 score_2 ... score_61
/factor = factor_1 factor_2 ... factor_61
/a=a_1 a_2 ... a_61
/b=b_1 b_2 ... b_61
/c=c_1 c_2 ... c_61.
COMPUTE score=factor*(a+b)+c.
END REPEAT.
EXECUTE.

In the fortunate event that your variables are in set order (i.e. - all factors are consecutive, all a are consecutive, etc. you may reference them usingto like this:

/factor = factor_1 TO factor_61

otherwise, you need to enumerate them one by one. Hope this helps

horace_vr
  • 3,026
  • 6
  • 26
  • 48
  • Thanks. It works if you add in the missing periods. should read – janette Jul 12 '19 at 19:50
  • DO REPEAT score=score_1 score_2...score_61 /factor = factor_1 factor_2...factor_61 /a=a_1...a_61 /b=b_1...b_61 /c=c_1...c_61 . COMPUTE score=factor*(a+b)+c. END REPEAT. EXECUTE. – janette Jul 12 '19 at 19:51
  • right, edited, thank you! too much python for me lately :) Please accept the answer as correct, if it answers your question – horace_vr Jul 12 '19 at 20:04