2015-05-11 20 views
1

我試圖讓Stata命令putexcel給出一個連續變量的摘要統計信息,該統計信息由一個接一個的分類變量分組在同一個工作表中。這應該重複/循環多年,每年都有自己的表格。Stata putexcel摘要按組向MS Excel統計

這就帶來兩個問題:

  1. 使用bysort不會記錄所有組(那也許是因爲它,但我不知道如何對它們進行檢索),如此看來我必須使用如果條件爲每個級別。因此,問題變成:

  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-strained右手也會很感激! This guy seems to be on to something similar,但是,實際內容太遠了,我很遺憾,因爲我會如何將這些知識轉移到我類似但不同種類的問題上。

更新:這裏是羅伯託的答案,但修改,使輸出變得更加緊湊,就像這樣:(最後一行沒有平均值和SD的原因是因爲示例數據在該類別中只有一個觀察值)

enter image description here

這裏是產生它的代碼。

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 

    } 

} 
+0

檢查'help levelsof'。此外,請考慮以_long_形式工作,而不是_wide_。這種方式大多數分析更容易。請參閱'幫助重塑'。 –

+0

謝謝,除非我無法完成它的工作。我更新了我的帖子,受到這個傢伙的啓發,但它不起作用,我不能完全理解爲什麼:http://www.andrewdyck.com/using-levelsof-to-future-證明循環通過階乘變量in-stata/ – emilBeBri

+0

您創建'本地employcode1996_tmp',但從不使用它。 –

回答

2

在你更新的代碼你正在爲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 

    } 

} 

結果:

enter image description here

檢查也help statsby

+0

這工作的魔力。我知道你是如何做到的,所以非常感謝。我根據你的代碼做了一個更加緊湊的輸出,我已經添加到了我原來的問題中。 – emilBeBri