Dear All,
Can I please seek your help on how to achieve this?
When group=ZZZ has the same freq to other groups (in this case group=XYZ and STU which will change depending on the time point of data extraction)  we are required to always present group=ZZZ as the last group before the next group with reduced freq (in this case, MNO 30).
Thanks a lot.
Let say the following is my input data.
| group | freq | 
| ABC | 100 | 
| CDE | 50 | 
| FGH | 45 | 
| IJK | 46 | 
| ZZZ | 43 | 
| XYZ | 43 | 
| STU | 43 | 
| MNO | 30 | 
How do I achieve the following the data? [PREVIOUS]
| group | freq | 
| ABC | 100 | 
| CDE | 50 | 
| FGH | 45 | 
| IJK | 46 | 
| XYZ | 43 | 
| STU | 43 | 
| ZZZ | 43 | 
| MNO | 30 | 
How do I achieve the following the data? [CURRENT]
| group | freq | 
| ABC | 100 | 
| CDE | 50 | 
| FGH | 45 | 
| IJK | 46 | 
| XYZ | 43 | 
| STU | 43 | 
| ZZZ | 43 | 
| MNO | 30 | 
| BBC | 30 | 
| CCN | 30 | 
| JKL | 30 | 
Like this?
data have;
input group $ 1-3 freq;
datalines;
ABC	100
CDE 50 
FGH 45 
IJK 46 
ZZZ	43 
XYZ	43 
STU	43 
MNO 30 
;
proc sql;
   create table want as
   select * from have
   order by freq desc, group = "ZZZ";
quit;
Result
group freq ABC 100 CDE 50 IJK 46 FGH 45 STU 43 XYZ 43 ZZZ 43 MNO 30
Like this?
data have;
input group $ 1-3 freq;
datalines;
ABC	100
CDE 50 
FGH 45 
IJK 46 
ZZZ	43 
XYZ	43 
STU	43 
MNO 30 
;
proc sql;
   create table want as
   select * from have
   order by freq desc, group = "ZZZ";
quit;
Result
group freq ABC 100 CDE 50 IJK 46 FGH 45 STU 43 XYZ 43 ZZZ 43 MNO 30
Given your group names, this is the quickest way:
proc sort
  data=have
  out=want
;
by descending freq group;
run;Thanks @PeterClemmensen  and @Kurt_Bremser  for your quick help 😊
Hi @PeterClemmensen.
Thanks but it doesn't work for current case.
How do we do achieve given the current case please?
You need to supply the source data for your second expected result. Since we have shown you how to do it, supply it as a data step with datalines.
Hi @Kurt_Bremser .
Thanks for your quick response.
It's OK now.
I managed to get the desired output by doing some slight adjustment in datastep and using proc sort.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.
