I am trying to update some missing values in a dataset with values from another.
Here is an example in Stata 14.2:
sysuse auto, clear
// save in order to merge below
save auto, replace
// create some missing to update
replace length = . if length < 175
// just so the two datasets are not exactly the same, which is my real example
drop if _n == _N
merge 1:1 make using auto, nogen keep(master match_update) update
The code above only keeps the observations updated (26 observations). It is exactly the same result if one uses keep(match_update) instead.
Why is Stata not keeping all observations in the master dataset?
Note that not using match_update is not helpful either, as it removes all observations.
My current workaround is to rename original variables, merge all, and then replace if original was missing. However, this defeats the point of using the update option, and it is cumbersome for updating many variables.
keep(master match_update)in the last line and it will work. - user8682794mergeto only keep the updated observations in the dataset that match the other and drop everything else. - user8682794masterinside the keep option? - luchonachokeep(master match match_update)does what you want. Personally I always prefer to manually drop / keep observations using_mergeas it is more transparent and less error prone. - user8682794