2010-11-18 27 views
1

假設我有一個數據集:如何對SAS中的數據集進行排序,以便記錄交錯?

data animals; 
    input animal $ 
     group $ 
     control $; 
datalines; 
dog A c1 
dog B c1 
dog C c1 
dog D c2 
dog E c2 
dog F c2 
dog G c3 
dog H c3 
dog I c3 
; 
run; 

我想,要以這樣的方式所產生的數據集的樣子進行排序:

dog A c1 
dog D c2 
dog G c3 
dog B c1 
dog E c2 
dog H c3 
dog C c1 
dog F c2 
dog I c3 

我沒有看到任何PROC特殊選項排序會做一個「交替」排序,所以我可能必須將我的數據集的子集「BY控制」,然後在數據步驟中進行重組,以便它們交錯/交替。

任何想法?謝謝。

+0

你能解釋一下你想達到什麼嗎?我在第二個數據集中沒有看到任何模式。此外,您正在討論組合兩個數據集,而我只看到一個。 – Aniko 2010-11-18 16:44:49

+0

我改變了標題以反映我的一般問題。注意結果數據集中的第三列(我的問題中的第二個代碼框)是如何變爲c1,c2,c3並重復的。明白了嗎? – Banjer 2010-11-18 17:14:27

回答

7
proc sort data= animals out= animals2; 
    by control group; 
run; 

data animals2; 
    set animals2; 
    by control; 
    retain orderWithinControlType; 
    if first.control then orderWithinControlType = 1; 
    else orderWithinControlType +1; 
run; 

proc sort data= animals2 out= animals3; 
    by orderWithinControlType control; 
run; 

proc print data= animals3; 
run; 
+0

太好了,謝謝。我很高興這個解決方案不需要一堆瘋狂的子集。 – Banjer 2010-11-18 18:43:31

相關問題