The Complete Guide to dtable in Stata: Master Descriptive Statistics Tables

The dtable command in Stata 18 allows you to easily create a table of descriptive statistics, commonly known as “Table 1”. It calculates summary statistics like means, standard deviations, frequencies etc. for continuous and categorical variables. The key features of dtable are:

The basic syntax of dtable is:

*Syntax of the dtable in Stata 
dtable [varlist] [if] [in] [, options]

Where varlist contains the variables for which you want summary statistics. The key options are:

Please note that dtable is Stata 18 command, and it will not work in older versions of Stata.

2. How to export to Word, Excel or LaTeX from dtable

A key feature of dtable is the ability to export the table to Word, Excel, PDF or LaTeX documents. This is done using the export() option. For example:

dtable varlist, export(table.docx)
This will export the table to a Word .docx file. The supported export formats are:

.docx – Microsoft Word
.xlsx – Microsoft Excel
.pdf – PDF
.tex – LaTeX
The export format is automatically determined from the file extension you specify. Some key points about export() :

Use replace to overwrite existing files
Excel exports will start at cell A1 by default
For Word export, use docx_options like noisily to see export commands
For Excel export, use excel_options like sheet() to specify worksheet
See the following examples of exporting to different formats:

 

// Export to Word

dtable varlist, export(table.docx) docx_options(noisily)

‎‏‏‎ ‎‎

// Export to Excel sheet named Results

dtable varlist, export(table.xlsx) excel_options(sheet(Results))

‏‏‎ ‎

// Export to PDF

dtable varlist, export(table.pdf)

‏‏‎ ‎

// Export to LaTeX file

dtable varlist, export(table.tex)

2.1 Exporting to Word

To export the dtable output to a Word .docx file, use the docx_options within export():

dtable varlist, export(table.docx) docx_options(noisily)

The noisily option will display the putdocx commands used to generate the Word file. This is helpful for understanding the underlying code. Other docx_options are:

By default, dtable produces a complete Word document with styles and formatting.

2.2 Exporting to Excel

To export to Excel, use the excel_options within export():

dtable varlist, export(table.xlsx) excel_options(sheet(Results) cell(B2))
This will export the table starting at cell B2 on a sheet named Results. Some useful excel_options are:

sheet(sheetname) – specify worksheet name
cell(B2) – starting cell for export
noisily – display putexcel commands
modify – allow modifying existing Excel file
dofolder(mydocs) – save putexcel commands to a do-file
By default, the exported Excel file will be opened in memory for modification. To prevent this, use:

dtable varlist, export(table.xlsx) excel_options(noopen)

2.3 Exporting to PDF

To export the table to a PDF file, use:

dtable varlist, export(table.pdf)
This will generate a PDF file containing the table. To see the underlying putpdf commands use:

dtable varlist, export(table.pdf) pdf_options(noisily) 
Other pdf_options like dofolder() can also be used similar to Word and Excel exports.

2.4 Exporting to LaTeX

To export the dtable output to a LaTeX .tex file, use:

dtable varlist, export(table.tex) 

This will generate a complete LaTeX file with the table. To append the table to an existing LaTeX file use:

dtable varlist, export(table.tex) tex_options(append)

The tex_options work similar to other export formats. This makes it very easy to integrate dtable output into LaTeX documents.