<?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: QUERY - Get count of row as percentage of column total in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/QUERY-Get-count-of-row-as-percentage-of-column-total/m-p/419515#M26976</link>
    <description>&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Post example test data, in the form of a datastep using the code window (its the {i} above the post area)!&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Otherwise we are just guessing what you want.&amp;nbsp; So at a guess:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table WANT as
  select COUNTRY,
         CUSTOMER_PER_COUNTRY,
         (select count(distinct CUSTOMER) from HAVE) as TOTAL_CUSTOMERS,
         CUSTOMER_PER_COUNTRY / calculated TOTAL_CUSTOMERS * 100 as PCENT
  from   HAVE &lt;BR /&gt;  group by COUNTRY;
quit; &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 08 Dec 2017 10:20:55 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2017-12-08T10:20:55Z</dc:date>
    <item>
      <title>QUERY - Get count of row as percentage of column total</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/QUERY-Get-count-of-row-as-percentage-of-column-total/m-p/419514#M26975</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am a complete novice with SAS, but have experience with MySQL.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am investigating some lost customers and want to compare:&lt;/P&gt;&lt;P&gt;- The country split of those that were lost&lt;/P&gt;&lt;P&gt;- The average country split of all customers&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My plan is to take the customers table, count customers per country (which would be one column) and then show this as a percentage of total customers - IE what percent of total customers does each customer represent.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have been searching but can only find references to doing this in summary tables. Is it possible to get a column like this in query, to use in later queries?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Fri, 08 Dec 2017 10:03:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/QUERY-Get-count-of-row-as-percentage-of-column-total/m-p/419514#M26975</guid>
      <dc:creator>jagnew</dc:creator>
      <dc:date>2017-12-08T10:03:51Z</dc:date>
    </item>
    <item>
      <title>Re: QUERY - Get count of row as percentage of column total</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/QUERY-Get-count-of-row-as-percentage-of-column-total/m-p/419515#M26976</link>
      <description>&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Post example test data, in the form of a datastep using the code window (its the {i} above the post area)!&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Otherwise we are just guessing what you want.&amp;nbsp; So at a guess:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table WANT as
  select COUNTRY,
         CUSTOMER_PER_COUNTRY,
         (select count(distinct CUSTOMER) from HAVE) as TOTAL_CUSTOMERS,
         CUSTOMER_PER_COUNTRY / calculated TOTAL_CUSTOMERS * 100 as PCENT
  from   HAVE &lt;BR /&gt;  group by COUNTRY;
quit; &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Dec 2017 10:20:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/QUERY-Get-count-of-row-as-percentage-of-column-total/m-p/419515#M26976</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-12-08T10:20:55Z</dc:date>
    </item>
    <item>
      <title>Re: QUERY - Get count of row as percentage of column total</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/QUERY-Get-count-of-row-as-percentage-of-column-total/m-p/419527#M26979</link>
      <description>&lt;P&gt;Sorry,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the code I am now trying, following your reply:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;PROC SQL;
   CREATE TABLE WORK.QUERY_BASIC_COUNTRY_SPLIT AS 
   SELECT t1.COUNTRY, 
          /* CUSTOMERS */
            COUNT(DISTINCT t1.CUST_NBR) AS CUSTOMERS,
	    (SELECT COUNT(DISTINCT t1.CUST_NBR) FROM WORK.QUERY_1A) AS TOTAL_CUSTOMERS,
		CALCULATED CUSTOMERS / CALCULATED TOTAL_CUSTOMERS * 100 AS PCENT,
      FROM WORK.QUERY_1A t1
      GROUP BY t1.COUNTRY;
QUIT;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I include the TOTAL_CUSTOMERS or the PCENT lines the query runs so slow it does not complete.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be much appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Fri, 08 Dec 2017 11:10:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/QUERY-Get-count-of-row-as-percentage-of-column-total/m-p/419527#M26979</guid>
      <dc:creator>jagnew</dc:creator>
      <dc:date>2017-12-08T11:10:01Z</dc:date>
    </item>
    <item>
      <title>Re: QUERY - Get count of row as percentage of column total</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/QUERY-Get-count-of-row-as-percentage-of-column-total/m-p/419531#M26981</link>
      <description>&lt;P&gt;Left join the count on rather than sub-query then:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table WANT as
  select A.COUNTRY,
         A.CUSTOMER_PER_COUNTRY,
         B.TOTAL_CUSTOMERS,
         A.CUSTOMER_PER_COUNTRY / B.TOTAL_CUSTOMERS * 100 as PCENT
  from   (select COUNTRY,count(distinct CUSTOMER) as CUSTOMER_PER_COUNTRY from HAVE) A&lt;BR /&gt;  left join (select count(distinct CUSTOMER) as TOTAL_CUSTOMERS from HAVE) B&lt;BR /&gt;  on     1=1;
quit; &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Dec 2017 11:49:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/QUERY-Get-count-of-row-as-percentage-of-column-total/m-p/419531#M26981</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-12-08T11:49:14Z</dc:date>
    </item>
    <item>
      <title>Re: QUERY - Get count of row as percentage of column total</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/QUERY-Get-count-of-row-as-percentage-of-column-total/m-p/419543#M26984</link>
      <description>&lt;P&gt;Although it looks like you have what you desire, another option is to do it in "Summary Tables", and in the "Results" tab, tick the "Save results to a data set" tick box. That way, you'll end up with a dataset with your desired results that you can use in later programs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Tom&lt;/P&gt;</description>
      <pubDate>Fri, 08 Dec 2017 12:32:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/QUERY-Get-count-of-row-as-percentage-of-column-total/m-p/419543#M26984</guid>
      <dc:creator>TomKari</dc:creator>
      <dc:date>2017-12-08T12:32:28Z</dc:date>
    </item>
    <item>
      <title>Re: QUERY - Get count of row as percentage of column total</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/QUERY-Get-count-of-row-as-percentage-of-column-total/m-p/419572#M26987</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt; wrote:&lt;BR /&gt;&lt;P&gt;Left join the count on rather than sub-query then:&lt;/P&gt;&lt;PRE&gt;proc sql;
  create table WANT as
  select A.COUNTRY,
         A.CUSTOMER_PER_COUNTRY,
         B.TOTAL_CUSTOMERS,
         A.CUSTOMER_PER_COUNTRY / B.TOTAL_CUSTOMERS * 100 as PCENT
  from   (select COUNTRY,count(distinct CUSTOMER) as CUSTOMER_PER_COUNTRY from HAVE GROUP BY COUNTRY) A&lt;BR /&gt;  left join (select count(distinct CUSTOMER) as TOTAL_CUSTOMERS from HAVE) B&lt;BR /&gt;  on     1=1;
quit; &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/PRE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks RW9, I amended that slightly, adding a GROUP BY, and it worked.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Dec 2017 14:43:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/QUERY-Get-count-of-row-as-percentage-of-column-total/m-p/419572#M26987</guid>
      <dc:creator>jagnew</dc:creator>
      <dc:date>2017-12-08T14:43:31Z</dc:date>
    </item>
  </channel>
</rss>

