简体   繁体   English

Stata中带有egen的循环

[英]Loops with egen in Stata

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 . delta采用与taubar对应的不同值。 However, I would like to create a variable which takes the mean of delta for each value of taubar . 但是,我想创建一个变量,该变量对于taubar每个值都taubar deltataubar 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 . 也就是说,如果taubar的频率值为400,且其值为2,并且对于这400个delta ,每个delta都不相同,那么我想创建一个变量,该变量是taubar每个值的delta 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. 在这种情况下,Stata创建的值称为0.234,这是所有400 2的增量平均值。 This is inefficient; 这是低效率的; also, there are 600 taubar s in the data set, each which may have 500 corresponding delta s. 同样,数据集中有600个taubar ,每个taubar可能具有500个对应的delta I would like to end up with each taubar from 1.5 to 327 corresponding to the mean of its delta s. 我想最后将每个taubar从1.5到327对应于其delta的平均值。

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. 但这会破坏您的数据并将其替换为均值数据集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM