Differences between revisions 3 and 4
Revision 3 as of 2023-06-09 15:44:42
Size: 1160
Comment:
Revision 4 as of 2025-10-24 16:34:25
Size: 915
Comment: Rewrite
Deletions are marked like this. Additions are marked like this.
Line 13: Line 13:
If the left and right datasets may overlap, use the [[Stata/Merge|merge]] command to ensure that duplicates are not created. If the left and right datasets may overlap, use [[Stata/Merge|-merge-]] to ensure that duplicates are not created.
Line 20: Line 20:
If they are known to not overlap, the [[Stata/Append|append]] command can be used instead. If they are known to not overlap, consider using [[Stata/Append|-append-]] instead.
Line 27: Line 27:
See also the [[Stata/FrLink|frlink]] command. See also [[Stata/FrLink|-frlink-]].
Line 33: Line 33:
== Left Join == == Left, Right, or Inner Joins ==
Line 35: Line 35:
Use the `merge` command as above and select cases based on the created `_merge` variable. Use [[Stata/Merge|-merge-]] as above and select cases based on the automatically created `_merge` variable.
Line 40: Line 40:
keep if _merge==1 | _merge==3 keep if _merge==1 | _merge==3 // left join
keep if _merge==2 | _merge==3 // right join
keep if _merge==3 // inner join
Line 42: Line 44:

Alternatively, try using the '''`keep(groups)`''' option.

{{{
use "left.dta"
merge 1:1 KEYVARS using "right.dta", keep(1 3)
}}}

See also the [[Stata/FrLink|frlink]] command.

----



== Right Join ==

As with the left join, but the groups of interest are 2 and 3.

----



== Inner Join ==

As with the left join, but only group 3 is of interest.

Joining Data with Stata

Stata offers several commands for joining datasets.


Full Join

If the left and right datasets may overlap, use -merge- to ensure that duplicates are not created.

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

If they are known to not overlap, consider using -append- instead.

use "cohort1.dta"
append using "cohort2.dta"

See also -frlink-.


Left, Right, or Inner Joins

Use -merge- as above and select cases based on the automatically created _merge variable.

use "left.dta"
merge 1:1 KEYVARS using "right.dta"
keep if _merge==1 | _merge==3       // left join
keep if _merge==2 | _merge==3       // right join
keep if _merge==3                   // inner join


CategoryRicottone

Stata/JoiningData (last edited 2025-10-24 16:34:25 by DominicRicottone)