BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

Could any one let me know whether multiple datasets can be sorted in a sigle sort step?

For ex I want to sort datasets A, B and C by variables AA, BB and CC respectively...

Any help is much appreciated....

1 ACCEPTED SOLUTION

Accepted Solutions
ieva
Pyrite | Level 9

I think the easiet way would be just writing it together with variables like:

%sort_it(B, var1 descending var2);

%sort_id (AB, descending var1);

View solution in original post

5 REPLIES 5
ieva
Pyrite | Level 9

Not a single step, but a simple macro could make your code look nice

%macro

sort_it (name, variables);

proc sort data=&name;

by &variables;

run;

%mend;

%sort_it(A, var1);

%sort_it(B, var1 var2);

%sort_id (AB, var1);

Narasimha_Kulkarni
Calcite | Level 5

Hey ieva,

Thank you for code, one simple concern is order i.e. Ascending and Descending...

If I want to add descending order how we should modify the code above...

Thank you for reply

ieva
Pyrite | Level 9

I think the easiet way would be just writing it together with variables like:

%sort_it(B, var1 descending var2);

%sort_id (AB, descending var1);

Narasimha_Kulkarni
Calcite | Level 5

Hey,

It worked fine, Great help ....

Much appreciated...

data_null__
Jade | Level 19

You asked for it, sort 3 data set in one data step by different variables. Silly? I'll let you decide. 

data a(keep=aa) b(keep=bb) c(keep=cc);

   do _n_ = 10 to 1 by -1;     

      aa = _n_;

      bb = _n_;

      cc = _n_;

      do j = 1 to 3;

         output;

         end;

      end;

   run;

data _null_;

   if 0 then set a;

   declare hash a(dataset:'A',multidata: 'Y',ordered:'Y');

   a.definekey('AA');

   a.definedata(all:'Y');

   a.defineDone();

   a.output(dataset:'SA');

   a.clear();

   if 0 then set b;

   declare hash b(dataset:'B',multidata: 'Y',ordered:'Y');

   b.definekey('BB');

   b.definedata(all:'Y');

   b.defineDone();

   b.output(dataset:'SB');

   if 0 then set c;

   declare hash c(dataset:'C',multidata: 'Y',ordered:'Y');

   c.definekey('CC');

   c.definedata(all:'Y');

   c.defineDone();

   c.output(dataset:'SC');

   stop;

   run;

proc print data=SA;

   run;

proc print data=SB;

   run;

proc print data=SC;

   run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 813 views
  • 0 likes
  • 3 in conversation