tags:

views:

578

answers:

2

Let's say I have a STATA dataset that has two variables: type and price. The type value for each observation is a number between 1 and 10.

I want to add a third value that is the average price of all variables of that type. So, for example, if the first observation had a type of 3 and a price of 10, then I'd like to add a third value that is the average price of all observations with type=3.

How can I do this in STATA?

+1  A: 

There are probably a few ways to do this but this is what I'd suggest.

gen newvar = .
forvalues i = 1/10 {

  qui sum price if type == `i', meanonly
  replace newvar = r(mean) if type == `i'

}

aTron
A: 

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]

eric.a.booth