2011-07-11 32 views
3

假設我有一個SAS數據集,看起來像這樣:設置的重複數據集在SAS數據步

id x 
1 1234 
2 2345 
3 3456 

我需要的是在(說)2次讀取該數據集的新數據集,以指示一個新的變量,其「複製」是這樣的:

id x  rep 
1 1234 1 
2 2345 1 
3 3456 1 
1 1234 2 
2 2345 2 
3 3456 2 

重要的是,該數據以這樣的準確順序讀 - 整個初始數據集被讀出一次,然後再等

任何關於效率的想法在數據步驟中做到這一點的方法? (實際上,我的數據集很大,我需要多次閱讀,而且我想避免排序。)

我試過了,但新數據集中的觀察順序並不是我想要的:

data foo; 
set tmp; rep=1; output; 
set tmp; rep=2; output; 
run; 

回答

8

如果你想保持數據的一步,那麼這將工作,如你所述。

data foo; 
    set tmp (in=INA) tmp (in=INB); 
    if INA then REP=1; 
    if INB then REP=2; 
run; 
0

你可以嘗試使用視圖和PROC追加像這樣:

/* create view for rep=2 */ 

data rep2/view=rep2; 
set tmp; 
rep = 2; 
run; 

/* create dataset for rep=1 */ 

data foo; 
set tmp; 
rep = 1; 
run; 

/* append rep=2 to rep=1 dataset */ 

proc append base=foo data=rep2; 
run; 
1
data rep; 
    set tmp; 

    do rep = 1 to 2; /* or 3, or whatever */ 
    output; 
    end; 
proc sort; 
    by rep id; 
run; 

就是這樣。

+0

謝謝,我試圖避免排序。 – itzy

+0

我想知道,爲什麼?您是否嘗試保留原始記錄訂單(超出ID)或有一些性能考慮因素? – Anton

+1

這是一個巨大的文件,排序大約需要20分鐘。我需要做這個數千次,所以你是對的,這是關於性能。 – itzy