Dear all,
I have been trying to solve the following problem: I want to convert data in (country-year) panel data format to conduct panel analysis.
My raw data is as follows:
Now, I use Baum and Cox (2007, Stata tip 45: Getting those data into shape) and try to reshape twice. This is what I try:
However, I get the error "variable IndicatorCode not constant within CountryName year". But my first reshape works and I am left with the following data before the code fails:
Now, how can I get country-year variables with all indicators in columns as in a normal country-year panel with multiple variables in the columns from ABOVE data?
I have tried many many variations in the reshape command but to no avail. Therefore, if any one of you can help me it will be greatly appreciated. Thanks in advance.
Kind Regards,
Sultan
Full Reference
Baum, C. F., & Cox, N. J. (2007). Stata tip 45: Getting those data into shape. Stata Journal, 7(2), 268-271.
I have been trying to solve the following problem: I want to convert data in (country-year) panel data format to conduct panel analysis.
My raw data is as follows:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str52 CountryName str3 CountryCode str131 IndicatorName str25 IndicatorCode double(YR1960 YR1961 YR1962) "Early-demographic dividend" "EAR" "Air transport, freight (million ton-km)" "IS.AIR.GOOD.MT.K1" . . . "Early-demographic dividend" "EAR" "Air transport, passengers carried" "IS.AIR.PSGR" . . . "Early-demographic dividend" "EAR" "Air transport, registered carrier departures worldwide" "IS.AIR.DPRT" . . . "Early-demographic dividend" "EAR" "All education staff compensation, primary (% of total expenditure in primary public institutions)" "SE.XPD.MPRM.ZS" . . . "Early-demographic dividend" "EAR" "All education staff compensation, secondary (% of total expenditure in secondary public institutions)" "SE.XPD.MSEC.ZS" . . . "Early-demographic dividend" "EAR" "All education staff compensation, tertiary (% of total expenditure in tertiary public institutions)" "SE.XPD.MTER.ZS" . . . "Early-demographic dividend" "EAR" "All education staff compensation, total (% of total expenditure in public institutions)" "SE.XPD.MTOT.ZS" . . . "Early-demographic dividend" "EAR" "Alternative and nuclear energy (% of total energy use)" "EG.USE.COMM.CL.ZS" .8061091873266525 1.0026897227851863 .8154736857379168 "Early-demographic dividend" "EAR" "Annual freshwater withdrawals, agriculture (% of total freshwater withdrawal)" "ER.H2O.FWAG.ZS" . . . "Early-demographic dividend" "EAR" "Annual freshwater withdrawals, domestic (% of total freshwater withdrawal)" "ER.H2O.FWDM.ZS" . . . end
Now, I use Baum and Cox (2007, Stata tip 45: Getting those data into shape) and try to reshape twice. This is what I try:
Code:
reshape long YR, i(CountryName IndicatorName) rename _j year reshape wide YR, i(CountryName year) j(IndicatorName IndicatorCode) string
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str52 CountryName str131 IndicatorName int year str3 CountryCode str25 IndicatorCode double YR "Bermuda" "GDP per capita (current US$)" 2012 "BMU" "NY.GDP.PCAP.CD" 85458.45550788604 "Bermuda" "GDP per capita (current US$)" 2013 "BMU" "NY.GDP.PCAP.CD" 85748.06541437825 "Bermuda" "GDP per capita (current US$)" 2014 "BMU" "NY.GDP.PCAP.CD" . "Bermuda" "GDP per capita (current US$)" 2015 "BMU" "NY.GDP.PCAP.CD" . "Bermuda" "GDP per capita (current US$)" 2016 "BMU" "NY.GDP.PCAP.CD" . "Bermuda" "GDP per capita growth (annual %)" 1960 "BMU" "NY.GDP.PCAP.KD.ZG" . "Bermuda" "GDP per capita growth (annual %)" 1961 "BMU" "NY.GDP.PCAP.KD.ZG" 2.1492747252721074 "Bermuda" "GDP per capita growth (annual %)" 1962 "BMU" "NY.GDP.PCAP.KD.ZG" 2.0039699570851326 end
Now, how can I get country-year variables with all indicators in columns as in a normal country-year panel with multiple variables in the columns from ABOVE data?
I have tried many many variations in the reshape command but to no avail. Therefore, if any one of you can help me it will be greatly appreciated. Thanks in advance.
Kind Regards,
Sultan
Full Reference
Baum, C. F., & Cox, N. J. (2007). Stata tip 45: Getting those data into shape. Stata Journal, 7(2), 268-271.
Comment