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: Merging datasets


From   Meredith Anderson <anderson.meredith.k@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Merging datasets
Date   Mon, 10 Oct 2011 11:33:33 -0700

I would use joinby, and then drop the records that you don't need.

use treatment.dta
joinby patientid using bloodresults.dta, unmatched(both)

("unmatched(both)" keeps unmatched patient IDs from both datasets. The
default - "unmatched(none)" - is to drop observations connected with a
patient ID that is only in one dataset. The unmatched option will
accept "both", "master", "using" or "none")

To get rid of blood tests after the treatment date:
drop if treatmentdate<testdate & _merge==3

To keep the most recent test before the treatment date:
bysort patientid treatmentdate testdate: assert _N==1
bysort patientid treatmentdate (testdate): drop if _n<_N & _merge==3

If I'm understanding what you need correctly, I think this will do it.

On Sun, Oct 9, 2011 at 1:30 PM, Oybek Rustamov
<oybek_rustamov@yahoo.co.uk> wrote:
>
> Dear All
>
> Could you help me with merging my two datasets. In my datasets some patients had more than two treatment cycles and number of blood tests. I  need to merge blood results that is closest to treatment date and before the treatment date.
>
> This is example of my datasets.
>
> 1) Treatment dataset
>
> Patient ID    Treatment date
>
> A.                 01/01/09
> A.                 05/07/10
>
> B.                  10/10/10
> B.                  12/10/11
>
>
> 2) Blood results dataset
>
> Patient ID       Test date
>
> A.                  12/12/2008
> A.                  10/09/2008
> A.                   01/01/2011
>
>
> B.               10/09/2009
> B.                03/09/2010
> B.                04/05/2011
>
> Many thanks
>
> Oybek
>
>
>
> Sent from my iPhone
> *
> *   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/

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index