SPSS Tutorials

BASICS REGRESSION T-TEST CHI-SQUARE TEST ANOVA

Export Output to Different Folders

Question

"In my data, I have different teams nested within different areas. Right now I have five cities and in each city teams A through E. Some output for each combination of area and team separately should be exported into its own folder. Is there any flexible way to accomplish this?"

SPSS Python Syntax Example

*1. Create test data.

begin program.
import spss,spssaux
teams = ['A','B','C','D','E']
areas = ['Amsterdam','Zaandam','Utrecht','Den Haag','Enkhuizen']
cases = '\n'.join(['""'.join(['"' + area,team + '"']) for case in range(10) for area in areas for team in teams])
spss.Submit('data list free/area(a9)team(a6).\nbegin data\n%s\nend data.\
\ncompute number_of_children=tru(rv.uni(0,3)).\nexe.'%(cases))
end program.

*2. Create output folders, generate output and export it into correct output folder for separate groups.

begin program.
rdir = 'd:/temp/' # Specify root directory for output folders.
splitvars = ['area','team'] # Specify split variables.
import spssaux
vals_0 = set(spss.Cursor([spssaux.VariableDict().VariableIndex(splitvars[0])])\
.fetchall())
vals_1 = set(spss.Cursor([spssaux.VariableDict().VariableIndex(splitvars[1])])\
.fetchall())
for val_0 in[val[0].rstrip() for val in vals_0]:
    for val_1 in [val[0].rstrip() for val in vals_1]:
        ndir = os.path.join(rdir,val_0 + '_' + val_1)
        os.mkdir(ndir)
        spss.Submit('''
OMS\n/SELECT ALL\n/EXCEPTIF SUBTYPES = ['NOTES']
/DESTINATION FORMAT = HTML OUTFILE ='%s/Output for area = %s, team = %s.htm'
VIEWER=no
/tag='freq'.
temp.
select if %s='%s' and %s='%s'.
fre number_of_children.
omsend tag=['freq'].
'''%(ndir,val_0,val_1,splitvars[0],val_0,splitvars[1],val_1))
end program.

Description

Previous tutorial: Copy all Files from Subfolders to Main Folder

Next tutorial: Rename or Prefix Many Files

Let me know what you think!

*Required field. Your comment will show up after approval from a moderator.

This tutorial has 1 comment!