SPSS Statistics

SPSS Statistics

Your hub for statistical analysis, data management, and data documentation. Connect, learn, and share with your peers! 

 View Only
Expand all | Collapse all

Analysis of Survey data: How to calculate the same statistical method for numerous variables

  • 1.  Analysis of Survey data: How to calculate the same statistical method for numerous variables

    Posted Mon February 07, 2022 09:27 AM

    Hello,

    We, the survey team, are producing results for >200 variables (e.g. A1 to A20, B1A to B1Z, Z1_new to Z10_new, etc.) using the same statistical analysis (CSTABULATE) and OMS to produce >200 output files.  In the example below, we analyze the C8 variable.

    OMS
    /SELECT TABLES
    /IF COMMANDS = ['CSTABULATE']
    INSTANCES = [LAST]
    /EXCEPTIF SUBTYPE = ['NOTES']
    /DESTINATION FORMAT = XLSX
    OUTFILE = "...\C8_cstab_output.xlsx"
    /NOWARN.

    MISSING VALUES C8 (66 thru 99).

    CSTABULATE
    /PLAN FILE='...\weighing2.csaplan'
    /TABLES VARIABLES=C8
    /CELLS TABLEPCT
    /STATISTICS SE CV CIN(95) COUNT
    /MISSING SCOPE=TABLE CLASSMISSING=EXCLUDE.
    EXECUTE.

    OMSEND.

    Currently, we are tediously replacing one variable after another variable and click run to get an output file.  We are wondering how can we write extra lines in syntax (such as loop, repeat, etc.) that will replace another variable in the place of C8 and run all variables at a run and produce >200 output files, instead of running one by one?

    Thank you very much in advance.



    ------------------------------
    Thomas
    ------------------------------

    #SPSSStatistics


  • 2.  RE: Analysis of Survey data: How to calculate the same statistical method for numerous variables

    Posted Mon February 07, 2022 10:32 AM
    I'm in a meeting so don't have much time at the moment but I point you to the MACRO facility, which can do it (essentially taking your entire syntax in the MACRO and substituting a variable name in a loop passed in by a list). Alternatively, a Python script could be written to do exactly the same thing.

    ------------------------------
    Rick Marcantonio
    Quality Assurance
    IBM
    ------------------------------



  • 3.  RE: Analysis of Survey data: How to calculate the same statistical method for numerous variables

    Posted Mon February 07, 2022 12:07 PM
    OK, here is an example I think might work:
    output close all.
    dataset close all.
    
    GET FILE="your file here".
    
    preserve.
    set printback none.
    
    ** Be sure to change the file location for the following text file.
    
    oms select logs /destination format=text outfile="/Users/rick/tmp/list.txt".
    do if $casenum=1.
    DO REPEAT existVar=v1 v3 to v5 v7 v8 to v10.
    print / existvar.
    END REPEAT.
    end if.
    execute.
    omsend.
    restore.
    
    begin program python3.
    import spss
    
    ** Now read that same file back into Python, making a list out of it.
    
    my_file = open("/Users/rick/tmp/list.txt", "r")
    myvariables = my_file.read()
    
    varlist=[]
    varcount=spss.GetVariableCount()
    for i in range(varcount):
      v=spss.GetVariableName(i)
      if v in myvariables:
        print("""
    OMS
     /SELECT TABLES
     /IF COMMANDS=['CSTABULATE'] INSTANCES=[LAST]
     /EXCEPTIF SUBTYPE=['NOTES']
     /DESTINATION FORMAT=XLSX OUTFILE="...\%s_cstab_output.xlsx"
     /NOWARN.
    
    MISSING VALUES %s (66 thru 99).
    
    CSTABULATE
     /PLAN FILE='...\weighing2.csaplan'
     /TABLES VARIABLES=%s
     /CELLS TABLEPCT
     /STATISTICS SE CV CIN(95) COUNT
     /MISSING SCOPE=TABLE CLASSMISSING=EXCLUDE.
    EXECUTE.
    
    OMSEND.
    """%(v,v,v))
    
    end program.​

    Substitute your variable list where I have << >> in "DO REPEAT existVar=<<v1 v3 to v5 v7 v8 to v10>>."

    The Python script can be easily changed to submit these commands but first try a sample and print it to make sure it is doing what you want.

    Let me know if any problems.



    ------------------------------
    Rick Marcantonio
    Quality Assurance
    IBM
    ------------------------------