Announcement

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

  • Example "table" command

    Dear Statalist followers,

    I am exploring the table command introduced in Stata 17. The command looks quite versatile, I was hoping for a practical example of how to output a nicely formatted descriptive statistics table with min, mean, max, SD and correlations (with statistical significance). Essentially, I want to do the same as with the user-written asdoc command below, but using the new table command:

    Code:
    ssc install asdoc
    sysuse auto, clear
    
    asdoc sum price mpg weight length ///
        , label dec(2) title(Means, Standard Deviations, Minimums, Maximums) stat(mean sd min max) save(descriptive_statistics.doc) replace
    
    asdoc pwcorr price mpg weight length ///
        , star(.05) label dec(2) save(descriptive_statistics.doc) append
    I would appreciate the example to better understand the syntax of the command.

    Thanks,
    Marvin

  • #2
    This requires two collections and putdocx to publish their tables to a single MS Word document. The first collection is built using table. The second is built using results from pwcorr.

    In the following I add extra calls to collect to show how you would change some of the default labels and styles.
    Code:
    sysuse auto
    
    local vlist price mpg weight length
    
    * summary table
    table (var) (result), ///
        stat(mean `vlist') ///
        stat(sd `vlist') ///
        stat(min `vlist') ///
        stat(max `vlist')
    * label and style changes
    collect label levels result sd "SD" min "Min" max "Max", modify
    collect style cell result[mean sd], nformat(%9.2f)
    collect style cell border_block[corner row-header], border(right, pattern(none))
    collect layout
    
    * correlation table
    collect create pwcorr
    pwcorr `vlist', sig
    collect get C=(vech(r(C))) sig=(vech(r(sig)))
    * stars, label, and style changes
    collect stars sig .05 "*", attach(C) shownote
    collect style cell result[C], nformat(%5.2f)
    local i 0
    foreach v of local vlist {
        local ++i
        local lab : var label `v'
        if `"`lab'"' == "" {
            local lab `v'
        }
        collect label levels rowname `v' "(`i') `lab'", modify
        collect label levels roweq `v' "(`i')", modify
    }
    collect style cell border_block[corner row-header], border(right, pattern(none))
    collect layout (rowname) (roweq) (result[C])
    
    * publish to MS Word
    putdocx begin
    putdocx paragraph, halign(center)
    putdocx text ("Means, Standard Deviations, Minimums, Maximums"), bold
    putdocx collect, name(Table) tablename(sum_tab)
    putdocx paragraph, halign(center)
    putdocx text ("Pairwise correlations"), bold
    putdocx collect, name(pwcorr) tablename(pwcorr_tab)
    putdocx save report.docx, replace
    Here is a screenshot of the published tables (from LibreOffice).
    Click image for larger version

Name:	Screenshot 2023-08-22 at 10.23.31 AM.png
Views:	1
Size:	48.7 KB
ID:	1724647

    Comment


    • #3
      Thanks so much, Jeff! This is very helpful!

      Comment


      • #4
        Hopefully, this is useful to others as well.

        Comment

        Working...
        X