我有两个变量year
和price
,我想计算前一个变量的增长。但是,每次计算中的基准年都是不变的。
请考虑下表:
+------+-------+--------+
| year | price | growth |
+------+-------+--------+
| 2010 | 7 | -0.3 |
+------+-------+--------+
| 2011 | 9 | -0.1 |
+------+-------+--------+
| 2012 | 10 | 0 |
+------+-------+--------+
| 2013 | 12 | 0.2 |
+------+-------+--------+
| 2014 | 13 | 0.3 |
+------+-------+--------+
| 2015 | 17 | 0.7 |
+------+-------+--------+
增长公式是:
(price of second year - price of first year) / price of first year
在我的公式中,第一年始终为2012
:
growth = (price - price (for year=2012) ) / price (for year=2012)
如何在Stata中生成此公式?
答案 0 :(得分:3)
以下对我有用:
clear
input year price growth
2010 7 -0.3
2011 9 -0.1
2012 10 0
2013 12 0.2
2014 13 0.3
2015 17 0.7
end
generate wanted = (price - price[3]) / price[3]
或
generate obs = _n
summarize obs if year == 2012, meanonly
generate wanted = (price - price[`=obs[r(min)]']) / price[`=obs[r(min)]']
结果:
list, separator(0)
+--------------------------------+
| year price growth wanted |
|--------------------------------|
1. | 2010 7 -.3 -.3 |
2. | 2011 9 -.1 -.1 |
3. | 2012 10 0 0 |
4. | 2013 12 .2 .2 |
5. | 2014 13 .3 .3 |
6. | 2015 17 .7 .7 |
+--------------------------------+