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 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!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 6 replies
  • 1543 views
  • 1 like
  • 5 in conversation