pythonspss-files

How to write SPSS syntaxes using Python for looping purpose


I have a list of Baseline and Post variables that I want to run descriptive statistics and ANCOVA.

Baseline variables = [Brief2_Inhibit_T_SELF_BL, Brief2_Completion_T_SELF_BL, Brief2_Shift_T_SELF_BL]
Post variables = [Brief2_Inhibit_T_SELF_PO, Brief2_Completion_T_SELF_PO, Brief2_Shift_T_SELF_PO]
Treatment_Group is on variable with two labels [1 - Intervention, 0- Control]

Below is my SPSS syntax for each pair of variable.

EXAMINE VARIABLES=Brief2_Inhibit_T_SELF_BL Brief2_Inhibit_T_SELF_PO BY Treatment_Group
  /PLOT NONE
  /STATISTICS DESCRIPTIVES
  /CINTERVAL 95
  /MISSING LISTWISE
  /NOTOTAL.

UNIANOVA Brief2_Inhibit_T_SELF_PO BY Treatment_Group WITH Brief2_Inhibit_T_SELF_BL
  /METHOD=SSTYPE(3)
  /INTERCEPT=INCLUDE
  /PRINT ETASQ DESCRIPTIVE HOMOGENEITY
  /CRITERIA=ALPHA(.05)
  /DESIGN=Brief2_Inhibit_T_SELF_BL Treatment_Group.

I found a helpful guide from UCLA to loop through two lists of variables to run regression.

begin program.
import spss, spssaux
spssaux.OpenDataFile('d:\data\elemapi2.sav')
vdict=spssaux.VariableDict()
dlist=vdict.range(start="api00", end="ell")
ilist=vdict.range(start="grad_sch", end="enroll")
ddim = len(dlist)
idim = len(ilist)

if ddim != idim: 
     print "The two sequences of variables don't have the same length."
else: 
        for i in range(ddim): 
             mydvar = dlist[i]
             myivar = ilist[i]

             spss.Submit(r"""
                    regression /dependent %s
                    /method = enter %s.
                                """ %(mydvar, myivar))
end program.

How can I edit the above list to run my SPSS syntaxes?


Solution

  • Here's a way to do this with a simple SPSS macro:

    This will be the macro definition:

    define !doAnalysis (!pos=!cmdend)
    !do !vr !in (!1)
    !let !BL=!concat(!vr,"_BL")
    !let !PO=!concat(!vr,"_PO")
    EXAMINE VARIABLES=!BL !PO BY Treatment_Group
      /PLOT NONE
      /STATISTICS DESCRIPTIVES
      /CINTERVAL 95
      /MISSING LISTWISE
      /NOTOTAL.
    UNIANOVA !PO BY Treatment_Group WITH !BL
      /METHOD=SSTYPE(3)
      /INTERCEPT=INCLUDE
      /PRINT ETASQ DESCRIPTIVE HOMOGENEITY
      /CRITERIA=ALPHA(.05)
      /DESIGN=!BL Treatment_Group.
    !doend
    !enddefine.
    

    The macro is now built to take a list of items, loop through them one by one, creating two names from each item in the list - by adding "BL" or "PO", and using these names to run your analyses.
    This will be the macro call:

    !doAnalysis Brief2_Inhibit_T_SELF  Brief2_Completion_T_SELF  Brief2_Shift_T_SELF .