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

Hello Everyone: I'm using proc tabulate to generate a table given below. Few things I would like to ask help for are:

1. Next to contract type an empty column is coming and I don't know why.

2. How can I sort the Month-Year column alphabetically, and

3. How can I add total after each month-year category. I manually created the TOTAL row to give an example on what I want my output to look like.

 

Thank you for any help. Here is my proc tabulate code I'm using:

 

proc tabulate data=ABC order=data;

 class Month-Year CONTRACT TYPE ORGANIZATION/ MISSING;

 table (Month-Year * CONTRACT TYPE )*N='',  

       ( ORGANIZATION all='Total');

 run;

 

   ORGANIZATION Total
   TYPE ATYPE B 
Month-YearContract Type    
Jul-20YEARLY    
 MONTHLY    
 WEEKLY    
 DAILY    
TOTAL     
Sep-19YEARLY    
 MONTHLY    
 WEEKLY    
 DAILY    
TOTAL     
Mar-20YEARLY    
 MONTHLY    
 WEEKLY    
 DAILY    
TOTAL     
May-20YEARLY    
 MONTHLY    
 WEEKLY    
 DAILY    
TOTAL     
Apr-20YEARLY    
 MONTHLY    
 WEEKLY    
 DAILY    
TOTAL     
Aug-20YEARLY    
 MONTHLY    
 WEEKLY    
 DAILY    
TOTAL     
Feb-20YEARLY    
 MONTHLY    
 WEEKLY    
 DAILY    
TOTAL     
1 ACCEPTED SOLUTION

Accepted Solutions
sasuser_sk
Quartz | Level 8

@ballardw  and @Reeza Thank you!!!!!. Your expertise are much appreciated.

 

Below code sorted month-year, removed the extra column and also added total after each contract type.

My date was time stamped. I created field named calculation=datepart(date) and applied MONYY. format to it which worked out perfectly. Awesome learning today.

 

proc tabulate data=ABC order=data;

 class Calculation CONTRACT TYPE ORGANIZATION / MISSING order=internal;

format Calculation monyy.;

 table Calculation *( CONTRACT TYPE all='Total'),  

       ( ORGANIZATION all='Total' );

        label Calculation='Month-Year' ;

 run;

View solution in original post

6 REPLIES 6
Reeza
Super User
That isn't real code....your variable names are not referenced properly.

You can use ALL to get the total, similar to how you get the overall total. Play around with different positioning to see what you want.

proc tabulate data=ABC order=data;

class Month-Year CONTRACT TYPE ORGANIZATION/ MISSING;

table (Month-Year * CONTRACT TYPE all = "Monthly Total" )*N='',

( ORGANIZATION all='Total');

run;


Just to clarify you want your Month-Year to be sorted alphabetically not in order of months in a calendar? Is your variable a SAS date or a character date? If it's a SAS date converting it to character would let it sort alphabetically but that would be very strange IMO.
sasuser_sk
Quartz | Level 8

Thanks for reply Reeza. I renamed variables while asking these questions.

 

My Month-Year is calculated from a date itself and yes I would like to order months as per calendar (sorry for incorrect working).

 

I tried adding all="total" at every level of variables but could find a perfect hit to add total after each Month-Year category.

 

I aslo do not know why I see an extra empty column after Contract Type.

Reeza
Super User

@sasuser_sk wrote:

Thanks for reply Reeza. I renamed variables while asking these questions.

 

My Month-Year is calculated from a date itself and yes I would like to order months as per calendar (sorry for incorrect working).

 

I tried adding all="total" at every level of variables but could find a perfect hit to add total after each Month-Year category.

 


Did you try the variant I showed in my response? Or something like ((monthYear*variable) all)

 

I don't know about the empty column, it may depend on the destination and style you're using, working off demo pseudo code without data doesn't really allow for testing and verification just guesses unless you happen to know the exact answer.

sasuser_sk
Quartz | Level 8

Yes I tried a few times (below) but got different order of table each time.

 

(Month-Year *Contract Type)all)*N='',
(organization all="Total" )

 

(Month-Year  all="Total" *Contract Type all="Total" )*N='',
(organization all="Total" )

 

((Month-Year  *Contract Type) all="Total" )*N='',
(organization all="Total" )

 

 

ballardw
Super User

@sasuser_sk wrote:

Thanks for reply Reeza. I renamed variables while asking these questions.

 

My Month-Year is calculated from a date itself and yes I would like to order months as per calendar (sorry for incorrect working).

 

I tried adding all="total" at every level of variables but could find a perfect hit to add total after each Month-Year category.

 

I aslo do not know why I see an extra empty column after Contract Type.


If you have an actual date use that. Apply a format to the date variable that has the interval you want. I would suggest MONYY7. The groups created by formats will be honored by Proc Tabulate to group multiple dates to a single month or whatever the format shows. BTW 2 digit years are just asking for problems in many places. The order would typically be the calendar order unless you specify an order option on the  CLASS statement that overrides the default.

The All typicall goes with the lower nested variable date*(contracttype All)  the All then totals the Contract type statistics.

 

The empty column you see comes from the N statistic with the label suppressed ( N=' '). You could either move the  N into a column position or use the table option / Row=float

Continuing with your sort of pseudocode:

proc tabulate data=ABC order=data;
   class datevar CONTRACT TYPE ORGANIZATION/ MISSING;
   format datevar monyy7.;
   table datevar*( CONTRACT TYPE All='Total' )*N='',  
         ( ORGANIZATION all='Total')
         / row=float
  ;
 run;
sasuser_sk
Quartz | Level 8

@ballardw  and @Reeza Thank you!!!!!. Your expertise are much appreciated.

 

Below code sorted month-year, removed the extra column and also added total after each contract type.

My date was time stamped. I created field named calculation=datepart(date) and applied MONYY. format to it which worked out perfectly. Awesome learning today.

 

proc tabulate data=ABC order=data;

 class Calculation CONTRACT TYPE ORGANIZATION / MISSING order=internal;

format Calculation monyy.;

 table Calculation *( CONTRACT TYPE all='Total'),  

       ( ORGANIZATION all='Total' );

        label Calculation='Month-Year' ;

 run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 1113 views
  • 0 likes
  • 3 in conversation