2017-12-18 300 views
4

我想在SAS數據步驟中對數據進行排序。我的意思是:proc的工作應該在數據步驟中完成。有沒有解決方法?如何使用SAS中的數據步驟排序數據步驟

+0

想知道你爲什麼想要做這樣的事情。 –

+0

剛剛有想法。沒有必要以複雜的方式來做這件事。但是,我想知道:出於熱情和焦慮,是否有可能在數據階段。我在谷歌搜索,但我沒有找到任何答案,所以我張貼在這裏。 – Saran

回答

3

如果您正在尋找僅限於數據步驟的解決方案,那麼可以使用hash table完成PROC SORT的工作。需要注意的是,你需要足夠的記憶來做到這一點。

如果你想做一個簡單的排序,你會加載散列表ordered:'yes'選項,並將其輸出到一個新表。默認情況下,ordered:yes將按升序對數據進行排序。您也可以指定descending

簡單排序

data _null_; 

    /* Sets up PDV without loading the table */ 
    if(0) then set sashelp.class; 

    /* Load sashelp.class into memory ordered by Height. Do not remove duplicates. */ 
    dcl hash sortit(dataset:'sashelp.class', ordered:'yes', multidata:'yes'); 

     sortit.defineKey('Height');  * Order by height; 
     sortit.defineData(all:'yes'); * Keep all variables in the output dataset; 

    sortit.defineDone(); 

    /* Output to a dataset called class_sorted */ 
    sortit.Output(dataset:'class_sorted'); 
run; 

去欺騙

要刪除重複,做同樣的操作,除了刪除multidata選項。在下表中,觀察值(8,9)和(15,16)是相互重複的。觀察結果9和16將被消除。

data _null_; 

    /* Sets up PDV without loading the table */ 
    if(0) then set sashelp.class; 

    /* Load sashelp.class into memory ordered by Height. Do not keep duplicates. */ 
    dcl hash sortit(dataset:'sashelp.class', ordered:'yes'); 

     sortit.defineKey('Height');  * Order by height; 
     sortit.defineData(all:'yes'); * Keep all variables in the output dataset; 
    sortit.defineDone(); 

    /* Output to a dataset called class_sorted */ 
    sortit.Output(dataset:'class_sorted'); 
run; 
1

有使用proc ds2的解決方案。

/*Just prepare dataset, because DS2 responds with an error on libraries like sashelp. */ 
data sql_prep; 
set sashelp.class; 
run; 

/*Delete test dataset before ds2 func, to avoid errors*/ 
proc datasets nodetails nolist; 
delete test; 
run; 

proc ds2; 

data test; 
    method run(); 
     set {select * from sql_prep order by Weight}; 
    end; 
enddata; 
run; 
quit; 

更多info關於sashelp庫的ds2錯誤。

Appendix轉換爲ds2文檔,關於ds2中的sql。

+0

如果這是有效的,那麼我想'dosubl'也是公平的遊戲...... – user667489

2

斯圖打我,但前提是你的數據集包含一個唯一的密鑰,並且可以適合在內存中的整個事情,你可以使用一個哈希排序,如:

data _null_; 
    if 0 then set sashelp.class; 
    declare hash h(dataset:"sashelp.class",ordered:"a"); 
    rc = h.definekey("age","sex","name"); 
    rc = h.definedata(ALL:'yes'); 
    rc = h.definedone(); 
    rc = h.output(dataset:"class_sorted"); 
    stop; 
run; 

如果您確實要避免使用任何內置排序方法,特別愚蠢的方法是將整個數據集加載到一系列臨時數組中,使用手動編碼算法對數組進行排序,然後再次輸出:

https://codereview.stackexchange.com/questions/79952/quicksort-in-sas-for-sorting-datasets