A little Stata command to export tables to Excel
A Stata ADO file to export tables to Excel without the problems of Outreg2
I usually prefer to work in LaTeX, but from time to time I’m forced to share my results in Excel. In Stata, I used to use the Outreg2 command, but the output generated by this program has a weird format, and you always have to read some error messages in Excel.
Because of this, I decided to program a little piece of code to easily export my regression results to Excel. I’m not very creative, so I called my custom command export_tables. You can find the code at my Github page.
Syntax
export_tables, Models(string) Dec(real) Cell(string) USING(string) SHEET(string) [ Options ]
Where:
- Models() contains a list of the models you estimated, and saved with the “Estimates Store” command.
- Dec() indicates the number of decimal places of your model’s coefficients.
- Cell() indicates in which Excel’s cell the table is going to begin.
- Using() indicates where are you going to save the excel document, and Sheet() indicates how the sheet of the document will be named.
- In options, you can use Keep() to keep the coefficients associated with certain variables, Drop() to drop the coefficients associated with some variables, Stats() to report some e() statistics stored in your models and Dstats() helps you to set the number of decimal places associated with these statistics. Variables prints the name of the variables in your table, and label prints the variables' labels instead. Std to display standard errors below the coefficients, Pvalues to display the p-values of the coefficients below them, and See to print your output in the console as a table.
How to use the command
The idea behind the command is to run your regressions, store the results with estimates store, and then export the results to excel. You will get a table without a header, but you will see in every row the name of the variable, the coefficients of each model (with stars representing the significance level), the standard errors, and at the end, some statitics. After running this command, you can use putexcel to further modify your spreadsheet, adding headers, bold or italic text, borders, etc.
For the moment, this command only works with regression commands like reg, probit, ivreg2 or others, but doesn’t work with commands like heckman or others that display the results of 2 or more estimations at the same time.
Example
You can run the following example code:
sysuse auto
reg mpg rep78 trunk
est sto m1
reg turn length gear_ratio rep78
est sto m2
export_tables, models(m1 m2) dec(2) cell(A1) using("test.xlsx") sheet("sheet1") stats("N r2_a") label std see
You could also use a wilcard in the models, and write something like models(m*)
.