Exporting Results from Stata to LaTeX

Regression Results

If you want to export a regression, just use esttab. Seriously, it’ll do anything, and has great help files.

Tables

  • Summary Statistics: sutex (tutorial)
  • Correlation Tables: corrtex (tutorial)

Custom tables

Custom tables can be trickier in Stata. I usually put things into a matrix, then output the matrix with outtable. Not always the best looking, but they work!

 

Here’s an example that makes a 3 row 4 column matrix of beta estimates for different dependent and independent variables.

local columns = 4
local independent_vars y1 y2 y3 y4
local rows = 3 
local dependent_vars var1 var2 var3
matrix results = J(`rows', `columns', 0)
 
local c = 1
foreach oucome in `independent_vars' {
    local colnames = "`colnames' `outcome'" 
 local r = 1 foreach variable in `my_vars' { if `r' == 1 { local rownames = "`rownames' name_for_this_row"
 } reg `outcome' `variable' `control' 
 * Store beta value matrix results[`r, `c']=_b[`variable'] 
 * Update row number
 local r = `r' + 1
 }
 * Update column number
 local c = `c' + 1 } * Export results matrix rownames results= `rownames' matrix colnames results= `colnames' matrix list results outtable using /path/for/results,  mat(results) replace /// 
 format(%9.3f) /// caption("your caption here") /// clabel(latex_label_here)

 

Simple Statistics

* First, calculate a statistic and store in a macro. 
sum var1, d
local statistic = r(max) 

* Then use this code to set the formatting. 
* This says "show it with 2 decimal places." 
* Display lets you check it. 

local cleaned_statistic: display %9.2f `statistic' 
display "`cleaned_statistic'" 

* Write to disk. 
* This syntax looks odd, but what it's doing is creating 
* a "handle" to a file, writing to that handle, 
* then closing the file when done. 

file open myfile using /path/to/results/statistic.tex, ///
 write text replace 
file write myfile "`cleaned_statistic'" 
file close myfile

Then in LaTeX, we just integrate with the \input{} command.

Further, the variable achieves a maximum value of \input{/path/to/results/statistic.tex}, much lower than perhaps we had thought!