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.
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.
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;
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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.