How to sort data using data step in SAS

I want to sort the data in the SAS data step. What I mean: the proc sorting job should be done in the data step. Is there any solution?

+4
source share
3 answers

If you are looking for a data-only solution, you can do the work PROC SORTwith a hash table. The caveat is that this requires enough memory.

If you want to make simple sorting, you load the hash table using the option ordered:'yes'and display it in a new table. By default, ordered:yessorts data in ascending order. You can also specify descending.

Easy sorting

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;

De-scoring

, , 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;
+3

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

+2

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;

ds2 sashelp libs.

ds2 docs, sql ds2.

+1

Source: https://habr.com/ru/post/1690951/


All Articles