Stata 18中使用collect合并表格并通过putdocx导出Word的问题
Stata 18 合并样本描述表问题
我之前在Stata 17制作了样本描述表,现在切换到Stata 18,需要合并两个表格:
- 第一个表格汇总样本筛选标准(
inclusion变量)及回归分析的纳入人数统计 - 第二个表格仅对纳入人群(
inclusion == 1)的性别与年龄信息进行汇总
我的目标是生成一个可通过putdocx导出至Word的单个表格,但使用collect combine命令后,合并后的变量顺序不符合预期。
原始最小可复现示例(MRE)
clear all input id inclusion age sex 1 1 35 0 2 1 56 1 3 0 78 1 3 1 43 1 4 0 3 0 5 1 40 0 6 1 5 0 7 0 55 1 8 1 31 0 9 1 26 0 10 0 34 1 11 0 45 1 12 0 67 0 end putdocx begin table () () (), statistic(mean inclusion) statistic(sd inclusion) statistic(min inclusion) statistic(max inclusion) nformat(%6.2f mean sd) collect layout (var) (result) collect label levels result mean "Mean" sd "SD" min "Min." max "Max.", modify putdocx pagebreak putdocx paragraph, style(Heading1) putdocx text ("Table 2: Sample description") putdocx collect table ( ) () () if inclusion == 1, /* */ statistic(mean age) statistic(sd age) statistic(min age) statistic(max age) /* */ statistic(mean sex) statistic(sd sex) statistic(min sex) statistic(max sex) collect layout (var) (result) collect label levels result mean "Mean" sd "SD" min "Min." max "Max.", modify putdocx collect putdocx save Tables, append
尝试的合并代码
* putdocx begin table () () (), statistic(mean inclusion) statistic(sd inclusion) statistic(min inclusion) statistic(max inclusion) nformat(%6.2f mean sd) collect layout (var) (result) collect label levels result mean "Mean" sd "SD" min "Min." max "Max.", modify collect rename Table table21 table ( ) () () if inclusion == 1, /* */ statistic(mean age) statistic(sd age) statistic(min age) statistic(max age) /* */ statistic(mean sex) statistic(sd sex) statistic(min sex) statistic(max sex) collect layout (var) (result) collect label levels result mean "Mean" sd "SD" min "Min." max "Max.", modify collect rename Table table22 collect combine Table2 = table21 table22, layout(right) style(right) collect preview
解决方法
要得到变量顺序为inclusion→age→sex的合并表格,需调整合并布局为垂直堆叠,并指定变量排列顺序:
clear all input id inclusion age sex 1 1 35 0 2 1 56 1 3 0 78 1 3 1 43 1 4 0 3 0 5 1 40 0 6 1 5 0 7 0 55 1 8 1 31 0 9 1 26 0 10 0 34 1 11 0 45 1 12 0 67 0 end * 创建第一个表格(inclusion统计) table () () (), statistic(mean inclusion) statistic(sd inclusion) statistic(min inclusion) statistic(max inclusion) nformat(%6.2f mean sd) collect layout (var) (result) collect label levels result mean "Mean" sd "SD" min "Min." max "Max.", modify collect rename Table table21 * 创建第二个表格(纳入人群的age和sex统计) table ( ) () () if inclusion == 1, /* */ statistic(mean age) statistic(sd age) statistic(min age) statistic(max age) /* */ statistic(mean sex) statistic(sd sex) statistic(min sex) statistic(max sex) collect layout (var) (result) collect label levels result mean "Mean" sd "SD" min "Min." max "Max.", modify collect rename Table table22 * 合并表格并指定垂直堆叠,调整变量顺序 collect combine Table2 = table21 table22, layout(stack) collect arrange var (inclusion age sex) // 强制指定变量显示顺序 collect preview * 导出到Word putdocx begin putdocx paragraph, style(Heading1) putdocx text ("Table 2: Sample description") putdocx collect Table2 putdocx save Tables, replace
关键说明
layout(stack):将两个表格垂直堆叠,把第二个表格的统计行追加到第一个表格下方,形成统一的单表格结构collect arrange var (inclusion age sex):强制指定变量的显示顺序,确保符合需求putdocx collect Table2:直接导出合并后的表格到Word文档
内容的提问来源于stack exchange,提问作者flxflks
相关产品推荐
相关产品推荐

