<?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: how to use proc sql to count numbers and calculate percentage in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-proc-sql-to-count-numbers-and-calculate-percentage/m-p/348896#M80855</link>
    <description>&lt;P&gt;You would think SQL like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;select&lt;/P&gt;
&lt;P&gt;sum(field1) as sum1,&lt;/P&gt;
&lt;P&gt;count(field1) as count1,&lt;/P&gt;
&lt;P&gt;sum1/count1 as myaverage&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;from&lt;/P&gt;
&lt;P&gt;someplace&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;would work, but it never does.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You always end up repeating the expressions and not being able to use the aliases. Which is annoying.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;select&lt;/P&gt;
&lt;P&gt;sum(field1) as sum1,&lt;/P&gt;
&lt;P&gt;count(field1) as count1,&lt;/P&gt;
&lt;P&gt;sum(field1)/count(field1) as myaverage&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;from wherever&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, I know there is an AVG function.&lt;/P&gt;</description>
    <pubDate>Mon, 10 Apr 2017 21:06:36 GMT</pubDate>
    <dc:creator>HB</dc:creator>
    <dc:date>2017-04-10T21:06:36Z</dc:date>
    <item>
      <title>how to use proc sql to count numbers and calculate percentage</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-proc-sql-to-count-numbers-and-calculate-percentage/m-p/348827#M80823</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table new as select distinct 
patientid
,(case when group = "-1" then 1 else 0 end) as group_ind
from lib1.file2
;
quit;


proc sql;

create table want  as

select 


        count(distinct patientid) as total,

    count(distinct group_ind)&amp;gt;0))as id_ct_r,

       
calculated id_ct_r / total as percent_group format percent7.2
from new;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;i need to&amp;nbsp;calculate&amp;nbsp;the percentage. I&amp;nbsp;can do with datastep but would like to try proc&amp;nbsp;sql.&amp;nbsp;Above one has&amp;nbsp;an error message. Can anyone give advice or provide a better way to calculate the&amp;nbsp;rate, a percentage, how many with the value "-1" for the variable group (it is a character variable, and the value "-1" has a special meaning. All other values would be a eight-number value.&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Apr 2017 18:34:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-use-proc-sql-to-count-numbers-and-calculate-percentage/m-p/348827#M80823</guid>
      <dc:creator>Bal23</dc:creator>
      <dc:date>2017-04-10T18:34:34Z</dc:date>
    </item>
    <item>
      <title>Re: how to use proc sql to count numbers and calculate percentage</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-proc-sql-to-count-numbers-and-calculate-percentage/m-p/348832#M80827</link>
      <description>&lt;P&gt;Show example have and want data sets in the form of a datastep. Your first call to proc sql reduces your data to one record for each patientid recoded group combination .. which may not be what you want to start with.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Mon, 10 Apr 2017 18:56:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-use-proc-sql-to-count-numbers-and-calculate-percentage/m-p/348832#M80827</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-04-10T18:56:44Z</dc:date>
    </item>
    <item>
      <title>Re: how to use proc sql to count numbers and calculate percentage</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-proc-sql-to-count-numbers-and-calculate-percentage/m-p/348896#M80855</link>
      <description>&lt;P&gt;You would think SQL like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;select&lt;/P&gt;
&lt;P&gt;sum(field1) as sum1,&lt;/P&gt;
&lt;P&gt;count(field1) as count1,&lt;/P&gt;
&lt;P&gt;sum1/count1 as myaverage&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;from&lt;/P&gt;
&lt;P&gt;someplace&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;would work, but it never does.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You always end up repeating the expressions and not being able to use the aliases. Which is annoying.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;select&lt;/P&gt;
&lt;P&gt;sum(field1) as sum1,&lt;/P&gt;
&lt;P&gt;count(field1) as count1,&lt;/P&gt;
&lt;P&gt;sum(field1)/count(field1) as myaverage&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;from wherever&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, I know there is an AVG function.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Apr 2017 21:06:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-use-proc-sql-to-count-numbers-and-calculate-percentage/m-p/348896#M80855</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2017-04-10T21:06:36Z</dc:date>
    </item>
    <item>
      <title>Re: how to use proc sql to count numbers and calculate percentage</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-proc-sql-to-count-numbers-and-calculate-percentage/m-p/348898#M80856</link>
      <description>&lt;P&gt;It always helps to show some example data.&lt;/P&gt;
&lt;P&gt;So if you had this data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input group $ patientid $ @@;
cards;
1 1 1 2 1 3 
-1 1 -1 2
2 4 2 5
;&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;But it sounds you want something like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select count(distinct patientid) as N_patients
     , count(distinct case when group='-1' then patientid end) as N_minus_one
     , calculated N_minus_one / calculated N_patients as Percent_minus_one
  from have
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you would get 5,2 and 0.4 as the answers since there are 5 distinct PATIENTID values of which only 2 are in the '-1' group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Apr 2017 21:18:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-use-proc-sql-to-count-numbers-and-calculate-percentage/m-p/348898#M80856</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-04-10T21:18:56Z</dc:date>
    </item>
  </channel>
</rss>

