Search code examples
spss

How do I use COMPUTE to create and calculate a series of values in SPSS?


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?


Solution

  • 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