Search code examples
loopsfor-loopmacrosspss

SPSS for loop based on a variable


I'm just learning SPSS and I want to do simple subgroup analysis based on a variable "status" I created which can take values from 0 to 8. I would like to print outputs in one go.

this is the pseudocode for what I want to do:

for( i = 1, i = 8, i++)
{
filter by (ststus = i)
display analysis
remove filter
}

That way I can do it all in one go but also i can add to the analysis code and do something easily for the 8 subgroups.

I don't know if it's relevant but here is the code I want to iterate over currently:

USE ALL.  
COMPUTE filter_$=(Workforce EQ 1 AND SurveySample = 1 AND State = 1).
VARIABLE LABELS filter_$ 'Workforce EQ 1 (FILTER)'. 
> VALUE LABELS filter_$ 0 'Not Selected' 1 'Selected'.  FORMATS filter_$
> (f1.0).  FILTER BY filter_$.  EXECUTE.
> 
> 
> FREQUENCIES VARIABLES = Q86 Q33 Q34 Q88 FSEScore    /BARCHART FREQ   
> /ORDER=ANALYSIS.
> 
> CROSSTABS    /TABLES=FSEScore BY Q86    /FORMAT=AVALUE TABLES   
> /CELLS=ROW    /COUNT ROUND CELL.
> 
> FILTER OFF. USE ALL.

Thanks guys.


Solution

  • this is probably a very getto way of doing this, the suggestion above is probably more sensible.

    You can initialise Python is spss. The following code works:

    begin program.
    import spss
    
    for i in xrange(1,8):
        string = str(i)
        spss.Submit("""
            USE ALL.
            COMPUTE filter_$=(Workforce EQ 1 AND SurveySample = 1 AND Status = %s). 
            VARIABLE LABELS filter_$ 'Workforce EQ 1 (FILTER)'. 
            VALUE LABELS filter_$ 0 'Not Selected' 1 'Selected'. 
            FORMATS filter_$ (f1.0). 
            FILTER BY filter_$. 
            EXECUTE.
    
        #analysis as required
        FREQUENCIES VARIABLES = Q86 
              /BARCHART FREQ 
              /ORDER=ANALYSIS.    
    
        """%(' '.join(string)) )
    end program.
    

    Many thanks to eli-k I probably should have just used splitfile.