Hello,
I have a dataset and I am trying to sum observations over a time interval of 365 days in not equally spaced data. My data set includes a person, center, date and events. I am trying to sum the number of events that occur at each center over the next 365 days for each person. Not every observation/person has an event, sometimes it is 2 events, sometimes 1 event, sometimes 0. The dates are not equally spaced. When I use the code below, what results is the number of persons or the count over the 365 day period rather than the sum of events.
sort ctr Date
generate sum_events =.
local N = _N
quietly forvalues i = 1/`N' {
sum(events) if ctr == ctr[`i'] & inrange(Date, Date[`i'], Date[`i']+365),
replace sum_events = r(N) in `i'
}
This returns the count of persons/observations over 365 days rather than the number of events over 365 days
input double person_code str8 ctr float Date double events
89 "0009" 19947 0
49 "0009" 20048 2
24 "0009" 20198 2
809 "0009" 20262 0
550 "0009" 20328 2
336 "0009" 20376 2
330 "0009" 20402 2
709 "0009" 20429 2
586 "0021" 16560 0
301 "0021" 16560 1
891 "0021" 16560 2
612 "0021" 16560 0
I have tried this code as well
sort ctr Date
by ctr: gen sum_events = sum(events) if inrange(Date, Date[`i'], Date[`i']+365)
The r(111) message returns and it says "Date not found"
I am pretty new to stata. I appreciate any help/assistance greatly. I hope my question is clear.
Thank you.
Luke
I have a dataset and I am trying to sum observations over a time interval of 365 days in not equally spaced data. My data set includes a person, center, date and events. I am trying to sum the number of events that occur at each center over the next 365 days for each person. Not every observation/person has an event, sometimes it is 2 events, sometimes 1 event, sometimes 0. The dates are not equally spaced. When I use the code below, what results is the number of persons or the count over the 365 day period rather than the sum of events.
sort ctr Date
generate sum_events =.
local N = _N
quietly forvalues i = 1/`N' {
sum(events) if ctr == ctr[`i'] & inrange(Date, Date[`i'], Date[`i']+365),
replace sum_events = r(N) in `i'
}
This returns the count of persons/observations over 365 days rather than the number of events over 365 days
input double person_code str8 ctr float Date double events
89 "0009" 19947 0
49 "0009" 20048 2
24 "0009" 20198 2
809 "0009" 20262 0
550 "0009" 20328 2
336 "0009" 20376 2
330 "0009" 20402 2
709 "0009" 20429 2
586 "0021" 16560 0
301 "0021" 16560 1
891 "0021" 16560 2
612 "0021" 16560 0
I have tried this code as well
sort ctr Date
by ctr: gen sum_events = sum(events) if inrange(Date, Date[`i'], Date[`i']+365)
The r(111) message returns and it says "Date not found"
I am pretty new to stata. I appreciate any help/assistance greatly. I hope my question is clear.
Thank you.
Luke
Comment