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

I'm running the following to get the number of males and females

/*Number of males and females*/
proc sql;
create table number_birth_sex as
select birth_gender 'Birth Gender', count(birth_gender) as Count
from tb_yearly_report1
group by birth_gender;
quit;

and I get this 

birth_gender Count
Female 11
Male

27

 

What I want to do is add another variable called "Percent" that calculates percent of Females (11/38) and Males (27/38) from the total. So ideally like this:

 

birth_gender Count Percent
Female 11 28.9%
Male 27 71.1%

 

I can do it with a data step but figured there's a way I can do it within this same section of code.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

You need two passes of the data with SQL. One to first calculate the total and a second to calculate the percentage. 

 

PROC FREQ is more efficient.

 

proc freq data=tb_yearly_report1;
table birth_gender / out=number_birth_sex outpct;
run;

A SQL solution would look something like this: 

 

proc sql;
    create table number_birth_sex as
    select sex 'Birth Gender', count(sex) as Count,
        calculated count / total as percent format=percent8.2
    from (select *, count(*) as total from sashelp.class)
    group by sex;
quit;

@vegan_renegade wrote:

I'm running the following to get the number of males and females

/*Number of males and females*/
proc sql;
create table number_birth_sex as
select birth_gender 'Birth Gender', count(birth_gender) as Count
from tb_yearly_report1
group by birth_gender;
quit;

and I get this 

birth_gender Count
Female 11
Male

27

 

What I want to do is add another variable called "Percent" that calculates percent of Females (11/38) and Males (27/38) from the total. So ideally like this:

 

birth_gender Count Percent
Female 11 28.9%
Male 27 71.1%

 

I can do it with a data step but figured there's a way I can do it within this same section of code.

 


 

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26
proc sql;
    create table number_birth_sex as
    select birth_gender label='Birth Gender', count(birth_gender) as Count,
        calculated count / sum(calculated count) as percent format=percent8.2
    from tb_yearly_report1
    group by birth_gender;
quit;

 

 

or probably better and easier, use PROC FREQ

 

proc freq data=tb_yearly_report1;
    tables birth_gender;
run;
--
Paige Miller
Reeza
Super User
When I run the SQL here I get an error that this type of nested calculations are not supported. I didn't think this was possible without a second pass or explicitly specifying the unique values to be counted.
Reeza
Super User

You need two passes of the data with SQL. One to first calculate the total and a second to calculate the percentage. 

 

PROC FREQ is more efficient.

 

proc freq data=tb_yearly_report1;
table birth_gender / out=number_birth_sex outpct;
run;

A SQL solution would look something like this: 

 

proc sql;
    create table number_birth_sex as
    select sex 'Birth Gender', count(sex) as Count,
        calculated count / total as percent format=percent8.2
    from (select *, count(*) as total from sashelp.class)
    group by sex;
quit;

@vegan_renegade wrote:

I'm running the following to get the number of males and females

/*Number of males and females*/
proc sql;
create table number_birth_sex as
select birth_gender 'Birth Gender', count(birth_gender) as Count
from tb_yearly_report1
group by birth_gender;
quit;

and I get this 

birth_gender Count
Female 11
Male

27

 

What I want to do is add another variable called "Percent" that calculates percent of Females (11/38) and Males (27/38) from the total. So ideally like this:

 

birth_gender Count Percent
Female 11 28.9%
Male 27 71.1%

 

I can do it with a data step but figured there's a way I can do it within this same section of code.

 


 

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!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 1550 views
  • 2 likes
  • 3 in conversation