use oxford, clear set more off *generate an interger variables named cutp3 etc... for the age group cut points. *(age group cut points are the day before beginning of new age group or end of an age group). *do not generate variables for the beginning of the first, or end of the last age groups. *here there are 2 age groups, so only one cut point is needed. generate cutp3 = 547 *put the number of age group cut points into a local macro called nage. local nage=1 *generate variables for the exposure risk period cut points. *start with the day before the first risk period begins. *end with the last day in the final risk period. *consecutive cut points must always be increasing or equal - no overlapping. *where there is no risk period put cutp# greater than or equal to the end of the observation period. generate cutp4 = exday + 14 generate cutp5 = exday + 35 *to save space delete anything that is no longer needed drop exday *creates a macro `a' containing a list of all age group cut points separated by commas. *to be used to generate agegr, but doesn't work if the list is too long to fit into a macro. local a=cutp3 local b=`nage'+2 while `b'>3{ local c = cutp`b' local d = ",`c'`d'" local b = `b' - 1 } local a = "`a'`d'" foreach var of varlist cutp*{ replace `var' = cutp1 if `var' < cutp1 replace `var' = cutp2 if `var' > cutp2 } compress sort indiv eventday *lists all the cutp in increasing order by each event. reshape long cutp, i(indiv eventday) j(type) sort indiv eventday cutp type *nevents = 1 if event occurred between 2 cutp, 0 otherwise. sums nevents over individuals. by indiv: generate int nevents = 1 if eventday > cutp[_n-1]+0.5 & eventday <= cutp[_n]+0.5 collapse (sum) nevents, by(indiv cutp type) *interval = length of interval between cutp by indiv: generate int interval = cutp[_n] - cutp[_n-1] *agegr = factor for age group. (`a' is a macro containing a list of age group cut points) by indiv: generate int agegr = irecode(cutp, `a') *exgr = factor for exposure status. Factors may need changing with the recode function. generate exgr = type-`nage'-3 if type>`nage'+2 count if exgr>=. local nmiss = r(N) local nchange = 1 while `nchange'>0{ by indiv: replace exgr = exgr[_n+1] if exgr>=. count if exgr>=. local nchange = `nmiss'-r(N) local nmiss = r(N) } replace exgr = 0 if exgr==. *drop variables that are not needed to fit the model *cut out observations with missing intervals (there's 1 more cutp than there are intervals) *and intervals of 0 length (where consecutive cut points were equal) drop cutp* type drop if interval ==0 | interval==. *loginterval = log of the intervals generate loginterval = log(interval) *save the chopped up data, choose any name for your data. save oxfordchop, replace *fit the model xi: xtpoisson nevents i.exgr i.agegr, fe i(indiv) offset(loginterval) irr