Joining Data with Stata

Firstly the Stata commands are listed with an explanation of syntax. Secondly the equivalents of standard SQL joins are listed.


Merge

To join two data files using a unique key variable, try:

use "left.dta"
merge 1:1 keyvarlist using "right.dta"

keyvarlist can be one or more variables, but all cases must be uniquely identified by the combination of values.

The merge command creates a _merge variable taking the values:

Value

Meaning

1

observation appeared in 'master' only

2

observation appeared in 'using' only

3

observation appeared in both

4

observation appeared in both, the update option was specified, and missing values were updated

5

observation appeared in both, the update option was specified, and non-missing values were updated

Many-to-one

To merge a file to a lookup table, try:

use "left.dta"
merge m:1 key using "right.dta"

Note: the inverse operation can be accomplished with 1:m.


Append

To add cases from a file into the active dataset, try:

append using "second.dta"


Joins

Full Join

Left Join

Right Join

Inner Join


CategoryRicottone