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   "Sebastian F. Büchte" <>
Subject   Re: st: Count number of observations in date range
Date   Thu, 19 Apr 2007 21:31:28 +0200


it would be very helpful for me if you could point out in more detail
where my code fails to identify an overlap. I admit that I somehow
assume that the entries in the "event" variable uniquely identify
observations. If this was not the case I would miss overlaps. But,
under this assumption I cannot figure out which overlaps I would miss.


On 4/19/07, Austin Nichols <> wrote:
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.

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
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
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 <> 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ö <> 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:

*   For searches and help try:

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