How to use the Stata Table Command.
This blog post is written by Dr George Naufal , assistant research scientist at the Public Policy Research Institute (PPRI) at Texas A&M University and a research fellow at the IZA Institute of Labor Economics.
Tables are in every report, article, and book chapter. Stata offers a way to create and customize tables using the commands table
and collect.
We use the NLS data which is based on the National Longitudinal Survey of Young Women 14-26 years of age in 1968. This data is one of the many data sets used for training purposes. We first edit the labels of the variable collgrad
label variable collgrad "College Graduate"
label
define collgrad_label 0 "No" 1 "Yes"
label
values collgrad collgrad_label
Then
we use the command table
to create a simple table of frequencies of
those with a college graduate degree.
table
( ) (collgrad) (), statistic(frequency)
Where the three brackets before the comma could be empty, have variable names, or include a specific keyword.
If you would like to show the percent instead of frequencies, the code is:
table
( ) (collgrad) (), statistic(percent)
Say now, you would like the percent statistic to be a column rather than a row, the code becomes:
table
(collgrad) (), statistic(percent)
Now, we would like to add a lot more details to the table. We would like to show the frequency and percent of college graduates and show the mean and standard deviation of age and experience by college graduate or not. The code becomes (including formatting options to show the mean and standard deviation in one decimal):
table
( var ) ( collgrad ) (), statistic(frequency) statistic(percent)
statistic(mean age exper)
statistic(sd age exper) nformat(%9.1f
percent) sformat("%s%%" percent) nformat(%9.1f mean) nformat(%9.1f sd)
We
can edit the labels of age
and exper
to make the table cleaner
and run the code again.
label
variable age "Age (years)"
label
variable exper "Total Work Experience (year)"
Say you would like to show the mean and standard deviation in separate sections (instead of the variables), remove the var from the first bracket:
table
() ( collgrad ) (), statistic(frequency) statistic(percent) statistic(mean age exper) statistic(sd age exper) nformat(%9.1f percent)
sformat("%s%%" percent) nformat(%9.1f
mean) nformat(%9.1f sd)
You can export the table to any format (Word file, PDF, HTML, etc.)
collect
export "…\table1.docx", as(docx) replace
One
neat thing is that you can also do the table above using putdocx
; adding
the below to the export code gives you the full code to create the same table
using putdocx
collect
export "…\table1.docx", as(docx) dofile("…\commands.do",
replace) replace
Finally, you can do everything above through a point and click (including changing the borders, font, etc.) by following:
Menu
Statistics > Summaries, tables, and tests > Tables of frequencies, summaries, and command results
College Graduate
No | Yes | Total | |
Frequency | 10,996 | 2,552 | 13,548 |
Percent | 81.2% | 18.8% | 100.0% |
Age (years) Mean | 29.8 | 32.0 | 30.2 |
Standard deviation | 6.5 | 5.8 | 6.4 |
Total work experience Mean | 6.5 | 8.0 | 6.8 |
Standard deviation | 4.3 | 4.5 | 4.4 |
current grade completed Mean | 11.8 | 16.5 | 12.7 |
Standard deviation | 1.6 | 1.0 | 2.4 |
For an example of how to use the table editor, check this video
You can download all of the Stata used throughout this blog via this link: Stata Table Command.