BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10
How to sort all datasets in a library using macros
10 REPLIES 10
SASKiwi
PROC Star

Sorting requires a data order. What data order do you want?

andreas_lds
Jade | Level 19

Do you want to sort all datasets using the same BY-statement?

 

BrahmanandaRao
Lapis Lazuli | Level 10
Yes
japelin
Rhodochrosite | Level 12

how about this code

 

%Macro MALLSORT(lib=,sortvar=);
  data _null_;
    set sashelp.vstable;
    where libname=upcase("&lib");
    call symputx(cats('ds',_N_),memname);
    call symputx('nobs',_N_);
  run;
  %do i=1 %to &nobs;
    proc sort data=&&ds&i;
      by &sortvar;
    run;
  %end;
%Mend;

Kurt_Bremser
Super User

No macro needed, only macro variables:

data _null_;
set sashelp.vtable;
where libname = upcase("&lib.");
call execute("
  proc sort data=&lib.." !! strip(memname) !! ";
  by &sortvar.;
  run;
");
run;
BrahmanandaRao
Lapis Lazuli | Level 10

How to call macro from above code 

suppose I have datasets in a library like 

data ds1;
set sashelp.class;
run;

 

data ds2;
set sashelp.cars;
run;

data ds3;
set sashelp.air;
run;

 

 

BrahmanandaRao
Lapis Lazuli | Level 10

No common variable for those datasets

class

cars

air

 

 

andreas_lds
Jade | Level 19

@BrahmanandaRao wrote:

No common variable for those datasets

class

cars

air

 

 


Don't use a macro at all, but proc sort directly. Will improve readability of the code as bonus effect.

Kurt_Bremser
Super User

Then you are obviously in the wrong thread, because you answered @andreas_lds 's question:

Do you want to sort all datasets using the same BY-statement?

with

Yes

so your new question doesn't belong here.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 2803 views
  • 0 likes
  • 5 in conversation