Hi everyone, I am new to STATA and am trying to create a loop that replaces the previous 399 observations and current observation of variable D_exp with value = 1 if the value of variable D is =1. I am unsure of how to do this and have posted my code below for reference. Currently the code provides the exact same results as variable D, it does not replace D_exp with 1 for the previous 399 observations. Any help is appreciated. Thank you
Code:
gen C = 0
gen D = 0
replace C = 1 if beta <= 1 & t>=400
replace D = 1 if beta > 1 & t>=400
// Create D_exp and replace with value 1 for the previous 399 observations plus the current observation when variable D is equal to 1
gen D_exp = 0
forval i = 400/`=_N' {
if D[`i'] == 1 {
replace D_exp = 1 in `i-399'/`i'
}
}

Comment