How do we go about merging files, using analysis software like SPSS? To merge two data files in SPSS (e.g. The combined childhood dataset ncds0123.sav and the 23-year-old dataset ncds4.sav), two approaches are possible. If you are familiar with SPSS syntax, this is the quickest and most elegant way. If not, you can use the drop-down windows menus. Add Cases merges the active dataset with a second dataset or external IBM® SPSS® Statistics data file that contains the same variables (columns) but different cases (rows). For example, you might record the same information for customers in two different sales regions and maintain the data for each region in separate files. This will allow you to match the data from different datasets to the right person. For cross sectional data, this will typically be a single variable, in other cases, two or more variables are needed, this is commonly seen in panel data where subject id and date or wave are often needed to uniquely identify an observation.
This function adds new cases (responses) to an existing SPSS data file; if you want to add new variables for the same cases then click here. Typically you will use this when you receive a new batch of data from a tracking study.
Both files being merged must be .sav (SPSS) files. A new .sav file containing the merged data will be produced, which you can import back into an existing project.
To Merge Data files and add new cases in Q go to Tools > Merge Data Files > Add New Cases
Whenever Q finds differences between variables in the Current file and the Donor file it must choose which to prefer. For example, if Q1 is a text variable in the current file and a numeric in the donor then it must choose which type of variable to include in the merged file. You make this choice after you select the file to merge with.
Typically you should choose to resolve conflicts by preferring the Donor file (the new data). This will ensure that your Q project evolves to match any changes to the questions and options in the ongoing study.
There are some exceptions to this policy of preferring one file over the other:
Where two labels share the same numeric code, the labels from the non-preferred file will be renumbered.
Variables with the same name are automatically placed next to each other, which means they will be merged. You can match other variables by dragging the variables names together. You can also reorder the output file or separate variables by dragging them to new positions between existing variables.
If two variables next to each other differ then they will be colored according to how closely they match:
|Yellow||Variables differ, but can probably be merged safely.|
|Red||Variables differ substantially and can probably not be merged.|
Use the Next and Previous buttons to help you find mismatched variables.
Use the Accept button if you are happy to merge the selected variable(s).
The variables in the output file will take their names and labels from the preferred input file, but you can change them by simply typing new names or labels into those columns.
After finishing you should import the newly merged file into your existing Q project. Q will alert you to any differences and allow you to update your project.
When you have two data files, you may want to combine them by stacking them one on top of the other (referred to as concatenating files). Below we have a file called dads and a file containing moms.
Below we have stacked (concatenated) these files creating a file we called momdad. These examples will show how to concatenate files in SPSS.
The SPSS program below creates an SPSS data file called dads.sav and then creates a file called moms.sav.
Below we see the output of the SPSS commands above showing that the data was read correctly.
We can combine the files using the add filescommand as shown below.
The output of these commands are shown below.
The output from this program shows that the files were combined properly. The dads and moms are stacked together in one file. But, there is a little problem. We can’t tell the dads from the moms. Let’s try doing this again but in such a way that we can tell which observations are the moms and which are the dads.
In order to tell the dads from the moms, let’s ask SPSS to create a variable called dad that will be 1 for observations from dads.sav, and a variable called mom that will be 1 for observations from moms.sav . As you see below, this is accomplished using /in=dad and /in=mom option. We then create a variable called momdad in the dads and moms data files that will contain 'dad' for the dads data file and 'mom' for the moms data file. When we combine the two files together the momdad variable will tell us who the moms and dads are.
The output of these commands is shown below.
Here we get a more desirable result, because we can tell the dads from the moms by looking at the variable momdad. In this example, we could have skipped the step of creating the momdad variable and just referred either to the mom dummy variable or the dad dummy variable. We wanted to illustrate the strategy of creating momdad if you ever combined three or more files, in which case the dummy variables would not be as useful.
You can use the /map subcommand with the add files command to see the order of the variables in the new file, as illustrated below. If you would like to rearrange the order of the variables in the new file, you can also add the /keep subcommand to the add files command. The variables will be ordered in the new file in the order that you list them on the /keep subcommand. If you do not list all of the variables on the /keep subcommand, the variables not listed will not be present in the new file. Also note that you can list the first few variables if they are the only ones that need to be reordered, and then use the keyword all to have the rest of the variables included in the new file. The variables not specified on the /keep subcommand will remain the order in which they are in the original files.
As you can see, the variables in the new file are now in the order name, famid and inc.
These above examples cover situations where there are no complications. However, look out for these complications.
For example, income is called dadinc and in the dads file and called mominc in the moms file, as shown below.
You can see the problem illustrated below.
Solution #1. The most obvious solution is to choose appropriate variable names for the original files (i.e., name the variable inc in both the moms and dads file). This solution is not always possible since you might be concatenating files that you did not originally create. To save space, we omit illustrating this solution.
Solution #2. If solution #1 is not possible, we can fix this problem after we combine the files as shown below. We use the momdad variable to assign dadinc to inc for the dads, and mominc to inc for the moms.
The results are shown below, where inc now has the income for both the moms and dads.
Solution 3. Another way you can fix this is by using the rename subcommand on the add files command to rename the variables just before the files are combined.
You can see the results below. This solution is more elegant than renaming the variables after the fact.
In the dads data set below, famid is a numeric variable. However, in the moms data set, it is a string variable. If we try to concatenate these files, we will get the error message shown below.
Variable(s) with conflicting type:
Result Type Type
FAMID FAMID NUM FAMID S1
Codes: NUM = Numeric, SN = String of length N.
>Error # 5127
>Mismatched variable types on the input files.
>This command not executed.
The solution is to change one of the variables to be the same type as the other. In most cases, you will want to change the string variable to be numeric, as shown below.
In all of the examples above, the variable name was input with the format A4 indicating name is an alphabetic (string) variable with a length of 4. What would happen if name in the dads file was A3 and name in the moms file was A4. This is illustrated below.
When we combine these files, SPSS gives us the error message shown below.
As you can see, SPSS considered this a serious error and did not merge the files.
Solution #1. Define the variables to have the same length in the original files (i.e., use the A4 format for both the moms and dads file). This is the simplest solution if you are creating the files yourself. We will omit illustrating this solution to save space.
Solution #2. You may not have created the original raw data files, so solution #1 may not be possible for you. In that case, you can create a new variable in each file that has the same length and will be compatible when you merge the files. Below we illustrate this strategy.
For the dads file, a new variable called name2 is created with a length of A4 the value of name is copied to name2. Finally, we save the file as dads2 and drop the variable name. The same is done for the moms file. Then, the files dads2 and moms2 can be combined now that name2 is compatible between the two files.
The results are shown below.
Note that if the variable is a numeric variable, then SPSS will still concatenate the file even if the lengths of the two numeric variables is not the same. The length of the variable in the first file listed in the command will be the length used in the concatenated file.
This problem is similar to the problem above, except that it has an additional wrinkle, illustrated below. In the dads file there is a variable called fulltime that is coded 1 if the dad is working full time, 0 if he is not. The moms file also has a variable called fulltime that is coded Y is she is working full time, ad N if she is not. Not only are these variables of different types (character and numeric), but they are coded differently as well.
We forego trying to combine these files since we already know that this will not work (based on the prior example). How can we solve this?
Solution #1. Code the variables in the two files in the same way. For example, code fulltime using 0/1 for both files with 1 indicating working fulltime. This is the simplest solution if you are creating the files yourself. We will omit illustrating this solution to save space.
Solution #2. You may not have created the original raw data files, so solution #1 may not be possible for you. In that case, you can create a new variable in each file that has the same coding and will be compatible when you merge the files. Below we illustrate this strategy.
For the dads file, we make a variable called full that is the same as fulltime, and save the file as dads2, dropping fulltime. For the moms, we create full by recoding fulltime, and save the file as moms2, also dropping fulltime. The files dads2 and moms2 both have the variable full coded the same way (0/1 where 1=works full time) so we can combine those files together.
The results are shown below.
If you run just the add files command, as shown below, SPSS will not do anything. However, you will see a note in the lower right corner of the data editor saying 'transformation pending'.
Solution: The solution is to add either the execute command or a procedure command that will force the execution of the transformation, such as the list command or the crosstab command.
For information on match merging data files, see the SPSS Learning Module on Match Merging SPSS Data Files.