2016-08-29 62 views
0

我想總結Stata中的數據。我有英國的地方政府代碼(例如E06000047)和一個數據集,這個數據集的級別更高(MSOA)。在Stata總結觀察

MSOA code MSOA name   Local authority code Net weekly income 
E02004297 County Durham 001 E06000047    480.00 
E02004290 County Durham 002 E06000047    540.00 
E02004298 County Durham 003 E06000047    520.00 
E02004299 County Durham 004 E06000047    430.00 
E02004291 County Durham 005 E06000047    400.00 

由於我對MSOA級別數據不感興趣,我想將數據歸納爲本地認證碼級別。我失敗的地方在於我無法用字符串數據進行計算。我想要做的是:

foreach identical "Local authority code" take the mean/median and 
store it in a var "means.local-auth" 

因此,我希望是這樣的:

Local authority code means.local-auth median.local-auth 
E06000047    474.00   480.00 
E06000048    486.00   485.00 
+1

請參閱'help collapse'。你需要的一切應該在那裏。 – ander2ed

+1

你不是在這裏發短信或限制字數。請避免使用諸如「我」這樣的「sth」和文盲等用法的縮寫。 –

+0

好問題顯示了一些代碼嘗試。請閱讀http://stackoverflow.com/help/mcve –

回答

1

對於這種簡單的總結的,沒有循環是必要的。這裏有一個egen用於生成變量的可重現示例,使用by()(它的參數可以是數字或字符串,實際上不需要是單個變量)。 tabdisp可以方便簡單的製表。

sysuse auto, clear 
egen mean_mpg = mean(mpg), by(rep78) 
egen median_mpg = median(mpg), by(rep78) 

tabdisp rep78, c(mean_mpg median_mpg) 

---------------------------------- 
Repair | 
Record | 
1978  | mean_mpg median_mpg 
----------+----------------------- 
     1 |   21   21 
     2 |  19.125   18 
     3 | 19.43333   19 
     4 | 21.66667  22.5 
     5 | 27.36364   30 
     . |  21.4   22 
---------------------------------- 

tabdisp rep78, c(mean_mpg median_mpg) format(%2.1f) 

---------------------------------- 
Repair | 
Record | 
1978  | mean_mpg median_mpg 
----------+----------------------- 
     1 |  21.0  21.0 
     2 |  19.1  18.0 
     3 |  19.4  19.0 
     4 |  21.7  22.5 
     5 |  27.4  30.0 
     . |  21.4  22.0 
---------------------------------- 
2

,因爲如果你的目標是產生一個報告,比如尼克的,或採取向在地方政府層面的分析的第一步它不是從你的問題清楚,這是一個使用collapse採取代碼的數據並將其減少到當地的權威級別。

. * Example generated by -dataex-. To install: ssc install dataex 
. clear 

. input str20(msoa_c msoa_n lac) float income 

      msoa_c    msoa_n   lac income 
    1. "E02004297" "County Durham 001" "E06000047" 480 
    2. "E02004290" "County Durham 002" "E06000047" 540 
    3. "E02004298" "County Durham 003" "E06000047" 520 
    4. "E02004299" "County Durham 004" "E06000047" 430 
    5. "E02004291" "County Durham 005" "E06000047" 400 
    6. end 

. format income %9.2f 

. drop msoa_c msoa_n 

. collapse (mean) mean_inc=income (median) med_inc=income, by(lac) 

. list 

    +--------------------------------+ 
    |  lac mean_inc med_inc | 
    |--------------------------------| 
    1. | E06000047  474.00 480.00 | 
    +--------------------------------+ 

.