1
votes

I would like to merge different SPSS files to one new file. I plan to use the MATCH command in SPSS for this. See also another question from me on that topic (Combining add cases and add variables by merging files in SPSS).

However, the number of files I want to merge automatically is not always the same. This depends on the data that a customer has been requested. On the other hand, each file will always be used in the same MATCH command.

For example, we have 7 files with data on health. When the customer has requested all data, all 7 files should be merged into one new file. However, when another customer has requested only data from 3 files, only these 3 files should be merged into one new file.

I am wondering if someone knows how I can achieve this. Maybe the files should be placed in a particular folder so that the existing files can be load/ match? The non-existing files should be ignored. Maybe a kind of 'if-then'-statement?

Many thanks in advance!

2

2 Answers

2
votes

You can generate and run a MATCH FILES command for all the files in a folder or all the files matching a wildcard name with a few lines of Python code. Here is an example that merges all the sav files in a particular folder.

begin program.
import spss, glob

cmd = ["MATCH FILES"]

for f in glob.glob(r"c:/temp/*.sav"):
    cmd.append("""/FILE="%s" """ % f)
spss.Submit(cmd)
end program.
1
votes

What I would do is the following. Might not be the most efficient way, but it works fine...

I suppose you have a file with a record for each customer, and 7 variables indicating the need for each of the files, like this:

data list /ID 1-3 (A) FIL1 to FIL7 5-11.
begin data
001 1100101
002 0100110
003 0011111
004 1111111
end data.

I always like the following old-fashioned approach. No macro's and no python!

str matchcommand (a2000).
compu matchcommand eq "match files".
do rep a = FIL1 to FIL7
      /b = "C:\fil1.sav" "C:\fil2.sav" "C:\fil3.sav" "C:\fil4.sav" 
           "C:\fil5.sav" "C:\fil6.sav" "C:\fil7.sav". 
if a matchcommand eq concat(matchcommand, "\n  /file='", b, "'").
end rep.

compu matchcommand eq concat(matchcommand, " /by id.")

The strings in matchcommand can be written to .sps file(s) using the write command. If you add a save command to these pieces of syntax, you can easily call the syntax bits or files by using insert file.

Using Macros or Python is far more flexible than this approach. However, this one I always remember and I can jot it down in no-time. It might be rusty, but it's fast. That's why I like it so much.