BookmarkSubscribeRSS Feed
SYLC
Calcite | Level 5

Hello.

 

I have create this code:

 

proc tabulate data=sashelp.cars;
class Origin DriveTrain;
table Origin * (DriveTrain all="Sub Total") all="Grand Total", all*n;
keylabel n='Number of each Drive Train';
run;

 

Which gives me this:

SYLC_0-1665163980170.png

But how do I sort the all, front and rear in descending order for each drivetrain like it does in excel pivot table below:

SYLC_1-1665164084539.png

Thanks in advance.

6 REPLIES 6
ballardw
Super User

Try this:

proc tabulate data=sashelp.cars;
   class Origin;
   class DriveTrain/ order=freq;
   table Origin * (all="Sub Total" DriveTrain ) all="Grand Total", all*n;
   keylabel n='Number of each Drive Train';
run;

I moved the All for the Drivetrain subtotal to have the total appear a the top if you want that behavior from the "pivot" as well.

 

Caution: the order=freq can get tricky with nested variables in complex tables.

 


@SYLC wrote:

Hello.

 

I have create this code:

 

proc tabulate data=sashelp.cars;
class Origin DriveTrain;
table Origin * (DriveTrain all="Sub Total") all="Grand Total", all*n;
keylabel n='Number of each Drive Train';
run;

 

Which gives me this:

SYLC_0-1665163980170.png

But how do I sort the all, front and rear in descending order for each drivetrain like it does in excel pivot table below:

SYLC_1-1665164084539.png

Thanks in advance.


 

SYLC
Calcite | Level 5

Thanks. I see what you mean by tricky. For the most part it has work but there are a few rows out of sequence.

 

I guess the alternative would be to create a table from scratch using the count function.

Reeza
Super User
Another alternative is to pipe the output to a table, sort using PROC SORT and then display using PROC REPORT.
Reeza
Super User
FYI - you should probably remove all from your pivot table as it double counts if you're using that Excel for anything.
ghosh
Barite | Level 11
proc tabulate data=sashelp.cars;
class Origin DriveTrain /order=freq;
table Origin * (DriveTrain all="Sub Total") all="Grand Total" , all=''*n 
  / Box='Origin and Drive Train';
keylabel n='Number of each Drive Train';
run;

ghosh_0-1665521210170.png

 

Ksharp
Super User
/*Very interesting question.
Hope SAS could have an option to handle this sitation.*/
proc freq data=sashelp.cars noprint ;
table Origin*DriveTrain/out=classdata ;
run;
proc sort data=classdata;by Origin descending count;run;
data classdata;
length DriveTrain $ 100;
 set classdata;
 by Origin;
n+first.Origin;
DriveTrain=repeat(' ',n)||DriveTrain;
drop n;
run;



proc tabulate data=classdata  format=best. ;
class Origin DriveTrain/order=data;
var count;
table Origin * (DriveTrain all="Sub Total") all="Grand Total", all*count="Number of each Drive Train"*sum="";
run;

Ksharp_0-1665574963303.png

 

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!

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
  • 6 replies
  • 799 views
  • 1 like
  • 5 in conversation