假设组变量group, 组中的每个人变量为a
gen AverageIncome = .
levelsof group, local(group)
sort group a
by group: gen id = _n
foreach x of local group {
forvalue i = 1 / 20 {
summ income if id != `i' & group == `x'
replace AverageIncome = r(mean) if id == `i' & group == `x'
}
}