<?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: Calculate percentages and counts with PROC SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-percentages-and-counts-with-PROC-SQL/m-p/617555#M180992</link>
    <description>&lt;P&gt;Why not use PROC FREQ which does the calculations by default, and doesn't have the drawback that missing values will cause the WRONG percentages to be calculated by PROC SQL (unless missing is a valid category). PROC FREQ gives you a choice how to handle missings, PROC SQL does not. PROC FREQ easily generalizes to two-way tables while PROC SQL does not. There are so many benefits to using PROC FREQ, I would not recommend PROC SQL for this purpose.&lt;/P&gt;</description>
    <pubDate>Wed, 15 Jan 2020 20:03:16 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2020-01-15T20:03:16Z</dc:date>
    <item>
      <title>How to calculate percentages and counts with PROC SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-percentages-and-counts-with-PROC-SQL/m-p/617541#M180982</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset (simplified example)&amp;nbsp;with only one variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data have;&lt;/P&gt;
&lt;P&gt;input class$;&lt;/P&gt;
&lt;P&gt;A&lt;/P&gt;
&lt;P&gt;A&lt;/P&gt;
&lt;P&gt;A&lt;/P&gt;
&lt;P&gt;B&lt;/P&gt;
&lt;P&gt;B&lt;/P&gt;
&lt;P&gt;C&lt;/P&gt;
&lt;P&gt;C&lt;/P&gt;
&lt;P&gt;C&lt;/P&gt;
&lt;P&gt;C&lt;/P&gt;
&lt;P&gt;D&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to calculate the percentages and counts of each variable in my dataset using PROC SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Output would be similiar to proc freq:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Desired output should be ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;SPAN&gt;class&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;SPAN&gt;Frequency&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;SPAN&gt;Percent&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;
&lt;P&gt;A&lt;/P&gt;
&lt;/TD&gt;
&lt;TD&gt;
&lt;P&gt;3&lt;/P&gt;
&lt;/TD&gt;
&lt;TD&gt;
&lt;P&gt;30&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;
&lt;P&gt;B&lt;/P&gt;
&lt;/TD&gt;
&lt;TD&gt;
&lt;P&gt;2&lt;/P&gt;
&lt;/TD&gt;
&lt;TD&gt;
&lt;P&gt;20&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;
&lt;P&gt;C&lt;/P&gt;
&lt;/TD&gt;
&lt;TD&gt;
&lt;P&gt;4&lt;/P&gt;
&lt;/TD&gt;
&lt;TD&gt;
&lt;P&gt;40&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;
&lt;P&gt;D&lt;/P&gt;
&lt;/TD&gt;
&lt;TD&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;/TD&gt;
&lt;TD&gt;
&lt;P&gt;10&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;How can I do this with PROC SQL?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jun 2021 17:33:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-percentages-and-counts-with-PROC-SQL/m-p/617541#M180982</guid>
      <dc:creator>Chris_Loke</dc:creator>
      <dc:date>2021-06-09T17:33:19Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate percentages and counts with PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-percentages-and-counts-with-PROC-SQL/m-p/617545#M180984</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/301077"&gt;@Chris_Loke&lt;/a&gt;&amp;nbsp; I copy pasted from online for you &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input class$;
cards;
A
A
A
B
B
C
C
C
C
D
;

proc sql;
create table want as
select class,count(class) as frequency,calculated frequency/(select count(*) from have) as pct format=percent.
from have
group by class;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Jan 2020 19:43:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-percentages-and-counts-with-PROC-SQL/m-p/617545#M180984</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-15T19:43:42Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate percentages and counts with PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-percentages-and-counts-with-PROC-SQL/m-p/617547#M180986</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;How about like that:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select a.class, count(a.class) as countclass,  (calculated countclass/b.all)*100 as percent
from
have as a, (select count(class) as all from have) as b
group by a.class
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;All the best&lt;/P&gt;&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2020 19:46:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-percentages-and-counts-with-PROC-SQL/m-p/617547#M180986</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-01-15T19:46:51Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate percentages and counts with PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-percentages-and-counts-with-PROC-SQL/m-p/617548#M180987</link>
      <description>Why not use PROC FREQ which does the calculations for you by default?</description>
      <pubDate>Wed, 15 Jan 2020 19:54:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-percentages-and-counts-with-PROC-SQL/m-p/617548#M180987</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-01-15T19:54:04Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate percentages and counts with PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-percentages-and-counts-with-PROC-SQL/m-p/617555#M180992</link>
      <description>&lt;P&gt;Why not use PROC FREQ which does the calculations by default, and doesn't have the drawback that missing values will cause the WRONG percentages to be calculated by PROC SQL (unless missing is a valid category). PROC FREQ gives you a choice how to handle missings, PROC SQL does not. PROC FREQ easily generalizes to two-way tables while PROC SQL does not. There are so many benefits to using PROC FREQ, I would not recommend PROC SQL for this purpose.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2020 20:03:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-percentages-and-counts-with-PROC-SQL/m-p/617555#M180992</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-01-15T20:03:16Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate percentages and counts with PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-percentages-and-counts-with-PROC-SQL/m-p/617571#M181004</link>
      <description>&lt;P&gt;Agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a quick example. I added missing values in the original data set and included the out= option to create new tables if you need them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
infile datalines missover;
input class$;
datalines;
A
A
A
B
B
C
C
C
 
C
 
D
 
;

run;

/*Ignore missing in frequency. Will show how many are missing in the output table but won't count them in the percentage*/
title "Ignore Missing in Frequency";
proc freq data=have;
	tables class /out=newtbl1(where=(class ne " ")) /*create a new table and remove the missing row from it*/
				  nocum /*remove cumulative freq*/;
run;

/*Include missing in frequency and will include missing values in the percentage*/
title "Include Missing in Frequency";
proc freq data=have;
	tables class /out=newtbl2 /*create a new table*/
				  missing /*remove cumulative freq*/
				  nocum /*include missing values in frequency*/;
run;
title;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HTML Results&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="results.jpg" style="width: 305px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/35357i0D6375A1B651F4BA/image-size/large?v=v2&amp;amp;px=999" role="button" title="results.jpg" alt="results.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;- Peter&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2020 20:44:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-percentages-and-counts-with-PROC-SQL/m-p/617571#M181004</guid>
      <dc:creator>Panagiotis</dc:creator>
      <dc:date>2020-01-15T20:44:02Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate percentages and counts with PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-percentages-and-counts-with-PROC-SQL/m-p/618205#M181314</link>
      <description>&lt;P&gt;Thanks!!!&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jan 2020 21:23:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-percentages-and-counts-with-PROC-SQL/m-p/618205#M181314</guid>
      <dc:creator>Chris_Loke</dc:creator>
      <dc:date>2020-01-17T21:23:44Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate percentages and counts with PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-percentages-and-counts-with-PROC-SQL/m-p/618207#M181316</link>
      <description>&lt;P&gt;Thank you for replying!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The thing is that I have ten different datasets for ten different years that I want to perform my counts on. I want join all the years togheter in to one table and the export it to excel.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If this is possible to do with Proc Freq I would be happy to learn it!&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jan 2020 21:29:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-percentages-and-counts-with-PROC-SQL/m-p/618207#M181316</guid>
      <dc:creator>Chris_Loke</dc:creator>
      <dc:date>2020-01-17T21:29:30Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate percentages and counts with PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-percentages-and-counts-with-PROC-SQL/m-p/618209#M181318</link>
      <description>&lt;P&gt;Both FREQ and SQL take only one input so it doesn't' matter which you use. Unless you create a view first and then run the SQL or FREQ on the view. Either way, FREQ would still be more efficient in terms of processing time, but you already have a solution so now it's most efficient to use that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/301077"&gt;@Chris_Loke&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you for replying!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The thing is that I have ten different datasets for ten different years that I want to perform my counts on. I want join all the years togheter in to one table and the export it to excel.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this is possible to do with Proc Freq I would be happy to learn it!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jan 2020 21:54:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-percentages-and-counts-with-PROC-SQL/m-p/618209#M181318</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-01-17T21:54:59Z</dc:date>
    </item>
  </channel>
</rss>

