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, 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 <>
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
tempfile a b

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

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

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

use `a', clear
merge 1:1 patient_id using `b'
*------------------- end example -------------------
(For more on examples I sent to the Statalist see: )

-- Maarten

Maarten L. Buis
Reichpietschufer 50
10785 Berlin

*   For searches and help try:

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