My original data is in panel format and contains a large number of countries. Here's an extract:
I need to calculate a growth factor for cons_pc from year x to 2009 for each country. Year x varies from country to country and is not listed nor included in the dataset. The way I tackled the problem is converting the dataset from long to wide and then calculate the growth factor for each country, one by one by entering the below code, which requires manual entry of the years. I save it as one variable per country and then overlap them to have one single column of growth factors. For instance:
Two issues, here:
1. Is there a more effective way of generating the above growth factors? (may be related to my next question)
2. I have another set of .dta files, one per country. I want to apply each country's growth factor to a specific variable found in each country's respective .dta file. How can I do this?
For question 2, I am aware I can loop over each of the country files and create a empty factor variable in each. Then, replace it with the appropriate growth factor, such as :
..for each of the country .dta files. This; however, entails manually typing the growth factor I calculated - which I'd prefer not to..
All advice is welcomed!
Code:
input int time str6 timecode str16 country str3 countrycode float cons_pc 2005 "YR2005" "Burkina Faso" "bfa" 154960.66 2006 "YR2006" "Burkina Faso" "bfa" 158090.44 2007 "YR2007" "Burkina Faso" "bfa" 155224.17 2008 "YR2008" "Burkina Faso" "bfa" 177343.6 2009 "YR2009" "Burkina Faso" "bfa" 178538.2 2010 "YR2010" "Burkina Faso" "bfa" 178796 2011 "YR2011" "Burkina Faso" "bfa" 187291.05 2005 "YR2005" "Burundi" "bdi" 127747.41 2006 "YR2006" "Burundi" "bdi" 155927.84 2007 "YR2007" "Burundi" "bdi" 129821.98 2008 "YR2008" "Burundi" "bdi" 155126.52 2009 "YR2009" "Burundi" "bdi" 197272 2010 "YR2010" "Burundi" "bdi" 179738.9 2011 "YR2011" "Burundi" "bdi" 220682.9 end
Code:
local x cons_pc gen grx_1=`x'2009/`x'2006 if countrycode=="bdi" gen grx_2=`x'2009/`x'2009 if countrycode=="bfa" egen gr_x= rowmax(grx_1-grx_150)
1. Is there a more effective way of generating the above growth factors? (may be related to my next question)
2. I have another set of .dta files, one per country. I want to apply each country's growth factor to a specific variable found in each country's respective .dta file. How can I do this?
For question 2, I am aware I can loop over each of the country files and create a empty factor variable in each. Then, replace it with the appropriate growth factor, such as :
Code:
replace factor=xxxxx if "`dataset'" == "bdi" replace factor=xxxxx if "`dataset'" == "bfa"
All advice is welcomed!
Comment