1. What is dtable?
- 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
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:
- 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:
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.3. dtable Options
dtable has many options to control the layout and content of the table. The most commonly used options are:
- by() – calculate statistics by groups
- continuous() – choose statistics for continuous variables
- factor() – choose statistics for categorical variables
- formatting options – control formatting of table items
- title() – add a table title
- note() – add table notes
3.1 by() Option
The by() option calculates statistics separately for each group defined by the grouping variable:
dtable age weight, by(sex)
This will calculate summary statistics for age and weight separately for males and females. Suboptions for by() control group statistics:
- tests – show p-values for comparisons between groups
- totals – show statistics for the overall sample
- missing – handle missing values as a separate group
For example:
dtable age weight, by(sex, tests totals)
Shows comparisons between sexes plus overall totals.
3.2 continuous() Option
The continuous() option chooses which statistics to display for continuous variables:
dtable age weight, continuous(mean sd median)
Shows the mean, SD and median for the continuous vars age and weight. You can select from:
- mean, sd, median
- min, max
- quantiles like q1, q3
- total
Statistics like count, geometric mean etc. are also available. Multiple continuous() options can be used.
3.3 factor() Option
The factor() option controls summary statistics for categorical variables:
dtable i.sex i.education, factor(fvpercent)
Shows column percentages for each sex and education level. The statistics are:
- fvfrequency – frequency counts
- fvpercent – column percentages
- fvproportion – proportions
Tests like Pearson’s chi-squared can also be added. Multiple factor() options are allowed.
3.4 Formatting Options
Options like nformat() and sformat() control formatting of results in the table:
dtable age weight, nformat(%3.1f) sformat("(%s)" mean)
Shows mean age and weight with 1 decimal place, and encloses the mean in parentheses. nformat() controls numeric formats and sformat() controls string formats.
Other formatting options are:
- varlabel/novarlabel – use variable labels
- fvlabel/nofvlabel – use value labels for categories
- halign() – horizontal alignment of cells
3.5 title() and note() Options
Add a title and notes to the table using:
dtable age weight, title(Table 1) note(Data from NHANES)
Style options like titlestyles() and notestyles() control formatting of titles and notes.
These options allow creating highly customized, publication-quality tables without any manual work!
4. Advanced dtable Usage
This section covers some more advanced usage of dtable like:
- Using survey data with svyset
- Defining composite results
- Controlling missing values
- Applying custom table styles
4.1 Using Survey Data
To use dtable with survey data, first svyset your data:
svyset [pweight=weightvar], strata(stratavar)
Then use the svy option with dtable:
dtable age weight, svy
This will compute statistics incorporating the survey design like weights, stratification etc. You can also specify subpopulations using the subpop() option.
4.2 Defining Composite Results
Composite results give control over how items are composed in table cells. For example, to show the ratio of mean and SD:
dtable age, define(mean_sd = mean sd)continuous(mean_sd) nformat(%3.2f)
The define() option creates a new result mean_sd composed from mean and sd. This is then shown in the table, and formatted with 2 decimal places.
4.3 Controlling Missing Values
By default, dtable handles missing values by listwise deletion. The nolistwise option changes this:
dtable age weight, nolistwise
Now all non-missing values will be used for each variable. This avoids deleting observations with partial missing data.
With by(), missing values can also be included as a separate group:
dtable age weight, by(sex, missing)
4.4 Applying Custom Table Styles
You can customize the look of your dtable using:
dtable age weight, style(mydtablestyle)
This applies the styles defined in mydtablestyle.dta to the table. Useful for custom fonts, colors, borders etc. The style files can be created using:
collect style create mydtablestyle collect style edit mydtablestyle
And then modified as needed. This gives full control over the table appearance.