导出结果

Max*_*ick 5 statistics stata

我确信这是一个问题,任何使用Stata出版物或报告的人都会遇到:

如何方便地将输出导出为可以通过脚本语言或Excel解析的内容?

有一些ado文件可以为特定命令执行此操作.例如:

  • findit tabout
  • findit outreg2

但是导出table命令的输出怎么样?还是结果anova呢?

我很想知道Stata用户如何针对特定命令或一般情况解决此问题.

Max*_*ick 8

经过一段时间的实验,我找到了一个适合我的解决方案.

有多种ADO可以处理导出特定功能.我已经使用outreg2了回归和tabout摘要统计.

对于更简单的命令,可以轻松编写自己的程序,以自动将结果保存为标准格式的纯文本.这里有一些我写的......请注意,这些都显示结果(保存到一个日志文件),并将其导出到文本文件-如果你想只保存为文本,你可以摆脱的di的和quisum,tab等命令:

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
Run Code Online (Sandbox Code Playgroud)

那么问题是如何将输出输出到Excel并对其进行格式化.我发现将文本输出文件从Stata导入Excel的最佳方法是将它们连接成一个大文本文件,然后使用Import Text File...Excel中的功能导入该单个文件.

我通过将这个Ruby代码放在output文件夹中然后从我的Do文件中运行int来连接文件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)}
Run Code Online (Sandbox Code Playgroud)

一旦我out.txt在Excel中导入自己的工作表,我就会使用一堆Excel的内置函数将数据整合到漂亮漂亮的表中.

我使用的是组合的vlookup,offset,match,iferror,和隐藏列与细胞数量和文件名来做到这一点.源.txt文件包含在out.txt该文件的内容正上方,这使您可以使用这些函数查找文件的内容,然后使用vlookup和引用特定的单元格offset.

这个Excel业务实际上是这个系统中最复杂的部分,并且没有向你展示文件就没有好的方法来解释它,尽管你希望自己有足够的想法来解决这个问题.如果没有,请随时通过http://maxmasnick.com与我联系,我可以为您提供更多信息.


Tri*_*tan 7

我发现estout包是最发达的,并且具有良好的文档.