BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Batman
Quartz | Level 8

I'm trying to order the rows by the frequency column "Count" but this code doesn't seem to work.

 

PROC REPORT DATA=SURG_CD;                                              

  COL SURG_DX SURG_DX=DESC N;                                          

  DEFINE SURG_DX / GROUP 'ICD-10-PCS CODE';                            

  DEFINE DESC / GROUP 'DESCRIPTION' FORMAT=$ICDDESC.;                  

  DEFINE N / 'COUNT' STYLE(COLUMN)={TAGATTR='FORMAT:#,##0'} ORDER=FREQ;                         

RUN;                                                                   

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ
Tom:
It's a "feature". Once the order for product was used for the first group, that same order was used for the second group. That's why I pre-sorted and used ORDER=DATA.
Cynthia

View solution in original post

5 REPLIES 5
ballardw
Super User

From the documentation of the Define statement for Proc Report

 

ORDER=DATA | FORMATTED | FREQ | INTERNAL

orders the values of a group, order, or across variable according to the specified order.

 

Your N variable is not a group, order or across variable. If you define it as an ORDER variable you might get what you expect. Maybe. Depends on what you want. Since the N variable is going to be within two other group variables the order would be within those variable groups.

 

Tom
Super User Tom
Super User

Shouldn't the ORDER=FREQ option be on one of the GROUP variables?

Cynthia_sas
SAS Super FREQ

Hi:

  Thanks @ballardw for pointing out the PROC REPORT documentation. It's always a good idea to verify that what you want is available "out of the box". But there's always more than one way to do something with PROC REPORT. For example -- using some FAKE data:

Cynthia_sas_0-1676590037665.png

Of course you could have used MEANS or TABULATE or even PROC SQL to pre-summarize the data, but I like to give PROC REPORT a chance to do it. Of course, PROC SORT played a part. Notice how the second PROC REPORT differs from the first PROC REPORT (where I created WORK.PASS1):

data fakedata(keep=DX_Code Product Sales);
  set sashelp.shoes ;
  if region in ('United States', 'Canada', 'Western Europe') 
     then DX_code = 'DX 1111.11';
  else DX_Code = 'DX 2222.22';
run;

title; footnote;
  
PROC REPORT DATA=Fakedata out=pass1(drop=_break_); 
  title 'Before: Create PASS1 -- summarize the data';
  COLUMN DX_Code Product N;                                       
  DEFINE DX_Code/ GROUP 'ICD-10-PCS CODE';                            
  DEFINE Product / GROUP 'DESCRIPTION' ;                  
  DEFINE N / 'COUNT';                     
RUN;     

proc sort data=pass1 out=pass1;
  by DX_Code descending N;
run;

PROC REPORT data=pass1;
  title 'AFTER: Display in sorted order';
  COLUMN DX_Code Product N;                                       
  DEFINE DX_Code/ ORDER order=data 'ICD-10-PCS CODE';                            
  DEFINE Product / display 'DESCRIPTION' ;                  
  DEFINE N / display 'COUNT'; 
run; 

I figured you were using ODS EXCEL because of the use of TAGATTR, however, the destination wasn't relevant to the ordering, so I just took the default HTML destination to take the screen shots.

Cynthia

Tom
Super User Tom
Super User

Order=FREQ seems to work when applied in the right place.

So increasing DX_CODE and decreasing FREQ

data fakedata(keep=DX_Code Product Sales);
  set sashelp.shoes ;
  if region in ('United States', 'Canada', 'Western Europe') 
     then DX_code = 'DX 1111.11';
  else DX_Code = 'DX 2222.22';
run;
 
PROC REPORT DATA=Fakedata out=pass1(drop=_break_); 
  title 'Before: Create PASS1 -- summarize the data';
  COLUMN DX_Code Product N;                                       
  DEFINE DX_Code/ GROUP 'ICD-10-PCS CODE' ;                            
  DEFINE Product / GROUP 'DESCRIPTION' order=freq descending;                  
  DEFINE N / 'COUNT';                     
RUN;  

Results

  ICD-10-PCS
  CODE        DESCRIPTION         COUNT
  DX 1111.11  Boot                   18
              Slipper                18
              Sport Shoe             18
              Women's Dress          18
              Men's Dress            17
              Sandal                 16
              Men's Casual           17
              Women's Casual         17
  DX 2222.22  Boot                   34
              Slipper                34
              Sport Shoe             33
              Women's Dress          33
              Men's Dress            33
              Sandal                 33
              Men's Casual           28
              Women's Casual         28

 But why dies it place the Sandal (16) before the Causals (17) ?

It is because there are more total SANDALS across all "DX_CODE" values.

 

That probably will NOT be a problem for the actual problem since the DESCRIPTIONS should be 1 to 1 with the CODES.

Cynthia_sas
SAS Super FREQ
Tom:
It's a "feature". Once the order for product was used for the first group, that same order was used for the second group. That's why I pre-sorted and used ORDER=DATA.
Cynthia

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 908 views
  • 0 likes
  • 4 in conversation