2
votes

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?

3

3 Answers

1
votes

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'

}

6
votes

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) 
1
votes

You can create the means with

by type: egen conditional_mean = mean(price)