Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Combine results from multiple table (command) (result) into a multi-panel table

    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
    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)
    Cheers,
    Felix
    Stata Version: MP 18.0
    OS: Windows 11

  • #2
    Consider this:

    Code:
    collect clear
    * Extract means from xtsum and store them in global
    capture program drop xtsumglobal
    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) ///
        name(panelA)
    collect addtags panel[panA], fortags(command result)
        
    * 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)) ///
        name(panelB)
    collect addtags panel[panB], fortags(command result)
    
    * 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) ///
        name(panelC)
    collect addtags panel[panC], fortags(command result)
    
    collect combine all = panelA panelB panelC, label(left)
    collect label levels panel panA "Panel A" panB "Panel B" panC "Panel C"
    collect label levels command 1 "Tenure" 2 "Hours worked" 3 "Wage (log)" 4 "Weeks of unemployment", modify
    collect style header panel , title(hide) level(label)
    collect style cell result[Noninclusion Inclusion Diff], nformat(%9.2f)
    collect style cell result[pvalue], nformat(%9.3f)
    
    collect layout (panel#command) (result)
    which gives you:
    Code:
    . collect preview
    
    --------------------------------------------------------------------
                            |  Noninclusion   Inclusion    Diff   pvalue
    ------------------------+-------------------------------------------
    Panel A                 |                                          
      Tenure                |          3.12        4.74   -1.62    0.000
      Hours worked          |         36.56       36.52    0.04    0.646
      Wage (log)            |          1.67        1.80   -0.12    0.000
      Weeks of unemployment |          2.55        2.94   -0.39    0.000
    Panel B                 |                                          
      Tenure                |          4.41        6.30    1.89    0.000
      Hours worked          |         36.09       38.10    2.01    0.000
      Wage (log)            |          1.75        1.97    0.22    0.000
      Weeks of unemployment |          2.92        2.48   -0.44    0.063
    Panel C                 |                                          
      Tenure                |          4.41        4.41    0.01    0.939
      Hours worked          |         36.09       35.10    0.99    0.000
      Wage (log)            |          1.75        1.77   -0.02    0.031
      Weeks of unemployment |          2.92        2.64    0.28    0.076
    --------------------------------------------------------------------
    Last edited by Hemanshu Kumar; 25 May 2025, 23:43.

    Comment

    Working...
    X