* this code assumes that the current directory is .../WDI

set more off
import excel using WDI_download_052014, firstrow  case(lower) sheet("Data") clear // these must be downloaded by hand from data.worldbank.org. The second sheet of the Excel file lists the required variables


// clean-up

drop indicatorname


encode countryname, gen(countrynumber)
drop countryname
rename countrynumber WBcountryname
rename countrycode WBcountrycode
drop if mi(WBcountrycode)


// putting data into xt format including renaming of variables
reshape long yr, i(WB* indicator) j(year)
replace yr="" if yr==".."
destring yr, replace
drop if mi(yr)

replace indicatorcode = "literacy" if indicatorcode=="SE.ADT.LITR.ZS"
replace indicatorcode = "prim_enrol" if indicatorcode=="SE.PRM.TENR"
replace indicatorcode = "militaryspending" if indicatorcode=="MS.MIL.XPND.GD.ZS"
replace indicatorcode = "socproc_generosity" if indicatorcode=="allsp.gen_pop"
replace indicatorcode = "city" if indicatorcode == "EN.URB.MCTY.TL.ZS"
replace indicatorcode = "gdp" if indicatorcode == "NY.GDP.PCAP.PP.KD"
replace indicatorcode = "ngdp" if indicatorcode == "NY.GDP.PCAP.KD"
replace indicatorcode = "gini" if indicatorcode == "SI.POV.GINI"
replace indicatorcode = "incsh_low20" if indicatorcode == "SI.DST.FRST.20"
replace indicatorcode = "incsh_low10" if indicatorcode == "SI.DST.FRST.10"
replace indicatorcode = "unempl_m1524_natl" if indicatorcode == "SL.UEM.1524.MA.NE.ZS"
replace indicatorcode = "unempl_male_natl" if indicatorcode == "SL.UEM.TOTL.MA.NE.ZS"
replace indicatorcode = "unempl_total_natl" if indicatorcode == "SL.UEM.TOTL.NE.ZS"
replace indicatorcode = "unempl_m1524_ILO" if indicatorcode == "SL.UEM.1524.MA.ZS"
replace indicatorcode = "unempl_male_ILO" if indicatorcode == "SL.UEM.TOTL.MA.ZS"
replace indicatorcode = "unempl_total_ILO" if indicatorcode == "SL.UEM.TOTL.ZS"
replace indicatorcode = "migrants" if indicatorcode == "SM.POP.TOTL.ZS"
replace indicatorcode = "urban" if indicatorcode == "SP.URB.TOTL.IN.ZS"
replace indicatorcode = "pop_total" if indicatorcode == "SP.POP.TOTL"

reshape wide yr, i(WB* year) j(indicatorcode) string

renpfix yr


// generating log GDP data
gen lngdp=ln(gdp)
label variable lngdp "ln(GDP pc PPP, 2005 US$) (WDI)"
gen nlngdp=ln(ngdp)
label variable nlngdp "ln(GDP pc non-PPP, 2005 US$) (WDI)"

// transforming percentages to shares
foreach variant of varlist gini migrants urban city unempl* {
	replace `variant'=`variant'/100
	}


// labelling variables

label variable literacy "Literacy rate, adult total (% of people >14) (WDI)"
label variable prim_enrol "Adj. net enrollment rate, primary (% of prim s age children) (WDI)"
label variable militaryspending "military spending, % of GDP (WDI)"
label variable socproc_generosity "(%) Generosity of All Social Protection (WDI)"
label variable gdp "GDP pc PPP, 2005 $ (WDI)"
label variable nlngdp "GDP pc non-PPP, 2005 $ (WDI)"
label variable gini "Gini coefficient (WDI)"
label variable migrants "Internat'l migrant stock / total pop (WDI)"
label variable urban "Urban pop, share of total pop (WDI)"
label variable city "Pop in cities >1m / total pop (WDI)"
label variable unempl_total_natl "Unempl. total / labor force, nat'l est (WDI)"
label variable unempl_male_natl "Male unempl. / male labor force, nat'l est. (WDI)"
label variable unempl_m1524_natl "Male unempl. ages 15-24 / male labor force 15-24, nat'l est. (WDI)"
label variable unempl_total_ILO "Unempl. total / labor force, ILO (WDI)"
label variable unempl_male_ILO "Male unempl. / male labor force, ILO (WDI)"
label variable unempl_m1524_ILO "Male unempl. ages 15-24 / male labor force 15-24, ILO (WDI)"
label variable pop_total "Total population (WDI)"
label variable incsh_low20 "income share held by lowest 20% (WDI)"
label variable incsh_low10 "income share held by lowest 10% (WDI)"



merge m:1 WBcountrycode using WBcountrycodes.dta, keep(1 3) keepusing(ISO3) nogenerate
order WBcountryname WBcountrycode ISO3code year lngdp gdp nlngdp ngdp gini inc* migrants urban city unempl_* pop_total military

* save WDI.dta, replace
