REPRO-Bench / 17 /replication_package /code /lib /stata /ado /panelcombine.ado
anonymous-submission-acl2025's picture
add 17
8a79f2e
// Do file of program to combine tables generated by iebaltab (and maybe other functions of esttab) to single table with multiple panels
// Andres Rodriguez modification of do file found in https://raw.githubusercontent.com/steveofconnell/PanelCombine/master/PanelCombine.do
//some 2010-gen inspiration from here: https://www.stata.com/statalist/archive/2010-01/msg00213.html
cap prog drop panelcombine
prog define panelcombine
qui {
syntax, use(str asis) paneltitles(str asis) columncount(integer) save(str asis) [CLEANup]
*local use "Balance_fullsample_blocker.tex Balance_fullsample_rsi.tex"
*local panel1titles "Blocker Treatment" "RSI Treatment"
*local columncount 4
*local save "Balance.tex"
preserve
tokenize `"`paneltitles'"'
//read in loop
local num 1
while "``num''"~="" {
local panel`num'title="``num''"
local num=`num'+1
}
tokenize `use'
//read in loop
local num 1
while "``num''"~="" {
tempfile temp`num'
insheet using "``num''", clear
save `temp`num''
local max = `num'
local num=`num'+1
}
//conditional processing loop
local num 1
while "``num''"~="" {
local panellabel : word `num' of `c(ALPHA)'
use `temp`num'', clear
if `num'==1 { //process first panel -- clip bottom
drop if strpos(v1,"Note:")>0 | strpos(v1,"in parentheses")>0 | strpos(v1,"p<0")>0
drop if v1=="\end{tabular}" | v1=="}"
cap drop temp
g temp = regex(v1,"Variable.*\\\\ \\hline \\\\")
*replace v1=subinstr(v1,"-1.8ex","0ex",1) if temp==1
replace temp = temp+temp[_n-1] if _n>1
replace temp = temp+temp[_n-1] if _n>1
local nobs = _N + 1
set obs `nobs'
gen v2=v1[_n-1] if temp>2
replace v2=v1 if temp<=1
replace v2 = "\multicolumn{`columncount'}{l}{ \linebreak \textbf{\textit{Panel `panellabel': `panel1title'}}} \\" if v2==""
drop v1 temp
rename v2 v1
replace v1 = "\hline \\[-1.8ex]" if regex(v1,"\\hline \\hline \\\\.*") & _n>4 //this is intended to replace the bottom double line; more robust condition probably exists
}
else if `num'==`max' { //process final panel -- clip top
//process header to drop everything until first hline
g temp = regex(v1,"Variable.*\\\\ \\hline \\\\.*")
*g temp = (v1 == "\hline")
replace temp = temp+temp[_n-1] if _n>1
drop if temp==0
drop temp
replace v1 = " \multicolumn{`columncount'}{l}{\linebreak \textbf{\textit{Panel `panellabel': `panel`num'title'}}} \\" if _n==1
}
else { //process middle panels -- clip top and bottom
//process header to drop everything until first hline
g temp = regex(v1,"Variable.*\\\\ \\hline \\\\.*")
*g temp = (v1 == "\hline")
replace temp = temp+temp[_n-1] if _n>1
drop if temp==0
drop temp
replace v1 = " \multicolumn{`columncount'}{l}{\linebreak \textbf{\textit{Panel `panellabel': `panel`num'title'}}} \\" if _n==1
drop if strpos(v1,"Note:")>0 | strpos(v1,"in parentheses")>0 | strpos(v1,"p<0")>0
drop if v1=="\end{tabular}" | v1=="}"
replace v1 = "\hline \\[-1.8ex]" if regex(v1,"\\hline \\hline \\\\.*")
}
save `temp`num'', replace
local num=`num'+1
}
use `temp1',clear
local num 2
while "``num''"~="" {
append using `temp`num''
local num=`num'+1
}
outsheet using `save', noname replace noquote
if "`cleanup'"!="" { //erasure loop
tokenize `use'
local num 1
while "``num''"~="" {
erase "``num''"
local num=`num'+1
}
}
restore
}
end