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!