gen simul = .
qui forval i = 1/`=_N' {
count if start > end[`i'] | end < start[`i']
replace simul = _N - r(N) - 1 in `i'
}
This is shorter than other solutions I have
seen. Brevity is attainable by noticing that
* It is easier to count the complementary subset,
non-overlapping events, and then subtract. You
need to subtract 1 more to correct for events
matching themselves.
* -count- makes creation of a variable containing
counts superfluous. For more on that, see
Stata Journal 7(1) 2007.
What you think should be done about events that
start and end at the same time will affect whether
you prefer
count if start >= end[`i'] | end <= start[`i']
Nick
[email protected]
[email protected]
I have two date variables in my dataset, which define
the start and end dates for a certain event. For each
observation in the dataset, I'd like to count the
number of other observations in the dataset that
overlap with this event window.
I.e.
Event Start date End date Simultaneous events
-----------------------------------------------------------------------------------
XYZ 11jan04 07feb04 ???
ZZZ 11jan04 08feb04 ???
YYY 14jan04 22jan04 ???
XXX 21jan04 13mar04 ???
ZZY 30jan04 04feb04 ???
aso.aso.
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/