2017-12-18 304 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