Here's a different approach that is more simple and efficient. If you've got a large dataset, this will be faster than the multi-step loop aTron suggested and this approach adapts to changes in the range of your "type" variable (if your dataset changes in size, you don't have to go back through your code and change the range in the forvalues command).
1) Create a fake dataset
clear
input type price
1 1000
2 3200
3 5000
4 1200
5 1000
1 4000
2 2000
3 4000
4 1200
5 2000
end
2) Generate the average price by type
bysort type: egen meanprice = mean(price)
li type price meanprice, sepby(type)
Finally: you could make aTron's code adapt to changes in your "type" variable, by changing the range from "1/10" to "1/`=_N'" (without quotes)...so,
forvalues i = 1/`=_N' {
qui sum price if type == `i', meanonly
replace newvar = r(mean) if type == `i'
}
Eric A. Booth
[email protected]
[email protected]