clear all
cd "C:\Users\Vladana\Google Drive\ngiastovladana\Seminars\MSc\2018-2019\Stata\Eurostat"

*You have to download the file from Eurostat and save it in the working directory
import delimited namq_10_gdp.tsv, clear 

rename v1 unit

*Rename all imported variables
foreach x of varlist v*{
	local i = `i' + 1
    rename `x' y`i'
}

**

*Split first comma-separated imported variable into 4 variables
split unit, p(,)
rename unit1 UNIT
rename unit2 SA
rename unit3 ITEM
rename unit4 COUNTRY
drop if UNIT=="unit"

/*Example 
B1GQ Gross domestic product at market prices
P3 	Final consumption expenditure 
*/ 
keep if ITEM=="B1GQ"|ITEM=="P3"

*Convert data from wide to long form
*quarter is the new variable indicating the change of the variable 
help reshape
reshape long y, i(unit) j(quarter)

*Group the same variables
egen myunit=group(unit)

*Declare data to be time-series data specifying that the time unit will be the quarter
tsset myunit quarter

*Create a time variable indicating the year and the specific quarter
gen time = (tq(2017q4))
format time %tq

*By sorting unit replace time with the same quarter of the previous year
bys myunit: replace time=time-[_n-1]

keep time COUNTRY ITEM SA UNIT y

*Drop special characters from string variables using regular expressions
moss y, match("([0-9]+)") regex

*Specifying the decimal separator
gen decimal="."

*Concatenate the substrings with the decimal separator
egen ynew=concat(_match1 decimal _match2)

*Obtain the real value of the new string variable
gen VALUE=real(ynew)

gen ITEMnew=.
replace ITEMnew=1 if ITEM=="B1GQ"
replace ITEMnew=2 if ITEM=="P3"

keep time COUNTRY ITEMnew SA UNIT VALUE
order time COUNTRY ITEMnew SA UNIT VALUE

*Convert data from long to wide form
*ITEMnew is the existing variable indicating the change of the variable 
reshape wide VALUE, i(time COUNTRY SA UNIT) j(ITEMnew)

*Label variables
label var time "Year-Quarter"
label var COUNTRY "Country"
label var SA "Seasonal adjustment"
label var UNIT "Unit of measure"

*Rename and label variables
rename VALUE1 GDP
label var GDP "Gross domestic product at market prices"
rename VALUE2 consumption
label var consumption "Final consumption expenditure"

*Keep not seasonally adjusted (NSA) data
keep if SA=="NSA"

*Use deflated data (2010=100) by CLV_I10
keep if UNIT=="CLV_I10"

*Choose countries
keep if COUNTRY=="AT"|COUNTRY=="BE"|COUNTRY=="DE"| ///
COUNTRY=="DK"|COUNTRY=="EL"|COUNTRY=="ES"|COUNTRY=="FI"| ///
COUNTRY=="FR"|COUNTRY=="IT"|COUNTRY=="PT"|COUNTRY=="SE" 

*Scatterplot
scatter consumption GDP

*Log transformation
gen lnY=ln(GDP)
label var lnY "Natural Logarithm of GDP"
gen lnC=ln(consumption)
label var lnC "Natural Logarithm of Consumption"

*Histrogram density
kdensity lnY
kdensity lnC

*Panel dimension
encode (COUNTRY), gen(countryid)
*egen countryid=group(COUNTRY)
tsset countryid time

*Panel-data line plots
xtline GDP if time>=tq(1996q1), tlabel(#10, angle(vertical))


*Twoway scatterplots
tw (tsline GDP if COUNTRY=="EL") (tsline GDP if COUNTRY=="DE") if time>=tq(1996q1), legend(order(1 "Greece" 2 "Germany"))
tw (tsline GDP if COUNTRY=="EL") (tsline consumption if COUNTRY=="EL") if time>=tq(1996q1)
tw (tsline GDP if COUNTRY=="EL" & time>=tq(1995q1) & time<=tq(2017q4)) ///
(tsline consumption if COUNTRY=="EL" & time>=tq(1995q1) & time<=tq(2017q4))

*Create dummies for year and quarter
gen year = year(dofq(time))
label var year "Year"
gen quarter = quarter(dofq(time))
label var quarter "Quarter"

tab countryid, gen(countryid_)
tab year, gen(year_)
tab quarter, gen(quarter_)
label var quarter_1 "1st Quarter"
label var quarter_2 "2nd Quarter"
label var quarter_3 "3rd Quarter"
label var quarter_4 "4th Quarter"


*Regression estimates
reg lnC c.lnY if year>=1995
reg lnC c.lnY i.b1995.year i.b3.quarter if year>=1995
reg lnC c.lnY i.b1995.year i.b3.quarter i.b3.countryid if year>=1995

*Arrange regression, summary, and tabulation into an illustrative table
outreg2 using "Table 1" if time>=tq(1996q1), append tex word label ///
replace title(Summary Statistics) dec(1) ///
sum(log) eqkeep(N mean sd min max) ///
addnote(Some notes can be included here) ///
keep(lnY lnC year_22 year_23 year_24 quarter_1 quarter_2 quarter_3 quarter_4)

*Growth rates
gen grGDP=lnY-L4.lnY
gen grcons=lnC-L4.lnC

*Panel-data line plots
xtline grGDP if time>=tq(1996q1), tlabel(#10, angle(vertical))
tw (tsline grGDP if COUNTRY=="EL") (tsline grGDP if COUNTRY=="DE") if time>=tq(1996q1) ///
,tlabel(#10, angle(vertical))  legend(order(1 "Greece" 2 "Germany"))

tw (tsline grGDP if COUNTRY=="EL") (tsline grcons if COUNTRY=="EL") if time>=tq(1996q1) ///
,tlabel(#10, angle(vertical))  legend(order(1 "Growth GDP" 2 "Growth Consumption"))


*Regression estimates
keep if year>=2001

reg grcons grGDP 
est store Model1
reg grcons grGDP i.b2001.year quarter_1 quarter_2  quarter_4 
est store Model2
reg grcons grGDP i.b2001.year quarter_1 quarter_2  quarter_4 i.b3.countryid 
est store Model3

*Arrange regression, summary, and tabulation into an illustrative table
outreg2 [Model1 Model2 Model3] using "Table 2", title(Regression) replace tex word nocons label dec(3) addtext(Some notes can be included here)
