How can I loop through variables in SPSS? I want to avoid code duplication

后端 未结 7 1689
失恋的感觉
失恋的感觉 2021-01-03 02:31

Is there a \"native\" SPSS way to loop through some variable names? All I want to do is take a list of variables (that I define) and run the same procedure for them:

<
7条回答
  •  温柔的废话
    2021-01-03 03:03

    There are two easy solutions for looping through variables (easier compared to using Python in SPSS).

    1) DO REPEAT-END REPEAT

    The draw back is that you can use DO REPEAT-END REPEAT mainly only for data transformations - for example COMPUTE, RECODE etc. Frequencies are not allowed. For example:

    DO REPEAT R=REGION1 TO REGION5.
    COMPUTE R=0.
    END REPEAT.
    

    2) DEFINE-!ENDDEFINE (macro facility)

    You can do Frequencies in a loop of variables using macro command. For example:

    DEFINE macdef (!POS !CHAREND('/'))
    !DO !i !IN (!1)
    frequencies variables = !i.
    !DOEND
    !ENDDEFINE.
    
    macdef VAR1 VAR2 VAR3  /.
    

提交回复
热议问题