SPSS Tutorials

BASICS REGRESSION T-TEST ANOVA CORRELATION

Merge Many Data Files

Question

"I have 200 .sav files that I'd like to merge by using ADD FILES. Since ADD FILES merges a maximum of 50 data files, I need to run it four times. I know how to write the syntax but inserting all file names is a lot of tedious copying-pasting-editing. Is there a more elegant way?"

SPSS Python Syntax Example

*1. Create 200 small data files for testing.

begin program.
rdir = 'd:/temp' #Specify empty test folder here.
import spss,os
for filno in [str(num).zfill(3) for num in range(200)]:
    spss.Submit('''
data list free/id.
begin data
%(filno)s
end data.
sav out '%(rdir)s/file_%(filno)s.sav'.'''%locals())
spss.Submit('new fil.')
end program.

*2. Merge all .sav files in root directory.

begin program.
rdir = 'd:/temp' #Specify folder containing .sav files.
import spss,os
fils = sorted([fil for fil in os.listdir(rdir) if fil.endswith('.sav')])
spss.Submit('get file "%s/%s".'%(rdir,fils.pop(0)))
for rep in range(len(fils)/49 + 1):
    spss.Submit('add files file=*/%s.'%'/'.join(['file="%s"'%os.path.join(rdir,fil) for fil in fils[49*rep:49*rep + min(49,len(fils)-49*rep)]]))
spss.Submit('exe.')
end program.

Description

Assumptions

Warnings

Whenever you use the ADD FILES command, there are three important caveats to keep in mind.

Previous tutorial: Adjust String Lengths before Merging Files

Next tutorial: Apply Dictionary Information from Excel

Let me know what you think!

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

This tutorial has 11 comments