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

Hello

In this proc tabulate table I want to order  by column PCTSUM

The code is not working.

How can I order output by PCTSUM?

 

proc tabulate data=sashelp.cars;  
class Make/ORDER=PCTSUM;  
var Invoice;  
table Make='',Invoice=''*(n='Cars sold' PCTN='%Cars sold' SUM='Sum sales'  PCTSUM='PCT SUM sales')/box='Brand';
run; 
1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

You can't order by PCTSUM, but as @andreas_lds  notes you  can order by data (i.e. in order of the data  set).  So you could

  1. Run proc summary to do the aggregation work, and write the totals to data set NEED
  2. Sort NEED by sum of invoices (or descending sum of invoices)
  3. Run proc tabulate against data set need, using order=data.
proc summary data=sashelp.cars;
  class make;
  var invoice;
  output out=need (where=(_type_=1)) mean=invoice sum=invtotal;
run;
proc sort;by descending invtotal;run;

proc tabulate data=need  order=data;
  class make;
  var invoice;
  freq _freq_;
  table Make='',Invoice=''*(n='Cars sold' PCTN='%Cars sold' SUM='Sum sales'  PCTSUM='PCT SUM sales')/box='Brand';
run;

 

  1. The "where = (_type_=1)" option in the proc summary tells sas to only output the 1-way frequencies and sums.  Otherwise it would also include a single output record with total frequency and overall sum (_TYPE_=0). 
    edited addition:  proc summary also creates the automatic variables _FREQ_ which give number of observations contributing to the aggregate record.
  2. The first proc generates the INVTOTAL for sorting, and the MEAN=INVOICE to use as an analysis variable after sorting.
  3. The second proc tabulate uses the "order=data" option.  The statement "FREQ _FREQ_;"  tells sas to treat the observations in data set need as weighted observations.
    edited addition:  _FREQ_ is the automatic variable created in step 1.
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

3 REPLIES 3
andreas_lds
Jade | Level 19

Have a look at the documentation: the option "order" is defined as

ORDER=DATA | FORMATTED | FREQ | UNFORMATTED

 

So there is no way to specify pctsum as sort-order.

mkeintz
PROC Star

You can't order by PCTSUM, but as @andreas_lds  notes you  can order by data (i.e. in order of the data  set).  So you could

  1. Run proc summary to do the aggregation work, and write the totals to data set NEED
  2. Sort NEED by sum of invoices (or descending sum of invoices)
  3. Run proc tabulate against data set need, using order=data.
proc summary data=sashelp.cars;
  class make;
  var invoice;
  output out=need (where=(_type_=1)) mean=invoice sum=invtotal;
run;
proc sort;by descending invtotal;run;

proc tabulate data=need  order=data;
  class make;
  var invoice;
  freq _freq_;
  table Make='',Invoice=''*(n='Cars sold' PCTN='%Cars sold' SUM='Sum sales'  PCTSUM='PCT SUM sales')/box='Brand';
run;

 

  1. The "where = (_type_=1)" option in the proc summary tells sas to only output the 1-way frequencies and sums.  Otherwise it would also include a single output record with total frequency and overall sum (_TYPE_=0). 
    edited addition:  proc summary also creates the automatic variables _FREQ_ which give number of observations contributing to the aggregate record.
  2. The first proc generates the INVTOTAL for sorting, and the MEAN=INVOICE to use as an analysis variable after sorting.
  3. The second proc tabulate uses the "order=data" option.  The statement "FREQ _FREQ_;"  tells sas to treat the observations in data set need as weighted observations.
    edited addition:  _FREQ_ is the automatic variable created in step 1.
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ballardw
Super User

Or in a modification of @mkeintz use one pass through proc tabulate to create an output data set then a data step for some modification and then sort. You may use proc tabulate but with pre-calculated statistics you need to be careful not to ask for pcts or sums again. Or use proc report list output.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 2482 views
  • 3 likes
  • 4 in conversation