Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Please help: Merge 1:1 delivers different results after each runthrough

    I want to merge two datasets based on one variable (neither dataset contains duplicates on this variable and there are no other identical variables in both sets) and I would expect _merge==3 to be 4,845 (I exported my data to excel and checked it there), both datasets are original datasets so the input data must be identical with each run;

    the code is very simple and looks like the following:

    use "$path\facility", clear
    sort id
    drop if id == id[_n-1]
    save "$path\facility", replace

    use "$path\facilitysponsor", clear
    sort id
    drop if id == id[_n-1]
    merge 1:1 id using "$path\facility"
    keep if _merge ==3

    However, each time I run the do-file I get slightly different values but never the correct number of merged data. Anyone has an idea where is my mistake?

    Thank you in advance!

  • #2
    This implies that the sort command affects the results of the merge.

    sort id
    drop if id == id[_n-1]
    To get the same results over different runs, you could set the seed, e.g.,

    Code:
    set seed 2019
    sort id
    drop if id == id[_n-1]
    However, you should first look into what observations you want to keep as this affects your merge results.

    Comment

    Working...
    X