Hi Stata Community,

I hope you are all doing well. I am afraid I have a question regarding the calculation of the mean inside a centile range. I would like to calculate the mean of the variable change1m_ inside the centile range c(24 69) for every year since 2000. My code is the following:

HTML Code:
putexcel set Trimmed_PCE.xlsx, sheet(data) replace
putexcel A1 = "Year"
putexcel B1 = "Month"
putexcel C1 = "Trimmed mean"
local row = 2
forvalues y = 2000(1)2021 {
forvalues m = 1(1)12{
preserve
keep if year==`y' & month==`m'
gsort -change1m_
centile change1m_, c(24 69)
asgen wtm_`y'`m' = change1m_ if inrange( change1m_, `r(c_1)', `r(c_2)'), w(weight_)
summarize wtm_`y'`m'
putexcel A`row' = `y'
putexcel B`row' = `m'
putexcel C`row' = `r(mean)'
local ++row
restore
}
}
I loop though the years, sot the values in descending order, and then hope to calculate the mean of the values that fall inside the range. I then inout into Excel. However, I think this calculates the weighted mean or something else that is not quite right. I would like the "pure" mean of the values that fall inside c(24 69). How do I do that? Your help would be much appreciated. Thanks!