Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: Merging datasets


From   Meredith Anderson <[email protected]>
To   [email protected]
Subject   Re: st: Merging datasets
Date   Wed, 12 Oct 2011 14:49:58 -0700

I think this should do it, as long as your data are unique by patient
id, treatment date and test date:

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): drop if _n<_N & _merge==3

This was in the second half of my previous email - I hope it answers
your question. Thanks,

Meredith

On Tue, Oct 11, 2011 at 3:03 AM, Oybek Rustamov
<[email protected]> wrote:
>
> Dear Meridith
>
> Many thanks for your help.
>
> More more question- how can I keep closest blood result if patient had more than one blood test before the treatment date?
>
> Regards
>
> Oybek
>
> Sent from my iPhone
>
> On 10 Oct 2011, at 19:33, Meredith Anderson <[email protected]> wrote:
>
> > 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
> > <[email protected]> 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/
>
> *
> *   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index