Code:

```
gen graph =.
preserve
forvalues v = 10(1)12 {
replace graph = `v'
drop if sales`v' == 0 | sales`v' ==. | sales`v' < 0
gen prod`v' = sales`v'/prof_`v'
egen prodmean`v'= mean(prod`v')
gen y`v' = prod`v'/prodmean`v'
gen percentile`v' = 0
forvalues i = 10(10)90 {
egen p`i'`v' = pctile(sales`v'), p(`i')
gen absolute_`i'`v' = abs(sales`v'-p`i'`v')
sort absolute_`i'`v'
gen min_`i'`v' = absolute_`i'`v'[1]
replace percentile`v' = `i' if absolute_`i'`v' == min_`i'`v'
}
keep if percentile`v' != 0
gen Y`v' = .
gen id`v' = _n
foreach i in varlist id`v' {
replace Y`v' = (y`v'[i]+y`v'[i+1])/2 if percentile`v'[i] == percentile`v'[i+1]
replace Y`v'= y`v' if percentile`v'[i] != percentile`v'[i+1]
}
duplicates drop percentile`v', force
if graph == 12 {
twoway scatter Y12 percentile12, title("2012")
}
if graph == 11 {
twoway scatter Y11 percentile11, title("2011")
}
if graph == 10 {
twoway scatter Y10 percentile10, title(" 2010")
restore
}
```

I want to run the graphs for successive years. Every time I find the percentiles, it destroys the dataset so I want to restore it to the way it was before and run the loop to find the percentiles again.

P.S: Pay no attention to the code finding percentiles. I just want to know why the code is not working to the 12th year. Thanks!!