Command reference
merge — join datasets by identifying variables
Combine the dataset in memory with another file by matching one or more key variables. Merge can add information such as school characteristics to student records while recording which observations matched.
Syntax
merge 1:1 keyvars using "filename.csv" [, options]
merge m:1 keyvars using "filename.csv" [, options]
merge 1:m keyvars using "filename.csv" [, options]
The dataset in memory is the master; the file after using is the using dataset. Both must contain keyvars. The relation describes whether each key can appear once or many times on each side. By default, unmatched observations from both datasets are retained.
Matching relations and options
Example
The sample joins student scores to school information by student ID. It tabulates the match indicator and lists the IDs that occur in only one file.
clear
import delimited "data/student_scores.csv"
merge 1:1 id using "data/student_schools.csv"
tabulate _merge
list id math school _merge if _merge != 3
Sample data included. Opens in a new tab; click Run.
What to look for
There are 22 matched students, two master-only students (IDs 23 and 24), and one using-only student (ID 25). The unmatched students remain in the result: school information is missing for IDs 23 and 24, and test scores are missing for ID 25. These are merge outcomes to inspect, not automatically records to delete.