No history yet

Merging MEPS Datasets

Combining MEPS Datasets

MEPS data is powerful because it's so detailed. But that detail is spread across many different files. You have person-level files with demographics, event-level files with information on every doctor visit, and condition-level files for specific health issues. To connect a person's demographic data with their medical appointments, you need to combine, or merge, these separate datasets.

In Stata, the primary tool for this is the merge command. It takes two datasets—a 'master' dataset already loaded into memory and a 'using' dataset saved on your disk—and combines them based on one or more shared identifying variables.

Types of Merges

Not all merges are the same. The relationship between your two datasets determines the type of merge you'll perform. Stata needs you to specify this relationship to ensure the data is combined correctly.

One-to-one (1:1): This is the simplest type. Each unique identifier in the master dataset corresponds to exactly one unique identifier in the using dataset. For example, merging two different person-level files from the same year, where each person appears only once in both files.

One-to-many (1:m): Here, a single record in the master dataset can link to multiple records in the using dataset. This is very common with MEPS data. You might merge a person-level file (one record per person) with a medical events file (many records per person, one for each event).

Many-to-one (m:1): This is the reverse of a one-to-many merge. Multiple records in your master dataset link to a single record in the using dataset.

There's also a many-to-many (m:m) merge, but it's generally a sign that your identifying variables aren't unique enough. It's best to avoid this unless you have a specific, advanced reason for it.

The Merge Process in Action

Let's walk through a typical one-to-many merge, combining a person-level file with a prescribed medicines file.

First, you need to identify the unique identifier that links the two files. In MEPS, the person-level identifier is almost always DUPERSID. You might also need to use the panel number, PANEL, if you're working with data across multiple years.

Next, both the master and the using datasets must be sorted by the key identifier(s) before you can merge them. Let's assume you have a person-level file called persons.dta and a medicine file called medicines.dta.

// First, prepare the 'using' dataset
use "medicines.dta", clear
sort DUPERSID
save, replace

// Now, open the 'master' dataset and merge
use "persons.dta", clear
sort DUPERSID

merge 1:m DUPERSID using "medicines.dta"

Verifying the Merge

How do you know if the merge worked as expected? Stata creates a special variable called _merge to help you.

This variable tells you the source of each observation in the newly combined dataset. After the merge, you should always check the values of _merge to diagnose any issues.

_merge ValueMeaning
1Observation comes from the master dataset only.
2Observation comes from the using dataset only.
3Observation is a successful match, with data from both.

In most cases, you'll want the vast majority of your observations to have _merge == 3. This indicates a successful match.

If you have many observations with _merge == 1, it means many records in your master file didn't find a match in the using file. If you have records with _merge == 2, it means some records from the using file didn't match anything in the master file. This can sometimes point to data entry errors or an incorrect key variable.

You can quickly see a summary by running tab _merge in Stata.

// After the merge command, check the results
tab _merge

Once you're satisfied with the merge, you can drop the _merge variable and proceed with your analysis. Properly merging files is a foundational skill for working with complex datasets like MEPS.