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" <[email protected]>
To   [email protected]
Subject   Re: st: Count number of observations in date range
Date   Thu, 19 Apr 2007 16:09:40 +0200

JS,

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.

regards
Sebastian

Here ist the code ...

/* BEGIN EXAMPLE */

//inputting example data
clear
input str3 event   str9 txt_start_date      str9 txt_end_date
XYZ     11jan2004         07feb2004
ZZZ     11jan2004         08feb2004
YYY     14jan2004         22jan2004
XXX     21jan2004         13mar2004
ZZY     30jan2004         04feb2004
end

//creating numeric dates from the string variables
gen start_date = date(txt_start_date,"dmy")
gen end_date   = date(txt_end_date,"dmy")

//record all levels from variable event to local macro -eventlist-
levelsof event, local(eventlist)

//initialize variables required
gen global_start=.
gen global_end=.
gen in_range=.
gen other_obs=.
tempvar tmp_oo

//begin looping over levels of event as recorded in local macro -eventlist-
quiet {	//quiet mode
foreach el of local eventlist {
	//copy begin of event to "global variable"
	replace global_start = start_date if event == "`el'"
	//copy end of event to "global variable"
	replace global_end   = end_date if event == "`el'"

	//copy event date rangeto all other observations
	bys global_start: replace global_start = global_start[1]
	by global_start : replace global_end = global_end[1]

	//evaluate for all other observations whether their event date range
overlaps with current "global" event date range
	replace in_range = ((start_date >= global_start & start_date <=
global_end) | (end_date >= global_start & end_dat<=global_end)) &
event!="`el'"
	
	//sum it up and save to temporary variable
	egen `tmp_oo' = sum(in_range)
	
	//copy the sum from the temp variable to the variable other_obs ONLY
for the current event (as in local macro -el-)
	replace other_obs = `tmp_oo' if event=="`el'"

	//drop temp variable
	drop `tmp_oo'
}
} 		//ending quiet mode

//drop some variables no longer needed
drop global_start global_end in_range

/* END EXAMPLE */

On 4/19/07, Jarl Svartsj� <[email protected]> wrote:
Dear Statalist,

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.

Which command should I use?
Thanks!

-JS



_________________________________________________________
Flyger tiden iv�g? F�nga dagen med Yahoo! Mails inbyggda
kalender. Dessutom 250 MB gratis, virusscanning och antispam. F� den p�: http://se.mail.yahoo.com
*
*   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/

*
*   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