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

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

 

Thanks,
Vahe
1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

7 REPLIES 7
AllanBowe
Barite | Level 11

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
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs
Vahe_Mar
Obsidian | Level 7

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

Thanks,
Vahe
AllanBowe
Barite | Level 11

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
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs
Vahe_Mar
Obsidian | Level 7

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;

Thanks,
Vahe
Vahe_Mar
Obsidian | Level 7
without second macro i have errors about "by"
Thanks,
Vahe
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

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
  • 7 replies
  • 1108 views
  • 1 like
  • 4 in conversation