2017-06-22 51 views
0

我目前工作的一個數據集在SAS這樣的:SAS - 減少行的NB與排名

人 - 字 - 日 - 等級

A - BLA - 2017年1月1日 - 1

A - BLA - 2017年2月1日 - 2

A - 測試 - 2017年3月1日 - 3

乙 - BLA - 2017年1月1日 - 1

乙 - 測試 - 2017年9月1日 - 2

Ç - BLA - 2017年3月1日 - 1

Ç - 測試 - 2017年5月1日 - 2

Ç - 測試 - 2017年7月1日 - 3

ç - SAS - 2017年8月1日 - 4

而且我想改變這樣的:

人 - 字 - 等級

--------甲BLA ----- 1

甲--------測試----- 2

乙------ - BLA ----- 1

乙--------測試----- 2

ç-------- BLA ----- 1

C -------- test ----- 2

C -------- sas ----- 3

排名是按日期分組的,由人分組。

我試圖用滯後功能,也與情況下的語法時(它的工作原理,但我必須爲每一種情況下做到這一點,我有94最大秩...不是真的很容易!)

所以我沒有找到一個很好的方式有最後一張桌子。

你能幫我嗎?

非常感謝

+0

請將您的示例數據作爲文本發佈在您的問題中,而不是圖像。 – user667489

+0

我改變了我的評論:)是否更好? – chloe4

+0

請描述您的轉換邏輯,並添加您嘗試過的代碼。 – Quentin

回答

0

雖然可以發佈您的嘗試代碼是在這個網站很好的協議,我不認爲它會幫助這裏lagcase when是不是要走的路。

基本上,您正在嘗試刪除重複的單詞條目並重新命名您的排名欄。您可以在單個數據集中實現此目的,利用first.處理,在使用by語句時可以使用該處理。

對於等級來說,最簡單的方法是在數據步驟移過記錄時從頭開始完全重建它。

data have; 
input people $ word $ date :ddmmyy10. rank; 
format date ddmmyy10.; 
datalines; 
A bla 01/01/2017 1 
A bla 02/01/2017 2 
A test 03/01/2017 3 
B bla 01/01/2017 1 
B test 09/01/2017 2 
C bla 03/01/2017 1 
C test 05/01/2017 2 
C test 07/01/2017 3 
C sas 08/01/2017 4 
; 
run; 

data want; 
set have (drop=rank date); /* remove rank as being rebuilt; date not required */ 
by people word notsorted; /* enable first. processing; notsorted option required as data not sorted by people and word */ 
if first.people then rank=0; /* reset rank when people value changes */ 
if first.word then do; 
    rank+1; /* increment rank by 1 for the first word (will ignore subsesquent duplicates) */ 
    output; /* output row */ 
end; 
run;