The Complete Guide to dtable in Stata: Master Descriptive Statistics Tables
The Complete Guide to dtable in Stata: Master Descriptive Statistics TablesAttaullah Shah2023-08-22T19:38:03+05:00
1. What is dtable?
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:
Calculates common summary statistics for continuous variables like mean, SD, quantiles etc.
Calculates frequencies and percentages for categorical/factor variables
Can add p-values from tests comparing groups
Handles survey data using svyset information
Creates professional looking tables with titles, notes etc.
Exports the table to Word, Excel, PDF, HTML etc.
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:
by() – calculate statistics by groups
continuous() – choose statistics for continuous vars
factor() – choose statistics for categorical vars
export() – export table to Word, Excel etc.
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:
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:
append – append table to existing Word file
noisily – display the putdocx commands
replace – overwrite existing Word file
dofolder(mydocs) – save putdocx commands to a do-file
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: