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]
st: RE: Generating a Count Variable of Number of Obs in a Time Interval Preceding the Current Obs
From
Joe Canner <[email protected]>
To
"[email protected]" <[email protected]>
Subject
st: RE: Generating a Count Variable of Number of Obs in a Time Interval Preceding the Current Obs
Date
Tue, 19 Nov 2013 18:34:29 +0000
David,
I think you're on the right track with both of your approaches, there just needs to be a few tweaks. First, I think you are better off looking at both -timedispatch- and -timeclose-. You will get a more accurate answer and it is really no more difficult. In the following example, I have taken your idea of looking back at no more than 20 calls to see how many are active. You can, of course, modify this to suit the realities of the data. There may be better ways to realistically limit the number of comparisons, but I can't think of any at the moment.
gen countcall=0
forvalues i=1/20 {
replace countcall= countcall +1 if inrange(timecreate,timedispatch[_n-`i'],timeclose[_n-`i'])
}
Be careful with the -inrange- function if there is missing data for any of the arguments. If so, you may want to do something like:
replace countcall =countcall+1 if timecreate>=timedispatch[_n-`i'] & !mi(timecreate) & timcreate<=timeclose[_n-`i'] & !mi(timeclose[_n-`I'])
Let us know how it goes.
Regards,
Joe Canner
Johns Hopkins University School of Medicine
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of David Feinswog
Sent: Tuesday, November 19, 2013 7:27 AM
To: [email protected]
Subject: st: Generating a Count Variable of Number of Obs in a Time Interval Preceding the Current Obs
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/