如何使用SAS中的数据步骤对数据进行排序
How to sort data using Data step in SAS
我想在 SAS 数据步骤中对数据进行排序。我的意思是:proc sort的工作应该在data step完成。有什么解决办法吗?
使用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;
关于 sashelp 库的 ds2 错误的更多info。
Appendix 进入 ds2 文档,大约 sql 进入 ds2.
如果您正在寻找仅数据步骤的解决方案,您可以使用 hash table 完成 PROC SORT
的工作。需要注意的是,您需要足够的内存才能执行此操作。
如果要进行简单排序,可以使用 ordered:'yes'
选项加载散列 table 并将其输出到新的 table。默认情况下,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
选项。在下面 table 中,观察 (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;
Stu 先于我,但前提是你的数据集包含唯一键,并且你可以将整个数据放入内存,你可以使用哈希排序,例如:
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
我想在 SAS 数据步骤中对数据进行排序。我的意思是:proc sort的工作应该在data step完成。有什么解决办法吗?
使用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;
关于 sashelp 库的 ds2 错误的更多info。
Appendix 进入 ds2 文档,大约 sql 进入 ds2.
如果您正在寻找仅数据步骤的解决方案,您可以使用 hash table 完成 PROC SORT
的工作。需要注意的是,您需要足够的内存才能执行此操作。
如果要进行简单排序,可以使用 ordered:'yes'
选项加载散列 table 并将其输出到新的 table。默认情况下,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
选项。在下面 table 中,观察 (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;
Stu 先于我,但前提是你的数据集包含唯一键,并且你可以将整个数据放入内存,你可以使用哈希排序,例如:
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