For my research, I am describing my sample construction of my country-year panel data. Data is not available for all years and countries, so I am trying to be as transparent as possible about the way I constructed my sample.
I have built the following table using the table (command) (result), collect and putdocx commands. I would like to combine these separate tables into one table. I have consulted example 4 in the STATA CUSTOMIZABLE TABLES AND COLLECTED RESULTS REFERENCE MANUAL and the Stata blog on customizable tables, but I am not able to combine the results into one multi-panel table.
* Working code creating separate tables
I have built the following table using the table (command) (result), collect and putdocx commands. I would like to combine these separate tables into one table. I have consulted example 4 in the STATA CUSTOMIZABLE TABLES AND COLLECTED RESULTS REFERENCE MANUAL and the Stata blog on customizable tables, but I am not able to combine the results into one multi-panel table.
* Working code creating separate tables
Code:
* Extract means from xtsum and store them in global
program define xtsumglobal
syntax varlist, gen(name)
foreach v of varlist `varlist' {
xtsum `v'
global `gen'_`v' = r(mean)
}
end
* Use the built-in panel data set
use https://www.stata-press.com/data/r18/nlswork.dta, clear
* Store the means in a matrix
xtsumglobal tenure hours ln_wage wks_ue, gen(s1pop)
* Take a sample of my years
keep if inrange(year, 80, 88)
* One sample t-test
table (command) (result), ///
command(Noninclusion = $s1pop_tenure Inclusion = r(mu_1) Diff = ($s1pop_tenure - r(mu_1)) pvalue = r(p): ttest tenure = $s1pop_tenure) ///
command(Noninclusion = $s1pop_hours Inclusion = r(mu_1) Diff = ($s1pop_hours - r(mu_1)) pvalue = r(p): ttest hours = $s1pop_hours) ///
command(Noninclusion = $s1pop_ln_wage Inclusion = r(mu_1) Diff = ($s1pop_ln_wage - r(mu_1)) pvalue = r(p): ttest ln_wage = $s1pop_ln_wage) ///
command(Noninclusion = $s1pop_wks_ue Inclusion = r(mu_1) Diff = ($s1pop_wks_ue - r(mu_1)) pvalue = r(p): ttest wks_ue = $s1pop_wks_ue)
collect label levels command 1 "Tenure" 2 "Hours worked" 3 "Wage (log)" 4 "Weeks of unemployment", modify
collect style cell result[Noninclusion Inclusion Diff], nformat(%9.2f)
collect style cell result[pvalue], nformat(%9.3f)
collect style putdocx, layout(autofitcontents) title("Panel A")
putdocx begin
putdocx collect
putdocx save "Panel-A.docx", replace
* Two sample t-test
table (command) (result), ///
command(Noninclusion = r(mu_1) Inclusion = r(mu_2) Diff = (r(mu_2)-r(mu_1)) pvalue = r(p): ttest tenure, by(union)) ///
command(Noninclusion = r(mu_1) Inclusion = r(mu_2) Diff = (r(mu_2)-r(mu_1)) pvalue = r(p): ttest hours, by(union)) ///
command(Noninclusion = r(mu_1) Inclusion = r(mu_2) Diff = (r(mu_2)-r(mu_1)) pvalue = r(p): ttest ln_wage, by(union)) ///
command(Noninclusion = r(mu_1) Inclusion = r(mu_2) Diff = (r(mu_2)-r(mu_1)) pvalue = r(p): ttest wks_ue, by(union))
collect label levels command 1 "Tenure" 2 "Hours worked" 3 "Wage (log)" 4 "Weeks of unemployment", modify
collect style cell result[Noninclusion Inclusion Diff], nformat(%9.2f)
collect style cell result[pvalue], nformat(%9.3f)
collect style putdocx, layout(autofitcontents) title("Panel B")
putdocx begin
putdocx collect
putdocx save "Panel-B.docx", replace
* Keep unions
keep if union == 0
* Store the means in globals
xtsumglobal tenure hours ln_wage wks_ue, gen(s3pop)
* Select industries
gen include_industry = (ind_code > 7)
keep if include_industry == 1
* One sample t-test
table (command) (result), ///
command(Noninclusion = $s3pop_tenure Inclusion = r(mu_1) Diff = ($s3pop_tenure - r(mu_1)) pvalue = r(p): ttest tenure = $s3pop_tenure) ///
command(Noninclusion = $s3pop_hours Inclusion = r(mu_1) Diff = ($s3pop_hours - r(mu_1)) pvalue = r(p): ttest hours = $s3pop_hours) ///
command(Noninclusion = $s3pop_ln_wage Inclusion = r(mu_1) Diff = ($s3pop_ln_wage - r(mu_1)) pvalue = r(p): ttest ln_wage = $s3pop_ln_wage) ///
command(Noninclusion = $s3pop_wks_ue Inclusion = r(mu_1) Diff = ($s3pop_wks_ue - r(mu_1)) pvalue = r(p): ttest wks_ue = $s3pop_wks_ue)
collect label levels command 1 "Tenure" 2 "Hours worked" 3 "Wage (log)" 4 "Weeks of unemployment", modify
collect style cell result[Noninclusion Inclusion Diff], nformat(%9.2f)
collect style cell result[pvalue], nformat(%9.3f)
collect style putdocx, layout(autofitcontents) title("Panel C")
putdocx begin
putdocx collect
putdocx save "Panel-C.docx", replace
* Append tables to one
putdocx append "Panel-A.docx" "Panel-B.docx" "Panel-C.docx", saving(Table_Sample.docx, replace)

Comment