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

后端 未结 7 1596
失恋的感觉
失恋的感觉 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:01

    If I understand the question correctly, there may be no need to use a looping construct. SPSS commands with a VARIABLES subcommand like FREQUENCIES allow you to specify multiple variables.

    The basic syntax for the FREQUENCIES is:

    FREQUENCIES
        VARIABLES= varlist [varlist...] 
    

    where [varlist] is a single variable name, multiple space-delimited variable names, a range of consecutive variables specified with the TO keyword, the keyword ALL, or a combination of the previous options.

    For example:

    FREQUENCIES VARIABLES=VARA
    
    FREQUENCIES VARIABLES=VARA VARB VARC
    
    FREQUENCIES VARIABLES=VARA TO VARC     
    
    FREQ VAR=ALL
    
    FREQ VAR=VARA TO VARC VARM VARX TO VARZ
    

    See SPSS Statistics 17.0 Command Syntax Reference available at http://support.spss.com/ProductsExt/SPSS/Documentation/SPSSforWindows/index.htm

    Note that it's been years since I've actually used SPSS.

    0 讨论(0)
  • 2021-01-03 03:01

    Yes, SPSS can do this. Sounds like the guys at UCLA use python 'cause they know how to do it in python and not in SPSS. :)

    Let's call your variables VARA, VARB, VARC. They must be numerical (since you are doing frequencies) and they must be consecutive in your spss data file. Then you create a vector saying in effect "here is the series of variables I want to loop through".

    VECTOR VectorVar = VarA TO VarC.
    LOOP #cnt = 1 to 3 by 1.
        FREQUENCIES VARIABLES=VectorVar(#cnt) / ORDER=ANALYSIS
    ENDLOOP.
    EXECUTE.
    

    (The above has not been tested. Might be missing a period somewhere, etc.)

    0 讨论(0)
  • 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  /.
    
    0 讨论(0)
  • 2021-01-03 03:06

    Here's a page from UCLA's Academic Technology Services that describes looping over lists of variables. Quote,

    "Because we are looping through more than one variable, we will need to use Python."

    In my experience, UCLA ATS is probably the site with the best coverage of all of the major statistical computing systems. If they say you need Python... you probably need Python.

    Er... sorry for being that guy, but maybe it's time to switch to a different stats system.

    0 讨论(0)
  • 2021-01-03 03:09

    I haven't used SPSS macros very much, but maybe they can get you where you need to be? Check out this site for some examples:

    http://spsstools.net/Macros.htm

    Also, the SPSS Data Management book may be helpful as well.

    Lastly, if memory serves, I think the problem may even be the main example of how to leverage Python inside of SPSS syntax. I have only used Python and SPSS a few times, but it is very handy to have that language accessible if need be.

    HTH

    0 讨论(0)
  • 2021-01-03 03:11

    It's more efficient to do all these frequencies on one data pass, e.g., FREQUENCIES a to c. but Python lets you do looping and lots of other control flow tricks.

    begin program.
    import spss
    for v in ['a','b','c']:
      spss.Submit("FREQUENCIES " + v)
    end program.
    

    Using Python requires installing the (free) Python plugin available from SPSS Developer Central, www.spss.com/devcentral.

    You can, of course, use macros for this sort of things, but Python is a lot more powerful and easier once you get the hang of it.

    0 讨论(0)
提交回复
热议问题