Stata putexcel汇总统计到MS Excel

我试图让putexcel命令putexcel给我一个连续variables的统计,一个接一个的分类variables,在同一个工作表中分组。 这应该重复/循环几年,每年有自己的表。

这提出了两个问题:

  1. 使用bysort不logging所有的组(也许它确实,但我不明白如何检索它们),所以似乎我必须使用每个级别的if条件。 因此,问题变成:

  2. 在我的分类variables中有150个类别(组),都是半随机的4位数字。 因此,理想情况下,解决scheme将自动检测组中的水平数量,而不是为每个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 

这是我不是很好的尝试解决这个问题。 我知道局部variables就像写在手里一样高效,但这是我最好的select。

 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 } 

希望能够得到很大的回应,我的右手也是如此! 这个人似乎正在类似的东西 ,但是,实际的内容是太遥远了,我很遗憾,我将如何将这个知识转移到我的类似,但种类不同的问题。

更新:这里是罗伯托的答案,但修改,使输出变得更加紧凑,就像这样:(最后一行没有意思和SD的原因是因为示例数据在该类别中只有一个观察)

在这里输入图像说明

这里是生成它的代码。

 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