Stata putexcel 按组汇总统计到 MS Excel

Stata putexcel summary statistics by group to MS Excel

我正在尝试让 Stata 命令 putexcel 为我提供连续变量的汇总统计信息,按分类变量分组,一个接一个,在同一工作中sheet。这应该是 repeated/looped 多年,每年都有自己的 sheet.

这带来了两个问题:

  1. 使用 bysort 并没有记录所有的组(也许它记录了但我不明白如何检索它们),所以看来我必须使用 if-每个级别的条件。因此,问题就变成了:

  2. 我的分类变量中有150个类别(组),都是半随机的4位数字。所以理想情况下,解决方案会自动检测组变量中的级别数,而不是我手写每个 if 语句的 150 个不同类别。

这是一个示例数据集:

clear
input ///
id income1996 income1997 employcode1996 employcode1997
1 500 400 3300 5000
2 500 300 5000 5000
3 900 1050 8830 8220
4 1000 1200 8220 3300
5 600 900 5000 8220
6 200 100 8220 5000
7 700 100 3300 3300
8 1000 100 5000 5000
end

这是我不太好的解决问题的尝试。我知道局部变量和手写一样(低)效率,但这是我最好的选择。

forval x=1996/1997 {
    local y=2
    local z=`y'+1
    local w=`y'+2   
    summarize income`x' if employcode`x'==3300
    putexcel A1=rnames A`z'=rscalars  using "C:\Users\emilbebri\Downloads\tmp\results.xlsx", sheet(year`x') modify colwise
    summarize income`x' if employcode`x'==5000 
    putexcel A`z'=rscalars  using "C:\Users\emilbebri\Downloads\tmp\results.xlsx", sheet(year`x') modify colwise
    summarize income`x' if employcode`x'==8220 
    putexcel A`w'=rscalars  using "C:\Users\emilbebri\Downloads\tmp\results.xlsx", sheet(year`x') modify colwise  
}

非常感谢您的回答,我的 rmi 劳损的右手也会如此! This guy seems to be on to something similar,然而,实际内容太远了,我不知道如何将这些知识转移到我的类似但种类不同的问题上。

更新:这里是罗伯托斯的回答,但修改后输出变得更紧凑,像这样:(最后一行没有均值和标准差的原因是因为示例数据中只有一个观察值类别)

这是生成它的代码。

forvalues x = 1996/1997 {

    local xlsrow = 2

    quietly levelsof employcode`x', local(ecodes)
    foreach ecode of local ecodes {

        // show on screen
        quietly display "Year `x', code `ecode'"
        quietly summarize income`x' if employcode`x' == `ecode'
        quietly display ""

        // save to MS Excel
        putexcel A`xlsrow'=("Code `ecode'") B`xlsrow'=rscalars ///
            A1=("discokode") B1=rnames  ///
            using "C:\Users\emilbebri\Downloads\tmp\results11.xlsx", ///
            sheet(`x') modify colwise

        // update MS Excel row
        local xlsrow = `xlsrow' + 1

    }

}

在更新后的代码中,forvalues 循环缺少 {}。另外,您没有使用 local employcode_tmp,而这似乎正是您的目标。

修复我提到的语法错误并删除你的第二个 quietly 应该会给你一些输出。但是,您的循环给出了重复的结果(每个就业代码有五个)。我不确定这是故意的。

一个完整的工作示例,以及我对您想要的内容的解释,是

clear
set more off

*----- example data -----

input ///
id income1996 income1997 employcode1996 employcode1997
1 500 400 3300 5000
2 500 300 5000 5000
3 900 1050 8830 8220
4 1000 1200 8220 3300
5 600 900 5000 8220
6 200 100 8220 5000
7 700 100 3300 3300
8 1000 100 5000 5000
end

*----- what you want -----

forvalues x = 1996/1997 {

    local xlsrow = 1

    quietly levelsof employcode`x', local(ecodes)
    foreach ecode of local ecodes {

        // show on screen
        display "Year `x', code `ecode'"
        summarize income`x' if employcode`x' == `ecode'
        display ""

        // save to MS Excel
        putexcel A`xlsrow'=("Code `ecode'") A`=`xlsrow'+1'=rscalars ///
            using "D:/Datos/rferrer/Desktop/test.xlsx", ///
            sheet(`x') modify colwise

        // update MS Excel row
        local xlsrow = `xlsrow' + 3

    }

}

结果:

同时检查 help statsby