DATA Step, Macro, Functions and more

create summary percentage tables by subsets of one variables.

Reply
Occasional Contributor
Posts: 18

create summary percentage tables by subsets of one variables.

[ Edited ]

Updated

 

Hi! Suppose I have dataset as followings

 

ID SEX    RACE                                      AGE 

1   F       Non-hispanic White                 5-15

2   M      Asian/Other                              5-15

3   M      Asian/Other                             15-20

4   F       Hispanic                                  15-20

5   M      Hispanic                                  15-30 

6   M     Non-hispanic Black                  25-30

 

I want to create a table as following 

 

 

5-15

15-20

25-30

Sex

 

 

 

Male

50%

66.6%

100%

Female

50%

33.3%

0

Race

 

 

 

 Non-Hispanic White

50%

0

0

Non-Hispanic Black

0

0

100%

Hispanic

0

66.6%

0

Asian/Other

50%

33.3%

0

 

I know how to use proc sql without sub-section age group but is there a way to aggregate all age sections in one step? Thanks!

Trusted Advisor
Posts: 1,270

Re: create summary percentage tables by subsets of one variables.

[ Edited ]
Posted in reply to Viveme789

Hi,

 

Please try this.

 

proc freq data=have order=data;
table sex*age/norow nocum nofreq nopercent ;
run;

Occasional Contributor
Posts: 18

Re: create summary percentage tables by subsets of one variables.

Thank you! I just updated my question. This problem is after running your code there is still a "total N" column and row. I need to generate all percentage tables using all variables with age, so my output need to be a dataset or table for future modification. 

Super User
Posts: 12,994

Re: create summary percentage tables by subsets of one variables.

Posted in reply to Viveme789

A report table could look something like:

proc tabulate data=have;
   class age sex race ;
   table sex race ,
         age=''*colpctn=''
         /misstext='0'
         ;
run;

add other categorical variables to the class and table.

 

Note that this will only work if all records have values for all of the variables as tabulate by default excludes records with any missing values for the variables on a class statement OR add /missing to the Class statement to get missing treated as a category.

Super User
Posts: 22,820

Re: create summary percentage tables by subsets of one variables.

Posted in reply to Viveme789
If this is for a report, consider using PROC TABULATE instead. You have more control over the output there, including percentages.
Ask a Question
Discussion stats
  • 4 replies
  • 135 views
  • 0 likes
  • 4 in conversation