global program "EEREV-D-21-00004_TableB3"

cap log close
cap log using "$log\log_$program$today", replace

**** REPLICATION OF TABLE B3 ****

clear all
set mem 2g
set more off

/*	
	OUTLINE
	1. CONTROLS TABLE
*/

*****************************
***** 1. CONTROLS TABLE *****
*****************************

// Import data

use $data\EEREV-D-21-00004_Data_Municipalities.dta, clear

keep if sample==1

global dialect 		"dialect_rhe dialect_pal dialect_als dialect_lor"
global pop 			"perc_b_1856_1900 perc_b_1928_1945 perc_b_1946_1964 perc_b_1965_1980 perc_b_1981_1996 perc_b_1997_2020 perc_male_counter"
global demog_chara 	"lpop_counter $pop perc_fore"
global socec_aspec 	"perc_bacplus perc_bluecollar perc_chom lrev_median lrev_sd"
global w 			"$demog_chara $socec_aspec"

// Shorten labels

la var annee				"\\ Election year"
la var perc_voixexd_ins 	"[0.3em] Radical right-wing (\% registered)"
la var perc_bna_ins 		"[0.3em] Abstention (\% registered)"
la var perc_fore 			"[0.3em] Foreigners (\%)"
la var perc_bacplus 		"[0.3em] High-school degree (\%)"
la var perc_bluecollar 		"[0.3em] Blue-collar workers (\%)"
la var perc_chom 			"[0.3em] Unemployment rate (\%)"
la var lrev_median 			"[0.3em] Median income (Log EUR)" // (EUR)
la var lrev_sd 				"[0.3em] Income std.dev. (Log EUR)"
la var lpop_counter			"[0.3em] Population (Log  excl. b. 1901-27)"
la var perc_b_1856_1900 	"[0.3em] Born 1856-1900 (\% excl. b. 1901-27)"
la var perc_b_1928_1945 	"[0.3em] Born 1928-1945 (\% excl. b. 1901-27)"
la var perc_b_1946_1964 	"[0.3em] Born 1946-1964 (\% excl. b. 1901-27)"
la var perc_b_1965_1980 	"[0.3em] Born 1965-1980 (\% excl. b. 1901-27)"
la var perc_b_1981_1996 	"[0.3em] Born 1981-1996 (\% excl. b. 1901-27)"
la var perc_b_1997_2020 	"[0.3em] Born 1997-2017 (\% excl. b. 1901-27)"
la var perc_male_counter	"[0.3em] Male (\% excl. b. 1901-27)"

// Get stats

estpost tabstat ///
	annee perc_voixexd_ins perc_bna_ins $w ///
	, s(N mean sd min max p5 p50 p95) columns(statistics) //
	
matrix N=e(count)
matrix m=e(Mean)
matrix s=e(SD)
matrix i=e(Min)
matrix a=e(Max)
matrix x=e(p5)
matrix y=e(p50)
matrix z=e(p95)

matrix obs = N'
matrix mean=m'
matrix sd=s'
matrix min=i'
matrix max=a'
matrix p5=x'
matrix p50=y'
matrix p95=z'

matrix b = obs, mean, sd, min, p5, p50, p95, max
matrix list b

// Export table

estout matrix(b, fmt(%9.0fc 2 2 2 2 2 2 2)) using ///
	"$results\\$program.tex", replace style(tex) type label /// 
	unstack collabels(none) ///
	mlabels(, none) ///
	msign(--) ///
	title(Covariates: Contemporary control variables) ///
	prehead("\begin{table}[t!]" ///
	"\begin{center}" ///
	"\begin{threeparttable}" ///
	"\begin{scriptsize}" ///
	"\caption{@title}" ///
	"\label{$program}"  /// 
	"\begin{tabular*}{\columnwidth}{@{\extracolsep{\stretch{1}}}l*{8}{c}@{}}" ///
	"\hline" "\hline" ///
	"&\multicolumn{3}{c}{Statistics} &\multicolumn{5}{c}{Distribution}\\" ///
	"\cmidrule(lr){2-4} \cmidrule(lr){5-9}" ///
	"\textit{Variable}	&\multicolumn{1}{c}{Obs.} &\multicolumn{1}{c}{Mean}  &\multicolumn{1}{c}{Std.Dev.}  &\multicolumn{1}{c}{Min}  &\multicolumn{1}{c}{5th pctile}   &\multicolumn{1}{c}{Median}  &\multicolumn{1}{c}{95th pctile}   &\multicolumn{1}{c}{Max}\\") ///
	posthead("\hline" ///
	"") ///
	prefoot("\\") ///
	postfoot("\hline" "\hline" ///
	"\end{tabular*}" ///
	"\end{scriptsize}" ///
	"\begin{tablenotes}[normal,flushleft]" ///
	"\begin{scriptsize}" ///
	"\item \emph{Notes}:" /// 	
	"Contemporary covariates used in Section \ref{VOTE}. " ///
	"The unit of observation is a municipality $\times$ year (the variables are time-varying). " ///
	"See Online Appendix \ref{DATA_COVARIATES} for the construction of the variables. " ///
	"\end{scriptsize}" ///
	"\end{tablenotes}" ///
	"\end{threeparttable}" ///
	"\end{center}" ///
	"\end{table}")

cap log close
