生成变量的放气值

时间:2019-02-24 14:39:46

标签: stata

我有以下数据:

* Example generated by -dataex-. To install: ssc install dataex
clear
input float id int year double price
1 2003 4179
1 2004 4199
1 2005 4233
2 2003 4248
2 2004 4246
2 2005 4248
3 2003 4594
3 2004 4606
3 2005 4668
end

我想计算price变量的放气值。

如何在Stata中做到这一点?

1 个答案:

答案 0 :(得分:1)

以下对我有用:

bysort id (year): egen double last = total((year == 2005) * price)
bysort id (year): generate double wanted = price / last

list, sepby(id)

     +---------------------------------------+
     | id   year   price    last      wanted |
     |---------------------------------------|
  1. |  1   2003    4179    4233   .98724309 |
  2. |  1   2004    4199    4233   .99196787 |
  3. |  1   2005    4233    4233           1 |
     |---------------------------------------|
  4. |  2   2003    4248    4248           1 |
  5. |  2   2004    4246    4248   .99952919 |
  6. |  2   2005    4248    4248           1 |
     |---------------------------------------|
  7. |  3   2003    4594    4668   .98414739 |
  8. |  3   2004    4606    4668   .98671808 |
  9. |  3   2005    4668    4668           1 |
     +---------------------------------------+