mardi 12 février 2019

Counting the number of observations for a time window without LOOP (in STATA)

I am trying to calculate the number of observations within a time window. Specifically, for 1 hour before and 1 hour after the arrival time. I tried it for 20 observations, and the code runs properly, but my dataset consists of 250,000 obs and this loop has taken >1.5 hours to run. Would appreciate suggestions for a more efficient method.

Currently I have:

local N=_N
quietly forval i = 1/`N' {
    count if (arrivaltime >= arrivaltime[`i']-1 & arrivaltime<=arrivaltime[`i']+1) 
    replace countcall = r(N) in `i'
}

My data looks like this:


PatientID  ArrivalTime
1      2015-02-17 19:35:00
2      2015-02-17 20:42:00
3      2015-02-17 21:50:00 


Already tried an egen command but it didn't work.

Aucun commentaire:

Enregistrer un commentaire