Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

[no subject]



Eva

****************
tempfile auto11 auto12 auto13 auto21 auto22 auto23 res11 res12 res21 res22

forvalues i = 1/2 {
    ** sample data 1
    sysuse auto, clear
    qui keep in 1/6
    gen idt = _n
    qui expand 2
    sort idt rep78
    gen id1 = _n

    drop weight
    qui save `auto`i'1'

    ** sample data 2
    sysuse auto, clear
    qui keep in 1/5
    gen idt = _n
    keep rep78 weight idt
    sort idt
    qui save `auto`i'2'

    ** merge 1 and 2:
    use `auto`i'1'
    * idt uniquely identifies obs in using
    merge idt using `auto`i'2'
    tab _merge
    * save result
    sort idt id1 make
    qui save `res`i'1'

    ** sample data 3
    sysuse auto, clear
    qui keep in 1/5
    keep rep78 weight

    qui expand 3
    sort rep78
    gen id3 = _n
    qui save `auto`i'3'

    ** merge 1 and 3:
    use `auto`i'1'
    sort rep78
    * rep78 does not uniquely identify obs in either dataset
    merge rep78 using `auto`i'3'
    tab _merge
    * save result
    sort id1 id3 make
    qui save `res`i'2'

}

** compare both runs of first merge:
use `res11', clear
cf _all using `res21'
* they are the same.

** compare both runs of second merge:
* they differ.
use `res12', clear
cf _all using `res22'

****************


2008/9/25 Neil Shephard <[email protected]>:
> Your different results may be down to the -merge- you are performing if one
> (or indeed both) of the datasets that you are merging does not have uniquely
> identifiable observations based on the variable you are merging on, so check
> the output after the merge very carefully.
>
>
> Neil
>
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index