<?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 PROC SQL uses DISTINCT and GROUP by in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/How-PROC-SQL-uses-DISTINCT-and-GROUP-by/m-p/427129#M68427</link>
    <description>&lt;P&gt;I can't remember why exactly, must have had issues with it at some point in the past so now I totally avoid using where clauses on group by's and put them in subqueries or havings.&amp;nbsp; It may just be habit from my side.&amp;nbsp; Definately can't use use where for aggregates.&lt;/P&gt;</description>
    <pubDate>Fri, 12 Jan 2018 08:57:20 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2018-01-12T08:57:20Z</dc:date>
    <item>
      <title>How PROC SQL uses DISTINCT and GROUP by</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-PROC-SQL-uses-DISTINCT-and-GROUP-by/m-p/426897#M68392</link>
      <description>&lt;P&gt;This is a pretty basic question, but I wanted some confirmation since I haven't been able to find a clear answer.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a database of users of a certain services we provide in a given year. Sometimes these users return for the same service or other services. I want to find how many distinct users I have of each service so I constructed this code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table SERVICES&amp;nbsp;as&lt;BR /&gt;select&lt;BR /&gt;service,&lt;BR /&gt;count(distinct user_id) as Num_Users&lt;BR /&gt;from&amp;nbsp;users&lt;BR /&gt;where year = 2017&lt;BR /&gt;group by service;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to know if this is counting unique users of each service or unique users from the total database. For example, if person A used Service1 and Service2 are they being counted in both service groupings or are they being counted as 1 unique user? Is this the correct interpretation of this code?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2018 15:38:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-PROC-SQL-uses-DISTINCT-and-GROUP-by/m-p/426897#M68392</guid>
      <dc:creator>KellyJade</dc:creator>
      <dc:date>2018-01-11T15:38:18Z</dc:date>
    </item>
    <item>
      <title>Re: How PROC SQL uses DISTINCT and GROUP by</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-PROC-SQL-uses-DISTINCT-and-GROUP-by/m-p/426900#M68393</link>
      <description>&lt;P&gt;You wouldn't want to use a where clause with a group by.&amp;nbsp; In SQL you would use a having clause with a group by:&lt;/P&gt;
&lt;PRE&gt;group by service having year=2017;&lt;/PRE&gt;
&lt;P&gt;Or you could sub-query:&lt;/P&gt;
&lt;PRE&gt;from (select * from users where year=2017)
group by service;&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Jan 2018 15:44:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-PROC-SQL-uses-DISTINCT-and-GROUP-by/m-p/426900#M68393</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-01-11T15:44:14Z</dc:date>
    </item>
    <item>
      <title>Re: How PROC SQL uses DISTINCT and GROUP by</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-PROC-SQL-uses-DISTINCT-and-GROUP-by/m-p/426901#M68394</link>
      <description>&lt;P&gt;A very small data set to test with such as:&lt;/P&gt;
&lt;PRE&gt;data users;
   input user_id Service;
   year=2017;
datalines;
1  1
1  2
;
run;&lt;/PRE&gt;
&lt;P&gt;should fairly easily answer this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that the output of the&amp;nbsp;Proc SQL&amp;nbsp;for that example is 2 records with a count of one each. Since there are only two records input ...&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2018 15:47:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-PROC-SQL-uses-DISTINCT-and-GROUP-by/m-p/426901#M68394</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-01-11T15:47:39Z</dc:date>
    </item>
    <item>
      <title>Re: How PROC SQL uses DISTINCT and GROUP by</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-PROC-SQL-uses-DISTINCT-and-GROUP-by/m-p/426920#M68396</link>
      <description>Sorry &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt; I'm not sure I'm following you, why can't I filter the source data with a WHERE clause like this? Beside the logical part of it, HAVING will never use indexes for optimization. &lt;BR /&gt;I pretty much use HAVING only for filtering on aggregations.</description>
      <pubDate>Thu, 11 Jan 2018 16:58:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-PROC-SQL-uses-DISTINCT-and-GROUP-by/m-p/426920#M68396</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2018-01-11T16:58:39Z</dc:date>
    </item>
    <item>
      <title>Re: How PROC SQL uses DISTINCT and GROUP by</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-PROC-SQL-uses-DISTINCT-and-GROUP-by/m-p/427129#M68427</link>
      <description>&lt;P&gt;I can't remember why exactly, must have had issues with it at some point in the past so now I totally avoid using where clauses on group by's and put them in subqueries or havings.&amp;nbsp; It may just be habit from my side.&amp;nbsp; Definately can't use use where for aggregates.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Jan 2018 08:57:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-PROC-SQL-uses-DISTINCT-and-GROUP-by/m-p/427129#M68427</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-01-12T08:57:20Z</dc:date>
    </item>
  </channel>
</rss>

