0
votes

I have a data set which looks as follows:

  delta    taubar
   0       1.5
  -0.223   2
   3       6.5
   0.334   2
   11      7
   2.123   1.5

delta takes different values corresponding to taubar. However, I would like to create a variable which takes the mean of delta for each value of taubar. That is, if taubar holds a value of 2 with a frequency of say 400, and for each of these 400 delta is different, I would like to create a variable which is the mean of delta for each value of taubar.

I have used

egen meandelta = mean(delta) if taubar == 2

In this case Stata creates a value of say 0.234 which is the mean of the deltas across all 400 2's. This is inefficient; also, there are 600 taubars in the data set, each which may have 500 corresponding deltas. I would like to end up with each taubar from 1.5 to 327 corresponding to the mean of its deltas.

1

1 Answers

1
votes

It's not entirely clear what you want, but there's no need to loop.

If you want to keep your original data, try:

bysort taubar: egen meandelta = mean(delta) 

You might also consider:

collapse (mean) meandelta=delta, by(taubar)

but that will destroy your data and replace it with a dataset of means.