Reputation: 407
I have two variables year
as well as price
and I want to calculate the growth of former variable. However, the base year is constant in each calculation.
Consider the following table:
+------+-------+--------+
| 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 |
+------+-------+--------+
The growth formula is:
(price of second year - price of first year) / price of first year
In my formula, the first year is always 2012
:
growth = (price - price (for year=2012) ) / price (for year=2012)
How can I generate this formula in Stata?
Upvotes: 1
Views: 124
Reputation:
The following works for me:
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]
or
generate obs = _n
summarize obs if year == 2012, meanonly
generate wanted = (price - price[`=obs[r(min)]']) / price[`=obs[r(min)]']
Results:
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 |
+--------------------------------+
Upvotes: 3