HTML Output in Stata

A mix of these commands can be really helpful by generating HTML files as outputs.

I have started prefering using htput command by Lloren Quint for majority of my analysis. You can search for it using net sj 12-4 dm0066

htput htsummary – Best for simple summary tables

The htopen htput htsummary and related commands suffice for majority of my needs. I will show you a brief example below

  • htopen – opens a new HTML file
  • htput – writes raw HTML. Use it to put headings, table headings etc. Also used to add graphs
  • htsummary – creates one-way and two-way summary tables, with tests of hypothesis.
    • each htsummary command add a new row variable
    • by default variable is treated as continuous, unless you specify freq option
    • Default reporting is mean (SD) and N (col %) – change using median, and row options.
    • The tables need to have an first and a last row. These are applied using head and close options to htsummary
    • Test of hypothesis are applied using test. Highlight rows where p < 0.05 using color(0.05)
    • Chang
  • htlog – pushes raw stata output to the html file – useful for corr, regress, logit, svy, ci, etc
  • htclose – closes the html file
cd "E:/"
cap mkdir temp

sysuse "nlsw88.dta", clear
cd "E:/temp"
local analysisTime : di %td_CY-N-D  date("$S_DATE", "DMY") " $S_TIME"


capture htclose
htopen using "results.html", replace


htput <h1>Analysis Results as on `analysisTime'</h1>

* htlog
htlog des

*htsummary - ONE WAY
htput <h2> Table 1 - Background characteristics One way summary table</h2>

htsummary age		 , 	   format(%8.1f) head 
htsummary race     	 , freq    format(%8.1f)
htsummary married	 , freq    format(%8.1f) rowtotal
htsummary smsa		 , freq    format(%8.1f) rowtotal
htsummary grade 	 , median  format(%8.1f)
htsummary hours 	 , median  format(%8.1f)
htsummary ttl_exp 	 , median  format(%8.1f)
htsummary tenure 	 , median  format(%8.1f)
htsummary wage	 	 , median  format(%8.1f)
htsummary collgrad 	 , freq    format(%8.1f) close

*htsummary - Comparison of participanst with and without College Graduation  - TWO WAY Table
htput <h2> Table 2 - Two way summary table</h2>

htsummary age		 collgrad , 	    format(%8.1f) head 
htsummary race     	 collgrad , freq    format(%8.1f)
htsummary married	 collgrad , freq    format(%8.1f) rowtotal
htsummary smsa		 collgrad , freq    format(%8.1f) rowtotal
htsummary grade 	 collgrad , median  format(%8.1f)
htsummary hours 	 collgrad , 	    format(%8.1f)
htsummary ttl_exp 	 collgrad , 	    format(%8.1f)
htsummary tenure 	 collgrad , 	    format(%8.1f)
htsummary wage	 	 collgrad , 	    format(%8.1f)
htsummary collgrad 	 collgrad , freq    format(%8.1f) close



*htsummary - Comparison of participanst with and without College Graduation  - TWO WAY Table WITH P VALUE
htput <h2> Table 3 - Two way summary table</h2>

htsummary age		 collgrad , 	    format(%8.1f) head      test   color(0.05)
htsummary race     	 collgrad , freq    format(%8.1f)           test   color(0.05)
htsummary married	 collgrad , freq    format(%8.1f) rowtotal  test   color(0.05)
htsummary smsa		 collgrad , freq    format(%8.1f) rowtotal  test   color(0.05)
htsummary grade 	 collgrad , median  format(%8.1f)           test   color(0.05)
htsummary hours 	 collgrad , 	    format(%8.1f)	    test   color(0.05)
htsummary ttl_exp 	 collgrad , 	    format(%8.1f)	    test   color(0.05)
htsummary tenure 	 collgrad , 	    format(%8.1f)	    test   color(0.05)
htsummary wage	 	 collgrad , 	    format(%8.1f)	    test   color(0.05)
htsummary collgrad 	 collgrad , freq    format(%8.1f) close     test   color(0.05)

* htlog - Correlations
htput <h2> Table 4 - Correlation between College grade and Wage </h2>
htlog: pwcorr age grade hours ttl_exp tenure wage , sig star(0.05)



* htput - Graphs
twoway (scatter grade wage) (lfit grade wage), name(scatterGradeWage, replace)
graph export scatterGradeWage.png, replace
htput <h2> Figure </h2>
htput <img src="scatterGradeWage.png" width="600">

* htlog - regression
htlog: regress wage  grade hours ttl_exp 

capture htclose
shell  "results.html", nowaitforexit

Analysis Results as on 2020-03-25 11:09:29

 
Contains data from C:\Program Files (x86)\Stata15\ado\base/n/nlsw88.dta 
  obs:         2,246                          NLSW, 1988 extract 
 vars:            17                          1 May 2016 22:52 
 size:        60,642                          (_dta has notes) 
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
              storage   display    value 
variable name   type    format     label      variable label 
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
idcode          int     %8.0g                 NLS id 
age             byte    %8.0g                 age in current year 
race            byte    %8.0g      racelbl    race 
married         byte    %8.0g      marlbl     married 
never_married   byte    %8.0g                 never married 
grade           byte    %8.0g                 current grade completed 
collgrad        byte    %16.0g     gradlbl    college graduate 
south           byte    %8.0g                 lives in south 
smsa            byte    %9.0g      smsalbl    lives in SMSA 
c_city          byte    %8.0g                 lives in central city 
industry        byte    %23.0g     indlbl     industry 
occupation      byte    %22.0g     occlbl     occupation 
union           byte    %8.0g      unionlbl   union worker 
wage            float   %9.0g                 hourly wage 
hours           byte    %8.0g                 usual hours worked 
ttl_exp         float   %9.0g                 total work experience 
tenure          float   %9.0g                 job tenure (years) 
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
Sorted by: idcode 

Table 1 – Background characteristics One way summary table

Variable Summary statistics
age in current year 1 39.2 (3.1) [2246]
race 2 white 1637 (72.9%)
black 583 (26%)
other 26 (1.2%)
married 2 single 804 (35.8%)
married 1442 (64.2%)
Total 2246 (100%)
lives in SMSA 2 nonSMSA 665 (29.6%)
SMSA 1581 (70.4%)
Total 2246 (100%)
current grade completed 3 12.0 (3.0) [2244]
usual hours worked 3 40.0 (5.0) [2242]
total work experience 3 13.1 (6.8) [2246]
job tenure (years) 3 3.8 (7.7) [2231]
hourly wage 3 6.3 (5.3) [2246]
college graduate 2 not college grad 1714 (76.3%)
college grad 532 (23.7%)

1: Arithmetic Mean (SD) [n]
2: n (column percentage)
3: Median (IQR) [n]

Table 2 – Two way summary table

Variable college graduate
not college grad college grad Total
age in current year 1 39.2 (3.1) [1714] 39.1 (3.0) [532] 39.2 (3.1) [2246]
race 2 white 1217 (71%) 420 (78.9%) 1637 (72.9%)
black 480 (28%) 103 (19.4%) 583 (26%)
other 17 (1%) 9 (1.7%) 26 (1.2%)
married 2 single 616 (35.9%) 188 (35.3%) 804 (35.8%)
married 1098 (64.1%) 344 (64.7%) 1442 (64.2%)
Total 1714 (100%) 532 (100%) 2246 (100%)
lives in SMSA 2 nonSMSA 550 (32.1%) 115 (21.6%) 665 (29.6%)
SMSA 1164 (67.9%) 417 (78.4%) 1581 (70.4%)
Total 1714 (100%) 532 (100%) 2246 (100%)
current grade completed 3 12.0 (1.0) [1712] 16.0 (2.0) [532] 12.0 (3.0) [2244]
usual hours worked 1 36.7 (10.1) [1711] 38.8 (11.7) [531] 37.2 (10.5) [2242]
total work experience 1 12.3 (4.8) [1714] 13.4 (3.9) [532] 12.5 (4.6) [2246]
job tenure (years) 1 5.7 (5.5) [1701] 6.8 (5.6) [530] 6.0 (5.5) [2231]
hourly wage 1 6.9 (5.3) [1714] 10.5 (6.3) [532] 7.8 (5.8) [2246]
college graduate 2 not college grad 1714 (100%) 0 (0%) 1714 (76.3%)
college grad 0 (0%) 532 (100%) 532 (23.7%)

1: Arithmetic Mean (SD) [n]
2: n (column percentage)
3: Median (IQR) [n]

Table 3 – Two way summary table

Variable college graduate p-value
not college grad college grad Total
age in current year 1 39.2 (3.1) [1714] 39.1 (3.0) [532] 39.2 (3.1) [2246] 0.7276 2
race 3 white 1217 (71%) 420 (78.9%) 1637 (72.9%) 0.0002 4
black 480 (28%) 103 (19.4%) 583 (26%)
other 17 (1%) 9 (1.7%) 26 (1.2%)
married 3 single 616 (35.9%) 188 (35.3%) 804 (35.8%) 0.8006 4
married 1098 (64.1%) 344 (64.7%) 1442 (64.2%)
Total 1714 (100%) 532 (100%) 2246 (100%)
lives in SMSA 3 nonSMSA 550 (32.1%) 115 (21.6%) 665 (29.6%) < 0.0001 4
SMSA 1164 (67.9%) 417 (78.4%) 1581 (70.4%)
Total 1714 (100%) 532 (100%) 2246 (100%)
current grade completed 5 12.0 (1.0) [1712] 16.0 (2.0) [532] 12.0 (3.0) [2244] < 0.0001 6
usual hours worked 1 36.7 (10.1) [1711] 38.8 (11.7) [531] 37.2 (10.5) [2242] 0.0001 2
total work experience 1 12.3 (4.8) [1714] 13.4 (3.9) [532] 12.5 (4.6) [2246] < 0.0001 2
job tenure (years) 1 5.7 (5.5) [1701] 6.8 (5.6) [530] 6.0 (5.5) [2231] < 0.0001 2
hourly wage 1 6.9 (5.3) [1714] 10.5 (6.3) [532] 7.8 (5.8) [2246] < 0.0001 2
college graduate 3 not college grad 1714 (100%) 0 (0%) 1714 (76.3%) < 0.0001 4
college grad 0 (0%) 532 (100%) 532 (23.7%)

1: Arithmetic Mean (SD) [n]
2: t-test
3: n (column percentage)
4: Chi-squared test
5: Median (IQR) [n]
6: Wilcoxon Rank Sum test

Table 4 – Correlation between College grade and Wage

 
             |      age    grade    hours  ttl_exp   tenure     wage 
-------------+------------------------------------------------------ 
         age |   1.0000  
             | 
             | 
       grade |  -0.0330   1.0000  
             |   0.1182 
             | 
       hours |  -0.0279   0.0871*  1.0000  
             |   0.1874   0.0000 
             | 
     ttl_exp |   0.1243*  0.1982*  0.2295*  1.0000  
             |   0.0000   0.0000   0.0000 
             | 
      tenure |   0.0737*  0.1228*  0.1608*  0.5766*  1.0000  
             |   0.0005   0.0000   0.0000   0.0000 
             | 
        wage |  -0.0362   0.3254*  0.1591*  0.2655*  0.1777*  1.0000  
             |   0.0866   0.0000   0.0000   0.0000   0.0000 
             | 

Figure

Table 4 – Regression

 
      Source |       SS           df       MS      Number of obs   =     2,240 
-------------+----------------------------------   F(3, 2236)      =    137.81 
       Model |  11590.2158         3  3863.40527   Prob > F        =    0.0000 
    Residual |  62683.9029     2,236  28.0339458   R-squared       =    0.1560 
-------------+----------------------------------   Adj R-squared   =    0.1549 
       Total |  74274.1187     2,239   33.172898   Root MSE        =    5.2947 
 
------------------------------------------------------------------------------ 
        wage |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval] 
-------------+---------------------------------------------------------------- 
       grade |   .6406903   .0453346    14.13   0.000      .551788    .7295925 
       hours |    .050071   .0109454     4.57   0.000     .0286068    .0715351 
     ttl_exp |   .2366114   .0253588     9.33   0.000     .1868822    .2863406 
       _cons |  -5.447002   .6971033    -7.81   0.000     -6.81404   -4.079965 
------------------------------------------------------------------------------ 

I find that tabout is especially useful for generating HTML output akin to the table x y , c(mean x ) stata command. To install it, download the tabout.txt file from http://tabout.net.au and save it as tabout.ado in your ‘ado/plus/t’ directory. The location of the directory can be sen by issuing the sysdir command.