BookmarkSubscribeRSS Feed
steve_citi
Calcite | Level 5
I would like to order my columns by the column totals, in descending order. Unfortunately it appears that the options available on the CLASS statement will either order your columns by the column headings(alphabetically), or by the frequency of the data (order=freq) but not by the actual total value.

I have tried order=formatted, but it did not appear to work. I am unsure if the "formatted" refers to the data or to the heading.

Can anoyone provide a solution to order the columns in descending order by the column totals?
1 REPLY 1
Cynthia_sas
SAS Super FREQ
Hi:
TABULATE will only put the ORDER on the CLASS statement. Normally, TABULATE wants a variable to be used for one thing -- setting categories or calculating statistics. What you want asks for a single variable to have 2 roles -- calculate the sum (use the variable for analysis) but then ALSO order by the calculated sum (use the variable as a CLASS variable).

One approach is to make one pass through the data and calculate the sum for ordering, and then to use that calculated sum for sorting purposes. In that case, ORDER=DATA on the CLASS statement should work.

There is another approach with PROC REPORT shown in this paper:

cynthia

[pre]
** One approach with PROC SQL and TABULATE;
** First query creates the sum of sales for just 3 regions.;
** Second query creates dataset for TABULATE.;
** (probably could have done this in one query with a remerge, but wanted to keep this simple);

proc sql;
create table work.sumsale as
select distinct region, sum(sales) as sumsale
from sashelp.shoes
where region in ('Asia', 'Pacific', 'Canada')
group by region;

create table work.ordsale as
select b.sumsale, a.region, a.subsidiary, a.product,
a.sales, a.inventory, a.returns
from sashelp.shoes as a,
work.sumsale as b
where a.region = b.region
order by sumsale descending;
quit;

ods listing close;
ods html file='c:\temp\showord.html' style=sasweb;
proc tabulate data=work.ordsale f=comma14.2;
class region /order=data;
class product;
var sales;
table region all,
product*sales=' '*sum all*sales=' '*(sum='Sales' pctsum*f=6.2);
keylabel sum = ' ';
run;
ods html close;
[/pre]

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1 reply
  • 1897 views
  • 0 likes
  • 2 in conversation