Title | Supported file formats for exporting customizable tables in Stata. | |
Author | Mia Lv, StataCorp |
When we create customizable tables in Stata using the table/etable/dtable/collect commands, many times we would like to include them in a report or document outside of Stata. There is no need to copy and paste these tables into our external files because Stata allows us to export our tables to various file formats, resulting in a table format more polished than the one we would get by copying and pasting manually.
Below is a complete list of supported file formats for exporting tables, with one example provided for each format.
File Extension | File Format |
---|---|
.docx | Microsoft Word |
.xlsx | Microsoft Excel 2007/2010 or newer |
.html | HTML 5 with CSS |
.md | Markdown |
.tex | LaTeX |
.txt | Plain text |
.smcl | SMCL (Stata Markup and Control Language) |
Please note that there are multiple methods to export a table to another file, such as using collect export, the export() option with dtable/etable, and putexcel. We showcase several exporting methods in this FAQ. But if you are interested in a more detailed discussion on all the exporting methods, please refer to FAQ: What methods can we use to export a customizable table from Stata to another format?
Example code:
. clear all . webuse nhanes2 . table region, stat(fvpercent sex race) nformat(%5.2f) . collect style cell result[fvpercent], sformat(%s%%) . collect style cell cell_type[item], font(, bold) . collect style cell var[1.sex 2.sex], shading( background(coral)) . collect style cell var[1.race 2.race 3.race], shading( background(aqua)) . collect title "Table 1: Sex and race percentages by region" . collect layout . collect export myfile.docx, replace
Please note that some table style customization such as the shading color and font change cannot be reflected in the Results window of Stata; however, it can be viewed in the Tables Builder or the exported files. For more detailed information, please refer to FAQ: Why can't I observe the style changes (background shading, font, etc.) in my table in the Results window?
Exported Word file:
Remark:
Alternatively, you may use the command putdocx collect to export a table or tables to a .docx file.
Example code:
. clear all . webuse nhanes2 . dtable weight bmi bpsystol i.agegrp, by(highbp, tests) column(test(, font(,bold))) nformat(%9.2f mean sd) title(Table 1: Descriptive statistics and tests) . collect style cell result[_dtable_test], shading(background(lightyellow)) font(, bold) . collect export myfile.xlsx, replace
Exported Excel file:
Remarks:
1. When exporting to an .xlsx file, you have the option to modify an existing file instead of creating a new file. Additionally, you can specify the worksheet and cell to which you want to export to. Here is an example:
. dtable weight bmi bpsystol i.agegrp, by(highbp, tests) export(myfile.xlsx, sheet(Dtable,replace) cell(B1) replace)
2. Alternatively, you can use putexcel to export tables, graphs, and text to a .xlsx file.
Example code:
. clear all . webuse nhanes2 . collect: regress bpsystol age weight . collect: regress bpsystol age weight c.age#c.weight . collect style cell, nformat(%9.3f) . collect label levels cmdset 1 "Model 1", modify . collect label levels cmdset 2 "Model 2", modify . collect style cell result[_r_ci], warn cidelimiter(`", "') sformat("[%s]") . collect layout (colname) (result[_r_b _r_se _r_ci]) (cmdset) . collect export myfile.pdf, replace
Exported PDF file:
Remark:
Alternatively, you may use the command putpdf collect to export a table or tables to a .pdf file.
Example code:
. clear all . webuse lbw (Hosmer & Lemeshow data) . regress bwt age lwt . estimates store model1 . regress bwt age lwt i.smoke i.ptl . estimates store model2 . etable, estimates(model1 model2) mstat(N) mstat(r2) column(estimate) cstat(_r_b , nformat(%9.2f)) cstat(_r_se , nformat(%9.2f)) showstars showstarsnote title(Tab 2: Estimation results of two linear regression models) export(myfile.html, replace)
Exported HTML file:
Remarks:
1. When you export to HTML files, the suboptions tableonly and append are supported. tableonly specifies that only the table be exported, rather than a complete HTML file created; this allows the file to be included in a master file. The suboption append allows additional tables to be appended to an existing file. The suboption tableonly is implied when specifying append.
2. Alternatively, you can create a Markdown file with Stata code and use the dyndoc command to process the Stata code and Markdown-formatted text to create an HTML file; please see the example below.
Example code:
. webuse nhanes2l, clear (Second National Health and Nutrition Examination Survey) . regress bpsystol age weight i.region . etable, title(Tab: Linear regression estimation results) export(myfile.md, replace)
Exported Markdown file myfile.md (opened in Do-file Editor):
We can generate an HTML file using the above .md file by running the following command within Stata:
. dyndoc myfile.md, replace
The created HTML file looks like this:
Remarks:
1. When exporting to .md files, you can use the suboption append within the export() option to append a table to an existing file.
2. For a more advanced example of exporting multiple tables to an HTML file using Markdown files and dyndoc, please refer to FAQ: How can I export multiple tables to one file?
Example code:
. clear all . webuse lbw, clear (Hosmer & Lemeshow data) . collect _r_b _r_ci _r_se _r_p: logistic low age lwt i.race smoke ptl ht ui . collect style cell, nformat(%9.3f) . collect style cell result[_r_ci], sformat([%s]) cidelimiter(",") . collect layout (colname) (result) . collect title "Logistic regression estimation results" . collect export myfile.tex, replace
The generated LaTex file myfile.tex (opened in TeXworks):
The PDF file compiled using myfile.tex:
Remarks:
1. When exporting to LaTeX files, you can use the tableonly option to export just the table, rather than creating a complete document. In other words, the “\begin{document}” and “\end{document}” commands will not be included in the resulting .tex file. When the tableonly option is specified, the resulting file cannot be compiled on its own, but it can be included in a master .tex file.
2. Additionally, you can use the option append to append a table to an existing file; the tableonly option is implied when specifying the append option.
Example code:
. clear all . webuse nhanes2 . table region, stat(fvfrequency sex race) stat(fvpercent sex race) sformat(%s%% fvpercent) . collect style header result, level(hide) . collect style row stack, nodelimiter . collect layout (var#result) (region) . collect title "Table: Frequency table of sex and race by region" . collect export myfile.txt, replace
Exported text file myfile.txt (open in Do-file Editor):
Remark:
When exporting to .txt files, you can append a table to an existing file by specifying the append option.
Example code:
. clear all . sysuse auto (1978 automobile data) . dtable length mpg turn i.rep78, by(foreign) nformat(%9.2f mean sd) title(Table 1: Descriptive statistics) export(myfile.smcl, replace)
Exported SMCL file (opened in Stata Viewer):
Remark:
When exporting to .smcl files, you can use the suboption append within the export() option to export additional tables to the same file.
If you are interested in reading an overview of Stata’s reporting features, please visit our reporting feature page.