"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
rdir = 'd:/temp' #Specify empty test folder here.
for filno in [str(num).zfill(3) for num in range(200)]:
data list free/id.
sav out '%(rdir)s/file_%(filno)s.sav'.'''%locals())
*2. Merge all .sav files in root directory.
rdir = 'd:/temp' #Specify folder containing .sav files.
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)]]))
- Note that this syntax uses Python so you need to have the SPSS Python Essentials installed in order to run it.
- The first program block generates 200 small data files for testing.
- The actual solution (second program block) simply generates standard
ADD FILEScommands that merge all .sav files present in the root directory.
- After running the solution, the resulting merged data will be the active dataset in SPSS. From there, you can either save it as a .sav file or first apply some more editing to it.
- It is assumed you don't have any data files or datasets open in SPSS when you run the second program block. If so, you can run
DATASET CLOSE ALL.followed by
- It is assumed that all relevant data files are located in a single folder. If not, see Move all Files from Subfolders to Main Folder or Copy all Files from Subfolders to Main Folder. Alternatively, a minor modification to this solution will solve the problem as well.
Whenever you use the
ADD FILES command, there are three important caveats to keep in mind.
- By default, the merged data that result from "ADD FILES" do not contain the source file for each record. For preventing this, see Add Filenames to Files Before Merging.
- Identically named string variables in different files must have equal lenghts. If this is not the case, the entire command will break down. For preventing the problem, see Adjust String Lengths before Merging Files.
- If two identically named variables in different files have different dictionary information, you may end up with (partly) nonsensical data. Nevertheless, the command will run fine without any errors or warnings. For a demonstration, see SPSS Add Files - Cautionary Note.
- Our SPSS Dictionary Checker provides a handy tool for preventing this from happening.