Export Stata Results

I am sure that this is a problem that anyone who uses Stata for publications or reports has encountered: how do you conveniently export your output to something that can be analyzed using a scripting language or Excel?

For some commands, there are several ADO files (try findit tabout or findit outreg2 ). But how to export the output of the table command? Or anova results?

I would like to hear how Stata users solve this problem for certain teams or in general.

+4
source share
5 answers

After several experiments, I found a solution that works for me.

There are many ADOs that handle certain functions. I used outreg2 for regressions and tabout for summary statistics.

For simpler commands, it's easy to write your own programs to automatically save the results in plain text in a standard format. Here are a few of them that I wrote ... note that both of these results are displayed (for saving to a log file) and exported to text files. If you just want to save the text, you can get rid of the di and qui commands sum , tab , etc.:

 cap program drop sumout program define sumout di "" di "" di "Summary of `1'" di "" sum `1', d qui matrix X = (r(mean), r(sd), r(p50), r(min), r(max)) qui matrix colnames X = mean sd median min max qui mat2txt, matrix(X) saving("`2'") replace end cap program drop tab2_chi_out program define tab2_chi_out di "" di "" di "Tabulation of `1' and `2'" di "" tab `1' `2', chi2 qui matrix X = (r(p), r(chi2)) qui matrix colnames X = chi2p chi2 qui mat2txt, matrix(X) saving("`3'") replace end cap program drop oneway_out program define oneway_out di "" di "" di "Oneway anova with dv = `1' and iv = `2'" di "" oneway `1' `2' qui matrix X = (r(F), r(df_r), r(df_m), Ftail(r(df_m), r(df_r), r(F))) qui matrix colnames X = anova_between_groups_F within_groups_df between_groups_df P qui mat2txt, matrix(X) saving("`3'") replace end cap program drop anova_out program define anova_out di "" di "" di "Anova command: anova `1'" di "" anova `1' qui matrix X = (e(F), e(df_r), e(df_m), Ftail(e(df_m), e(df_r), e(F)), e(r2_a)) qui matrix colnames X = anova_between_groups_F within_groups_df between_groups_df P RsquaredAdj qui mat2txt, matrix(X) saving("`2'") replace end 

The question is how to get the output in Excel and format it. I found that the best way to import text output files from Stata to Excel is to combine them into one large text file and then import this single file using the Import Text File... function in Excel.

I merge the files by putting this Ruby code in the output folder and then running int from my Do file using qui shell cd path/to/output/folder/ && ruby table.rb :

 output = "" Dir.new(".").entries.each do |file| next if file =~/\A\./ || file == "table.rb" || file == "out.txt" if file =~ /.*xml/ system "rm #{file}" next end contents = File.open(file, "rb").read output << "\n\n#{file}\n\n" << contents end File.open("out.txt", 'w') {|f| f.write(output)} 

As soon as I import out.txt into my worksheet in Excel, I use a bunch of Excel built-in functions to collect the data into beautiful beautiful tables.

I use a combination of vlookup , offset , match , iferror and hidden columns with cell numbers and file names for this. The source .txt file is included in out.txt just above the contents of this file, which allows you to view the contents of the file using these functions, and then reference specific cells using vlookup and offset .

This Excel business is actually the hardest part of this system, and there really is no good way to explain it without showing you the file, although I hope you can get enough ideas to figure it out for yourself. If not, feel free to contact me through http://maxmasnick.com and I can get more information.

+7
source

I found that estout package is the most developed and has good documentation.

+6
source

In most tutorials, add a few packages where it would be great to have only one export of everything that Max offers above with its interesting method.

I personally use tabout for summary statistics and frequencies, estout for regression output, and try mkcorr for correlation matrices.

+3
source

I use estpost-- part of the estout package - to count the results from commands without an estimate. You can save them and easily export.

Here is an example:

 estpost corr varA varB varC varD, matrix est store corrs esttab corrs using corrs.rtf, replace 

Then you can add options to change formatting, etc.

+1
source

It has been a while, but I believe that you can issue a log command to capture the output.
log using c:\data\anova_analysis.log, text
[commands]
log close

0
source

Source: https://habr.com/ru/post/1300457/


All Articles