Bookmark and Share

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 Count Variable of Number of Obs in a Time Interval Preceding the Current Obs


From   David Feinswog <[email protected]>
To   [email protected]
Subject   Re: st: Generating a Count Variable of Number of Obs in a Time Interval Preceding the Current Obs
Date   Wed, 20 Nov 2013 15:57:34 +0000

Thank you Joe Canner and Nick Cox.

The following article was incredibly helpful: http://www.stata-journal.com/sjpdf.html?articlenum=pr0033 .

The solution I came up with for generating a count variable of open calls was:

gen opencall=0
local N=_N
forval i=1/`N' {
count if inrange(timecreate[`i'],timedispatch,timeclose)
replace opencall=r(N) in `i'
}

The quietly command can also be used by replacing "forval i=1/`N' " with "quietly forval i=1/`N' ".

This returns the number of calls dispatched and not closed at the time of a given call's creation. i.e. timedispatch<=timecreate[obs]<=timeclose

As Nick pointed out, 53*60000 would need to be used to create a count of calls dispatched up to 53 minutes prior to a -timecreate-.

Best,
David Feinswog

On Nov 19, 2013, at 12:27 PM, David Feinswog wrote:

I am working with Emergency Medical Services (EMS) ambulance call data
using Stata/IC 12.  The variables of consequence are:

1. timecreate: the time when the 911 dispatcher first received the 911
call
2. timedispatch: the time when the 911 dispatcher sent an EMS resource
(i.e. ambulance) to the location
3. timeclose: the time when the EMS resource cleared from the call and
returned to service
* if it matters, times are formatted %tc

I am trying to capture a measure of EMS system use load.
Specifically, I need to generate a count of all open calls when a call
came in.  For simplicity, I am going to keep things simple for now and
disregard "timeclose".  I will try to capture ALL CALLS DISPATCHED IN
THE 53 MINUTES (average call duration) PRIOR TO EACH "timecreate" OBS.


The simple attempt using egen:

egen countcall= count(timedispatch<= timecreate[_n] & timedispatch
+53<= timecreate[_n])

This returned _N for each observation.


Attempt using foreach loops:

gen countcall=o

*using a limited numlist (1/20 vs 1/_N) to speed things up while I
figure out the appropriate coding

foreach obs of numlist 1/20 {
sum timecreate if _n==`obs'
local time1=r(mean)
gen dum1=1 if timedispatch<= `time1' & timedispatch<= `time1'-53
egen dum2=total(dum1)
replace countcall=dum2 if _n==`obs'
drop dum*
}

This returned _n for each obs in the specified numlist 1/20

Please let me know if there is any additional information that could
help.

Best,
David Feinswog
Economics, Tulane University
*
*   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/

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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index