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

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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 

 

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

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 

 

Miracle
Barite | Level 11

Thanks @PeterClemmensen  and @Kurt_Bremser  for your quick help 😊

Miracle
Barite | Level 11

Hi @PeterClemmensen.
Thanks but it doesn't work for current case.
How do we do achieve given the current case please?

Kurt_Bremser
Super User

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.

Miracle
Barite | Level 11

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.

CFC_SAS_Communities_400x225.jpg

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

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
  • 6 replies
  • 4076 views
  • 6 likes
  • 3 in conversation