There is a new command in Stata 13, putexcel, that allows you to easily export matrices, expressions, and stored results to an Excel file. Combining putexcel with a Stata command’s stored results allows you to create the table displayed in your Stata Results window in an Excel file.
A stored result is simply a scalar, macro, or matrix stored in memory after you run a Stata command. The two main types of stored results are e-class (for estimation commands) and r-class (for general commands). You can list a command’s stored results after it has been run by typing ereturn list (for estimation commands) and return list (for general commands). Let’s try a simple example by loading the auto dataset and running correlate on the variables foreign and mpg
. sysuse auto (1978 Automobile Data) . correlate foreign mpg (obs=74) | foreign mpg -------------+------------------ foreign | 1.0000 mpg | 0.3934 1.0000
Because correlate is not an estimation command, use the return list command to see its stored results.
. return list scalars: r(N) = 74 r(rho) = .3933974152205484 matrices: r(C) : 2 x 2
Now we can use putexcel to export these results to Excel. The basic syntax of putexcel is
putexcel excel_cell=(expression) … using filename [, options]
If you are working with matrices, the syntax is
putexcel excel_cell=matrix(expression) … using filename [, options]
It is easy to build the above syntax in the putexcel dialog. There is a helpful video on Youtube about the dialog here. Let’s list the matrix r(C) to see what it contains.
. matrix list r(C) symmetric r(C)[2,2] foreign mpg foreign 1 mpg .39339742 1
To re-create the table in Excel, we need to export the matrix r(C) with the matrix row and column names. The command to type in your Stata Command window is
putexcel A1=matrix(r(C), names) using corr
Note that to export the matrix row and column names, Read more…