DATA Step, Macro, Functions and more

SORTING

Accepted Solution Solved
Reply
Contributor
Posts: 38
Accepted Solution

SORTING

Hi, how can I sort more then 2 datas same time by one variable which available in every data.

 


Accepted Solutions
Solution
‎11-24-2017 04:46 AM
Super User
Super User
Posts: 9,227

Re: SORTING

Your not really providing any information.  What datasets do you mean, do they have a common name, like ds1 ds2 which we can use, will they all have different names, is it all datasets in a given library?  If its all in a library then:

data _null_;
  set sashelp.vtable (where=(libname="YOURLIB"));
  call execute(cats('proc sort data=yourlib.',memname,'; by var1; run;'));
run;

View solution in original post


All Replies
Contributor
Posts: 53

Re: SORTING

Assuming you mean datasets (and that they have the same structure), you can do this by creating a view and sorting that - eg:

 

 

data one;
  x=1;y=2; output;
  x=1;y=3; output;
data two;
  x=4;y=6; output;
  x=0;y=7; output;
data sortview /view=sortview;
  set one two;
proc sort data=sortview out=final;
  by x;
run;

 

/Allan
Think you know SAS? Take the sasensei challenge..
Contributor
Posts: 38

Re: SORTING

Posted in reply to AllanBowe

Hi Allan Bowe, I mean all datas have same variable and need to sort before set

Contributor
Posts: 53

Re: SORTING

ok - I'm going to assume you didn't necessarily mean that the datasets had to be sorted at the same time (which is possible, using rsubmit or other techniques).  If you are happy for the datasets to be sorted in sequence, here is a simple macro solution:

 

data one;
x=1;y=2; output;
x=1;y=3; output;
data two;
x=4;y=6; output;
x=0;y=7; output;
data three;
x=6;y=6; output;
x=6;y=7; output;

%macro sortandappend(datasets=,var=x,outds=outds);
%local i;
%do i=1 %to %sysfunc(countw(&datasets));
  %let ds=%scan(&datasets,&i);
  proc sort data=&ds;
    by &var;
  run;
%end;
data &outds;
  set &datasets;
run;
%mend;

%sortandappend(datasets=one two three,var=x,outds=outds)

You provide a list of input datasets to the macro (one, two and three), set the variable to sort by, and the dataset to create.

/Allan
Think you know SAS? Take the sasensei challenge..
Contributor
Posts: 38

Re: SORTING

Posted in reply to AllanBowe

what you think about this ? 


%Macro VR(data, sort);
    proc sort data=&data;
         by &sort;
    run;
%mend vr;

 

data rm;
  length macro $100;
   set sashelp.vtable;
   where libname="WORK" and memname ~in("_SV", "__MVALLRAW");;
macro='%VR(' || strip(memname) || ", _PATIENT_DISPLAY_ID_FULL" || ");";

run;

%macro VV;

proc sql;
     select macro into:macro separated by " " from rm;
quit;
&macro;
%mend VV;
%VV;

Super User
Posts: 9,611

Re: SORTING

You can call the %vr macro with call execute() from the data step, the second macro is not needed.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 38

Re: SORTING

Posted in reply to KurtBremser
without second macro i have errors about "by"
Solution
‎11-24-2017 04:46 AM
Super User
Super User
Posts: 9,227

Re: SORTING

Your not really providing any information.  What datasets do you mean, do they have a common name, like ds1 ds2 which we can use, will they all have different names, is it all datasets in a given library?  If its all in a library then:

data _null_;
  set sashelp.vtable (where=(libname="YOURLIB"));
  call execute(cats('proc sort data=yourlib.',memname,'; by var1; run;'));
run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 253 views
  • 0 likes
  • 4 in conversation