Taking a conditional mean in Stata

Let's say I have a Stata dataset that has two variables: type

and price

. The value type

for each observation is a number between 1 and 10.

I want to add a third value which is the average of price

all the variables of this type

. So, for example, if the first observation had type

of 3 and a price

10, then I would like to add a third value, which is the average of price

all observations with type

= 3.

How can I do this in Stata?

+2


source to share


3 answers


There are probably several ways to do this, but this is what I suggest.

gen newvar = .
forvalues i = 1/10 {

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

      



}

+1


source


This is a different approach that is simpler and more efficient. If you have a large dataset, this will be faster than aTron's suggested multi-step loop, and this approach will adapt 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 a command forvalues

) ...

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) Create a mean price

bytype

bysort type: egen meanprice = mean(price)

li type price meanprice, sepby(type) 

      

+6


source


You can create funds with

by type: egen conditional_mean = mean(price)

      

+1


source







All Articles