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: generating a variable that requires comparing two dates
From
Clyde B Schechter <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: generating a variable that requires comparing two dates
Date
Fri, 12 Apr 2013 15:11:45 +0000
Ariel Linden wrote:
...
I would like to generate a new variable ( by ID) that will equal 1 if there
was an alert (triggered on the surveydate, when that surveydate was the same
as the admitdate. The obvious problem is that the dates are not always
aligned so a straight "bys ID: gen test if surveydate==admitdate & alert==1"
will not always work.
In the data below, the code would need to "see" that the admitdate of
4/6/2011 has a corresponding surveydate a few lines down where the alert==1.
Similarly, the next admitdate is 4/20/2011, so it would need to lookup the
corresponding surveydate and verify that alert==1.
...
**** example data****
ID surveydate alert admitdate
1 4/2/2011 1 4/6/2011
1 4/3/2011 1 4/20/2011
1 4/4/2011 0 4/20/2011
1 4/5/2011 0 4/20/2011
1 4/6/2011 1 4/20/2011
1 4/7/2011 0 4/20/2011
1 4/8/2011 0 4/20/2011
1 4/9/2011 0 4/20/2011
1 4/10/2011 0 4/20/2011
1 4/11/2011 0 4/20/2011
1 4/12/2011 0 4/20/2011
1 4/13/2011 0 4/20/2011
1 4/14/2011 0 4/20/2011
1 4/15/2011 0 4/20/2011
1 4/16/2011 0 4/20/2011
1 4/17/2011 0 4/20/2011
1 4/18/2011 0 4/20/2011
1 4/19/2011 0 4/20/2011
1 4/20/2011 1 4/20/2011
1 4/21/2011 0 4/20/2011
How about:
tempfile surveyalertdates
preserve
keep if alert == 1
keep id surveydate
/* DEPENDING ON YOUR DATA YOU NEXT SHOULD DO EITHER
isid id surveydate
OR
duplicates drop
*/
rename surveydate alertdate
save `surveyalertdates'
restore
gen alertdate = admitdate
merge 1:1 id alertdate using `surveyalertdates', keep(match master)
gen test = (_merge == 3)
The later question about identifying the situation where the alert occurred 1 day before admitdate just requires modifying the gen alertdate... statement in the above.
Hope this helps.
Clyde Schechter
Dept. of Family & Social Medicine
Albert Einstein College of Medicine
Bronx, NY, USA
*
* 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/