<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Calculating percent from counts in PROC SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculating-percent-from-counts-in-PROC-SQL/m-p/852389#M336953</link>
    <description>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.</description>
    <pubDate>Thu, 05 Jan 2023 20:32:59 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2023-01-05T20:32:59Z</dc:date>
    <item>
      <title>Calculating percent from counts in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-percent-from-counts-in-PROC-SQL/m-p/852349#M336933</link>
      <description>&lt;P&gt;I'm running the following to get the number of males and females&lt;/P&gt;
&lt;PRE&gt;/*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;&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;
&lt;P&gt;and I get this&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="50%" height="30px"&gt;birth_gender&lt;/TD&gt;
&lt;TD width="50%" height="30px"&gt;Count&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%" height="30px"&gt;Female&lt;/TD&gt;
&lt;TD width="50%" height="30px"&gt;11&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%" height="30px"&gt;Male&lt;/TD&gt;
&lt;TD width="50%" height="30px"&gt;
&lt;P&gt;27&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;birth_gender&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Count&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Percent&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Female&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;11&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;28.9%&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Male&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;27&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;71.1%&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can do it with a data step but figured there's a way I can do it within this same section of code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jan 2023 18:50:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-percent-from-counts-in-PROC-SQL/m-p/852349#M336933</guid>
      <dc:creator>vegan_renegade</dc:creator>
      <dc:date>2023-01-05T18:50:59Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating percent from counts in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-percent-from-counts-in-PROC-SQL/m-p/852364#M336939</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;or probably better and easier, use PROC FREQ&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=tb_yearly_report1;
    tables birth_gender;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Jan 2023 19:59:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-percent-from-counts-in-PROC-SQL/m-p/852364#M336939</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-01-05T19:59:05Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating percent from counts in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-percent-from-counts-in-PROC-SQL/m-p/852382#M336950</link>
      <description>&lt;P&gt;You need two passes of the data with SQL. One to first calculate the total and a second to calculate the percentage.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC FREQ is more efficient.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=tb_yearly_report1;
table birth_gender / out=number_birth_sex outpct;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;A SQL solution would look something like this:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/301219"&gt;@vegan_renegade&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I'm running the following to get the number of males and females&lt;/P&gt;
&lt;PRE&gt;/*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;&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;
&lt;P&gt;and I get this&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="50%" height="30px"&gt;birth_gender&lt;/TD&gt;
&lt;TD width="50%" height="30px"&gt;Count&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%" height="30px"&gt;Female&lt;/TD&gt;
&lt;TD width="50%" height="30px"&gt;11&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%" height="30px"&gt;Male&lt;/TD&gt;
&lt;TD width="50%" height="30px"&gt;
&lt;P&gt;27&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;birth_gender&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Count&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Percent&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Female&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;11&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;28.9%&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Male&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;27&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;71.1%&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can do it with a data step but figured there's a way I can do it within this same section of code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jan 2023 20:02:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-percent-from-counts-in-PROC-SQL/m-p/852382#M336950</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-01-05T20:02:54Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating percent from counts in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-percent-from-counts-in-PROC-SQL/m-p/852389#M336953</link>
      <description>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.</description>
      <pubDate>Thu, 05 Jan 2023 20:32:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-percent-from-counts-in-PROC-SQL/m-p/852389#M336953</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-01-05T20:32:59Z</dc:date>
    </item>
  </channel>
</rss>

