2013-07-23 51 views
2

我正在學習proc報告,並希望使用計算列做出簡單報告。程序報告計算

這裏是我的代碼:

proc report data = schools nowd; 
    columns school class maths science total; 
    define school  /group; 
    define class  /display; 
    define maths  /analysis; 
    define science /analysis; 
    define total  /computed; 

    compute total; 
     total = maths + science; 
    endcomp; 

run; 

這是我得到的輸出:

Schools Class  Maths Science  total 
Airport i   50   41   0 
Airport ii   92   53   0 
Airport iii   62   60   0 
Airport iv   66   61   0 
Amrut  i   84   58   0 
Amrut  ii   42   83   0 
Amrut  iii   53   64   0 
Amrut  iv   89  100   0 
Asia  i   42   74   0 
Asia  ii   48   91   0 
Asia  iii   75   76   0 
Asia  iv   46   84   0 

誰能請解釋一下我爲什麼我得到的總價值爲0,我相信可以在PROC REPORT中創建一個新列。這是什麼,我做錯了。

感謝和問候
阿米特

回答

2

PROC REPORT計算順序可能會造成混淆。基本上,你有一條日誌消息說'數學'和'科學'缺失,因爲它們沒有與報告中COMPUTE發生的那些列相關聯。您可以使用_C#_,其中#是列號,以便更容易地引用列。

另外,正如評論中指出的那樣,當訪問分析變量時,需要通過分析類型來引用它,所以weight.sum而不是weight。

proc report data = sashelp.class nowd; 
    columns name sex height weight bmi; 
    define name/group; 
    define sex  /display; 
    define height/analysis; 
    define weight/analysis; 
    define bmi/computed; 

    compute bmi; 
     bmi=_c4_/(_c3_**2); 
    endcomp; 

run; 
+0

是的,這是不實際的體重指數,因爲它不是在KG和M.只是一個簡單的例子:) – Joe

+0

我不相信這個問題是列的順序,因爲它們是分析變量(按照定義語句),它們將需要由複合名稱引用。如果TOTALS或BMI變量出現在COLUMNS語句中的這些變量之前,那麼在變量被知道之前嘗試計算總數會出現問題 –

+0

這是正確的。他的問題是他沒有訪問.sum(當你的表只是報告原始數據而沒有實際計算總和時,這很容易忘記)。 – orh

4

當分析變量用於計算統計量時,需要複合變量名稱。你可以分別將數學和科學分別作爲數學和科學。如果您已將這些變量作爲顯示變量保留,您也可以在不使用複合名稱的情況下引用它們。但是,如果您更改了COLUMNS語句中這些變量的順序,它可能會改變您的計算(只是需要考慮的一些因素)。但是,直接引用可以用於c3c4

proc report data = schools nowd; 
columns school class maths science total; 
define school  /group; 
define class  /display; 
define maths  /analysis; 
define science /analysis; 
define total  /computed; 

compute total; 
    total = maths.sum + science.sum; 
endcomp; 
run; 
0
proc report data = school out= xyz nowd; 
    columns schools class maths science total; 
    define schools  /group; 
    define class  /display; 
    define maths  /order; 
    define science /order; 
    define total  /computed; 

    compute total ; 
     if maths or science ne . then 
     total = maths + science ; 
    endcomp; 

run; 
+0

如果語句可以在非缺失值的情況下被刪除 –