Search code examples
loopsvariablesspss

Use loop to create interactions in SPSS


The below loop fails to correctly create any interaction terms (i.e. new variables that are multiplications of eachother). I am not exactly sure how to correctly specify x(#j + #i), so maybe this is what is messing things up.

DATA LIST LIST /        A1L1    A1L2    A1L3    P1  P2  P3  P4  P5  P6  P7  P8  P9  P10.
BEGIN DATA                                                      
1   0   0   1   0   0   0   0   0   0   0   0   0       
1   0   0   0   1   0   0   0   0   0   0   0   0       
0   1   0   0   0   1   0   0   0   0   0   0   0       
0   1   0   0   0   0   1   0   0   0   0   0   0       
0   0   1   0   0   0   0   1   0   0   0   0   0       
0   0   1   0   0   0   0   0   1   0   0   0   0       
-1  -1  -1  0   0   0   0   0   0   1   0   0   0       
-1  -1  -1  0   0   0   0   0   0   0   1   0   0       
-1  -1  -1  0   0   0   0   0   0   0   0   0   1       
END DATA.                                                       
LIST.                                                       

vector A1L1P    A1L2P   A1L3P  (10).
vector x =  A1L1P1 to A1L3P10.
VECTOR ASC = P1 to P10.
VECTOR EcLvl = A1L1 to A1L3.
LOOP #j = 1 to 3.
    LOOP #i = 1 to 10.
        COMPUTE x(#j + #i) = (ASC(#i)  *  EcLvl(#j)).
    END LOOP.
END LOOP.
EXECUTE.

Solution

  • Instead of

        COMPUTE x(#j + #i) = (ASC(#i)  *  EcLvl(#j)).
    

    I think you want

        COMPUTE x(10*(#j-1) + #i) = (ASC(#i)  *  EcLvl(#j)).