Announcement

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

  • Stata 17 Table Collect functions (and how to keep formatting in markstat)

    I have been playing around with the new table and collect commands in Stata 17 as I see the appeal in being able to create customisable and reproducible tables.

    I'm wondering if it's possible to create a customisable table within markstat using collect. This works well up to a point, but the moment you apply any cell appearance formatting (e.g. changing table cell margins, changing fonts) this isn't transferred into the word document created by markstat with a simple 'collect preview' command. Cell formatting is applied correctly and retained if you use 'collect export' but this produces a separate word document.

    I'm wondering if it's possible to integrate collect with markstat to produce word docs containing inline customisable formatted tables.

    I realise this might be a question more for Germán, but if anyone has had a go with this and can provide any tips that would be great.

    Some code that I've been experimenting with is below.

    Also attached is a markstat file (rejected .stmd so I changed to .txt). markstat_collect.txt

    Thanks - Paul

    Code:
    sysuse auto
    collect clear 
    
    // Now launch Table Builder (Statistics > Summaries, Tables and Tests > Tables and Collections > Build and Style Table)
    
    quietly collect _r_b _r_se _r_ci _r_p: regress mpg price c.trunk##i.foreign 
    quietly collect layout (colname) (result) // Set proposed table layout
    
    // Note that the Table Builder now populates. Look at Levels within the Result(result) Dimension. These are the results you can display. 
    // In the above I have only selected the coefficient, SE, CI and p value.
    
    collect style showbase off // Remove ref cat
    collect style cell result[_r_b _r_se _r_ci], nformat(%5.2f) // leave 3 decimal places on p value
    collect style cell result[_r_b _r_se _r_ci _r_p], halign(center)
    collect label levels result _r_b "beta", modify // Change 'Coefficient' to "beta"
    collect label levels result _r_se "S.E.", modify // Change 'Std. error' to "S.E."
    collect label levels result _r_p "p", modify // Change 'p-value' to "p"
    collect label levels colname trunk "Trunk Space", modify // Remove units from label
    
    // Changes to the 'cell appearance style' don't appear in the console or in markstat docs
    
    collect style cell, margin(bottom top, width(10)) margin(left right, width(30)) // Increase column width
    collect style cell, font( garamond, )  // change font
    collect preview
    
    // How to incorporate this directly into word doc using markstat??
    
    // This is how it should look
    collect export "test.docx", as(docx) replace

  • #2
    Hi Paul Sanfilippo. I got my copy of Stata 17 and have been studying carefully the new customized tables and collection system, and of course testing how it works with -markstat-.

    I think you summarize the Word issues well. Basically -markstat- works with the log file, doing its best to translate it to various output formats. But not all styles transfer to the log file, and thus will not appear in the generated Word document. The alternative is to -collect export- to Word, which keeps all formatting but, as you noticed, produces a complete Word document. Unfortunately, I am not aware of any way to incorporate that into the Word document created by Pandoc, which is what -markstat- uses. I am actively looking for workarounds other than manual inclusion.

    Things are much better if you -collect export- to html, because -markstat- can -.include- html files, except that with the -tableonly- option Stata generates both an html and a css file. A forthcoming update of -markstat- fixes this issue by having -.include- grab both the html file and the css file if it exists (so it will work with other commands that generate html only). Things are even better if you -collect export- to LaTeX because -markstat- lets you use -\input- to import the file, and probably wrap it in a table or similar environment. This works fine with the current release of -markstat-.

    Perhaps I should also mention that there is a versioning issue if you use the new -table- command, which has a different syntax from the -table- command in Stata 16 and earlier. This will be fixed in the forthcoming -markstat- update, but a quick workaround is to add -version 17- in your first Stata block. I don't think this affects you because you didn't use -table- but it may help others.

    Comment


    • #3
      You may try Stata's html2docx command to convert the resulted HTML file to docx. The html2docx preserves styles in HTML and styles defined in css.

      Comment


      • #4
        Thanks very much Germán.

        Comment

        Working...
        X