Hi!

I'm trying to create groups in my data: for every year and district I create a group that includes everyone 2.5 years more or less than that person. To do this, I created a loop that takes every year district group and only keeps the observations I want and appends them. I run a code that works up to a certain point and suddenly stops working. Here is my code:

keep person_id year district
duplicates drop

preserve
keep if _n>=1
gen group=.
save "file_1.dta", replace
restore

local i=1

levelsof district, local(lev_1)
levelsof year, local(lev_2)

foreach l_1 of local lev_1{
foreach l_2 of local lev_2{
preserve
keep if abs(year-`l_2')<=3 & district==`l_1'

keep person_id
gen year=`l_2'
gen district=`l_1'
gen group=`i'

append using "file_1.dta"
save "file_1.dta", replace
restore

local i=`i'+1
}
}

Thanks for any help!

Miranda