Statalist


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

st: Re: drop conditioning on 2 different variables


From   "Martin Weiss" <martin.weiss1@gmx.de>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: Re: drop conditioning on 2 different variables
Date   Fri, 1 May 2009 23:24:12 +0200

<>

I think you can get there in one line, everything else is just to make it look nicer...

**********
clear*

input  id             cd4c str15 cd4cdate
1 325 01Mar06
1 352 01Mar06
1             500          03Aug06
2 167 20Mar06
2 302 20Mar06
2            900          12Dec06
3 118 20Oct05
3 178 20Oct05
3              450          01May06
end

compress

gen date=date(cd4cdate, "DMY", 2020)
format date %tdMonth_DD,_CCYY

//achieve the drop
bys id cd4cdate (cd4c): drop if _n==_N & _N>1

sort id date
list, noobs sepby(id)
**********


HTH
Martin
_______________________
----- Original Message ----- From: "Ann Miller" <MILLERA@hsph.harvard.edu>
To: <statalist@hsphsun2.harvard.edu>
Sent: Friday, May 01, 2009 10:34 PM
Subject: st: drop conditioning on 2 different variables


Dear Statalist,

I am trying to clean some data, in which I have 2 different and contradictory lab results on the same date. Example:

id             cd4c cd4cdate
1 325 01 Mar 06
1 352 01 Mar 06
1              500          03 Aug 06
2 167 20 Mar 06
2 302 20 Mar 06
2              900          12 Dec 06
3 118 20 Oct 05
3 178 20 Oct 05
3              450          01 May 06

I want to drop the row with the highest cd4c date when there's a date match. This is proving to be surprisingly hard to do. I tried
sort id cd4c cd4cdate
bysort imbd_id cd4cdate: gen min= cd4c[1]
and then tried to replace the cd4c with min, but in this case, min was not always the smallest cd4. I suspect that's because when I sort based on the date, it doesn't also sort by the cd4c value. Is there a way that I can reliably drop the row with the largest CD4c value when id and cd4cdate match?

Many thanks!

--Ann



Ann C. Miller, PhD, MPH
Research Associate
FXB Center for Health and Human Rights
Harvard School of Public Health
651 Huntington Ave, 7th Floor
Boston, MA 02115
(617) 432-7297

*
*   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   |   What's new   |   Site index