Announcement

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

  • Collect combine let the title of the dimension group span all columns

    I am creating a table using the collect command. In this table, I am combining the descriptives for the entire sample and then for black and whites separately. These are then combined vertically.
    I would now like to format the dimension group, so that the header in the Table span all columns, the text is centered and in bold. The code above creates the table. As you will be able to see, the labels for the dimensions for group are left centered and do not span all columns.
    I have read the manual, but could not find a good starting point.

    Code:
    webuse union, clear
    
    collect clear
    
    collect style column, dups(center)
    collect style cell result[mean sd  min max N sd_b min_b max_b n sd_w min_w max_w Tbar], nformat(%8.2f)
    unab list : age-union
    
    foreach var of local list {
        collect, tags(col1name[`var']) : xtsum `var'
        collect, tags(col2name[`var']) : sum `var', detail
        collect remap col2name[`var'] = col1name[`var'], fortags(result[p50])
    }
    
    collect addtags group[panelA], fortags(result[mean p50 sd sd_b sd_w min max])
    collect rename default tbl_all
    
    * Separate collection for black and white people
    forvalues i = 0(1)1 {
        collect create dev`i'
        foreach var of local list {
            collect, tags(col1name[`var']): xtsum `var' if black == `i'
            collect, tags(col2name[`var']): sum `var' if black == `i', detail
            collect remap col2name[`var'] = col1name[`var'], fortags(result[p50])
        }
        collect dims
        collect layout (col1name) (result[mean p50 sd sd_b sd_w min max])
        collect style cell result[mean p50 sd sd_b sd_w min max], nformat(%8.2f) halign(center) valign(top)
        collect label levels result p50 "Median" sd "SD" sd_w "Within SD" sd_b "Between SD" min "Min." max "Max.", modify
        foreach var of local list {
            local vlabel : variable label `var'
            collect label levels col1name `var' "`vlabel'"
        }
        collect addtags group[panel`i'], fortags(result[mean p50 sd sd_b sd_w min max])
    }
    
    * Combine the collections into one table
    collect combine all = tbl_all dev0 dev1, label(right) replace
    collect label levels group panelA "All" panel0 "White" panel1 "Black"
    collect style header group , title(hide)
    collect style putdocx, layout(autofitcontents)
    collect layout (group[panelA panel0 panel1]#col1name) (result)
    putdocx begin
    putdocx paragraph, style(Heading2)
    putdocx text ("Summary table")
    putdocx paragraph
    putdocx collect
    putdocx save "Example-Summary-Table.docx", replace
    Cheers,
    Felix
    Stata Version: MP 18.0
    OS: Windows 11
Working...
X