Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Count number of observations in date range


From   "Austin Nichols" <[email protected]>
To   [email protected]
Subject   Re: st: Count number of observations in date range
Date   Thu, 19 Apr 2007 15:10:50 -0400

Jarl Svartsj� --
I don't think that Sebastian's code deals with all overlaps.
Another approach would be to loop over observations, which I outline below.

clear
input event start end
      1  16081 16108
      2  16081 16109
      3  16084 16092
      4  16091 16143
      5  16100 16105
      6  16109 16143
      7  16110 16110
end
format st end %d
li , noo clean
g long nsimul=.
forv i=1/`=_N' {
loc c "inrange(st,st[`i'],end[`i'])"
loc c "`c'|inrange(end,st[`i'],end[`i'])"
loc c "`c'|inrange(st[`i'],st,end)"
loc c "`c'|inrange(end[`i'],st,end)"
g byte s=`c'
g long sum=sum(s)
replace nsimul=sum[_N] in `i'
drop s sum
}
g long others=nsimul-1
compress
li , noo clean

The conditions for overlap with the current obs are collected in a
local `c' and
g byte s=`c'
determines which windows overlap with the current obs, but all that
could be in one -gen- command (which might break awkwardly across
lines in an email).

On 4/19/07, Sebastian F. B�chte <[email protected]> wrote:
I do not think there exist a _simple_ Stata command which would help
you achieving what you are asking for. But you could try to program it
yourself. I prepared some example code to show you how I would do it.
And as always I would expect that the code can be improved or maybe
some expert knows a Stata command that can do the trick.

On 4/19/07, Jarl Svartsj� <[email protected]> wrote:
> 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.
>
*
*   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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index