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

Dear All, 

I want the below output table with count and % in separate rows instead of columns by row & columns variables. Percent (%)  below count (n) 

Data Have

IdAreaGenderRace
1112
2223
3312
422.
5123
6311
7222
8312
9111
10323

 

 

Output want

  GenderRace
 Total12123
Area total1055243
Area1321111
 30.040.020.050.025.033.3
Area2303011
 30.00.060.00.025.033.3
Area3431121
 40.060.0020.0050.0050.0033.33

Thank you in advance!  Akter 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@Akter wrote:

Hi ballardw,

Thank you so much😊. We are very close, only thing is that I see the variable name 'Area' appears in the output in 1st column immediate below the "All Areas" but i want '1'  (merged cell) with number and percent  next to it, exactly same as we have for Area 2 and 3 in column 1. (I don't want 'Area' again below the "Area Total' i want only '1' like 2 and 3 under "All Areas").

In addition, if i want to add another categorical variable (i.e age group) in column what should i do. Sorry I'm still trying to learn and have not used proc tabulate yet that much. 

This is very efficient way to doing without writing lots of code. I really appreciate your help. Again thank you for your patience! 


Second bit first:

Tabulate Table statement has dimensions which are separated by columns.

Table <some variables/statistics>   ,    <= one comma the first set has the row

         <some other variables or statistics>   <= after the comma is the columns

;

Or a "3 dimension" report:

Table <some variables>   ,    <= one comma the first set has the "page" or separate table

         <some other variables or statistics>  ,  <= after the comma is the Row

         <some other variables or statistics>   <= after the comma is the colums

 

;

 

So adding another variable after Race for example, would make that another category with associated columns.

 

You use the * to "nest" items:   Area*(n colpctn) means that each level of Area gets nested statistics.

You  could do  Area*Race*(n colpctn) to get the results nested in a row.

Items, variables or statistics, in parentheses are "grouped".

Experiment.

Warning: Tabulate does not allow having a statistic cross another statistic.

There are enough options that SAS has published a book, not one of the largest, on Proc Tabulate.

The documentation is your friend.

 

The syntax in the table of: variable=' '  is how to suppress the label of a variable or statistic, or place text in the quotes to override the default. So if you want the values of Area to appear but not the variable name/label:

 

Area=' '*(n   colpctn)

 

Because of the original way your "table" for output displayed it wasn't really clean.

 

 

 

 

View solution in original post

4 REPLIES 4
ballardw
Super User

@Akter wrote:

Dear All, 

I want the below output table with count and % in separate rows instead of columns by row & columns variables. Percent (%)  below count (n) 

Data Have

Id Area Gender Race
1 1 1 2
2 2 2 3
3 3 1 2
4 2 2 .
5 1 2 3
6 3 1 1
7 2 2 2
8 3 1 2
9 1 1 1
10 3 2 3

 

 

Output want

    Gender Race
  Total 1 2 1 2 3
Area total 10 5 5 2 4 3
Area1 3 2 1 1 1 1
  30.0 40.0 20.0 50.0 25.0 33.3
Area2 3 0 3 0 1 1
  30.0 0.0 60.0 0.0 25.0 33.3
Area3 4 3 1 1 2 1
  40.0 60.00 20.00 50.00 50.00 33.33

Thank you in advance!  Akter 


Many users here don't want to download Excel files because of virus potential, others have such things blocked by security software. Also if you give us Excel we have to create a SAS data set and due to the non-existent constraints on Excel data cells the result we end up with may not have variables of the same type (numeric or character) and even values.

 

Please pay attention to the appearance of your desired output. The message windows on this forum will reformat stuff and the "as shown" version is almost certainly not what you "want".

 

Without a LOT of data manipulation this is the closest I can get to your "want" as I think it was meant:

Data Have;
input Id	Area	Gender	Race;
datalines;
1	1	1	2
2	2	2	3
3	3	1	2
4	2	2	.
5	1	2	3
6	3	1	1
7	2	2	2
8	3	1	2
9	1	1	1
10	3	2	3
;

proc tabulate data=have;
   class area gender race/missing;
   table all='All areas' *n=' '  area*(n=' ' colpctn=' '),
         (all='Total' gender) race
        /row=float  misstext='0'
   ;
run;

Proc tabulate, while very flexible for somethings, will either not include any data that has missing for a CLASS variable or use the missing option to include it but a row/column heading with the missing value will appear.

 

 

 

 

Akter
Obsidian | Level 7

Hi ballardw,

Thank you so much😊. We are very close, only thing is that I see the variable name 'Area' appears in the output in 1st column immediate below the "All Areas" but i want '1'  (merged cell) with number and percent  next to it, exactly same as we have for Area 2 and 3 in column 1. (I don't want 'Area' again below the "Area Total' i want only '1' like 2 and 3 under "All Areas").

In addition, if i want to add another categorical variable (i.e age group) in column what should i do. Sorry I'm still trying to learn and have not used proc tabulate yet that much. 

This is very efficient way to doing without writing lots of code. I really appreciate your help. Again thank you for your patience! 

ballardw
Super User

@Akter wrote:

Hi ballardw,

Thank you so much😊. We are very close, only thing is that I see the variable name 'Area' appears in the output in 1st column immediate below the "All Areas" but i want '1'  (merged cell) with number and percent  next to it, exactly same as we have for Area 2 and 3 in column 1. (I don't want 'Area' again below the "Area Total' i want only '1' like 2 and 3 under "All Areas").

In addition, if i want to add another categorical variable (i.e age group) in column what should i do. Sorry I'm still trying to learn and have not used proc tabulate yet that much. 

This is very efficient way to doing without writing lots of code. I really appreciate your help. Again thank you for your patience! 


Second bit first:

Tabulate Table statement has dimensions which are separated by columns.

Table <some variables/statistics>   ,    <= one comma the first set has the row

         <some other variables or statistics>   <= after the comma is the columns

;

Or a "3 dimension" report:

Table <some variables>   ,    <= one comma the first set has the "page" or separate table

         <some other variables or statistics>  ,  <= after the comma is the Row

         <some other variables or statistics>   <= after the comma is the colums

 

;

 

So adding another variable after Race for example, would make that another category with associated columns.

 

You use the * to "nest" items:   Area*(n colpctn) means that each level of Area gets nested statistics.

You  could do  Area*Race*(n colpctn) to get the results nested in a row.

Items, variables or statistics, in parentheses are "grouped".

Experiment.

Warning: Tabulate does not allow having a statistic cross another statistic.

There are enough options that SAS has published a book, not one of the largest, on Proc Tabulate.

The documentation is your friend.

 

The syntax in the table of: variable=' '  is how to suppress the label of a variable or statistic, or place text in the quotes to override the default. So if you want the values of Area to appear but not the variable name/label:

 

Area=' '*(n   colpctn)

 

Because of the original way your "table" for output displayed it wasn't really clean.

 

 

 

 

Akter
Obsidian | Level 7

Thank you ballarw for explaining in details, very helpful. I really appreciate your time and effort. 😊

 

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
  • 4 replies
  • 930 views
  • 2 likes
  • 2 in conversation