Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: how do you merge a large dataset and small dataset together based on a unique id?


From   Maarten Buis <maartenlbuis@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: how do you merge a large dataset and small dataset together based on a unique id?
Date   Tue, 23 Apr 2013 14:02:59 +0200

On Tue, Apr 23, 2013 at 1:29 PM, Gwinyai Masukume wrote:
> I have two datasets and each has a unique id – the patient id.
> One dataset (the larger) contains patient demographic data and the
> other dataset (the smaller) contains the results of a laboratory test.
> (Not all patients had the laboratory test done).
>
> I would like to merge the two datasets.
>
> I have tried a one to one merge based on the patient id. The two
> datasets do merge, however a patient is paired with the wrong
> laboratory result (i.e. a result which is not the patient’s, but
> belonging to another patient).

Here is an example of how -merge- works. How does what you did deviate
from what the example below does? What are the patient id in both
files and do they match? What is the exact code you typed in order to
do the merge? How exactly did you determine that the merge went bad?

*------------------ begin example ------------------
//============================= create files
set seed 12345
clear
tempfile a b

// first file (demographics)
set obs 10
gen patient_id = _n
gen byte female = runiform() < .5
save `a'
list

// second file (tests)
drop _all
set obs 5
gen patient_id = _n
gen test_result = rnormal()
save `b'
list

//=============================== merge files

use `a', clear
merge 1:1 patient_id using `b'
list
*------------------- end example -------------------
(For more on examples I sent to the Statalist see:
http://www.maartenbuis.nl/example_faq )

-- Maarten

---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany

http://www.maartenbuis.nl
---------------------------------

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index