# SPSS Tutorials

BASICS REGRESSION T-TEST ANOVA CORRELATION

# 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

• This syntax uses Python so you need to have the SPSS Python Essentials installed for using it.
• The first program block will generate some test data for the actual solution.
• The test data contain two string variables, "team" and "area". Each combination of a team within an area defines a group.
• In the second block, a root directory is specified. This is where the output folders for each group will be created. Their names will contain the team as well as the group so it's easy to see which output it will contain.
• The output for each group will be generated and automatically exported to the correct output folder.
• This solution would need a lot of modifying in order to run it on other data than the test data provided. It's included mainly to illustrate one of several possible approaches to this problem.

# Let me know what you think!

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

# This tutorial has 3 comments

• ### By Ruben Geert van den Berg on March 24th, 2018

Hi Lisa, I don't think there's any really simple solution for that. Perhaps try something like

``` begin program. import spss,datetime basename = "d:/some-output" timestamp = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S') spss.Submit('output save outfile="%s-%s.spv".'%(basename,timestamp)) end program. ```

HTH!

• ### By Lisa on March 23rd, 2018

I am trying to see how I can use Python to automatically add today's date to output files. I am able to do it for .sav files, but not .spv files. This is the syntax I use for .sav files:

BEGIN PROGRAM.
import spss, spssaux, spssaux2
spssaux.SaveDataFile(spssaux2.CreateFileNameWDate(basename="CPOSS results.sav"))
END PROGRAM.

• ### By Alison on March 5th, 2015

I've tried running the code above to create different output files (changing the name of the root directory of course) and cannot get it to create any ouput folders or files.