<?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: what is wrong in my code? (proc sql, having clause) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/what-is-wrong-in-my-code-proc-sql-having-clause/m-p/273088#M54393</link>
    <description>&lt;P&gt;I don't see anything explicitly wrong with your code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you add some sample data that replicates your issue?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And/or your log if it shows any Notes/Errors.&lt;/P&gt;</description>
    <pubDate>Wed, 25 May 2016 17:44:22 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2016-05-25T17:44:22Z</dc:date>
    <item>
      <title>what is wrong in my code? (proc sql, having clause)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/what-is-wrong-in-my-code-proc-sql-having-clause/m-p/273065#M54381</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to use the following code to select the first 30days (from the first wagerdatedatime) data, but it turned out&amp;nbsp; I still got the whole data. Where could be the problem?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
create table final2.First_30_days_wager as
	select DUPI, 
	       count(distinct provider) as sites_wagered,
		   count(distinct datepart(wagerdatetime) ) as betting_days,
		   min(total_wager) as min_wager,
	       max(total_wager) as max_wager,
		   mean(total_wager) as mean_wager,
		   std(total_wager) as std_wager,
		   sum(total_wager) as sum_wager,
		   count(total_wager) as betting_times,
		   min(datepart(wagerdatetime)) as first_day
	from Anawager.dupi_wager_b_all
	where total_wager&amp;gt;0  
	group by DUPI
    having datepart(wagerdatetime)&amp;lt;min(datepart(wagerdatetime))+30;
quit;&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 May 2016 16:15:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/what-is-wrong-in-my-code-proc-sql-having-clause/m-p/273065#M54381</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2016-05-25T16:15:18Z</dc:date>
    </item>
    <item>
      <title>Re: what is wrong in my code? (proc sql, having clause)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/what-is-wrong-in-my-code-proc-sql-having-clause/m-p/273088#M54393</link>
      <description>&lt;P&gt;I don't see anything explicitly wrong with your code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you add some sample data that replicates your issue?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And/or your log if it shows any Notes/Errors.&lt;/P&gt;</description>
      <pubDate>Wed, 25 May 2016 17:44:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/what-is-wrong-in-my-code-proc-sql-having-clause/m-p/273088#M54393</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-05-25T17:44:22Z</dc:date>
    </item>
    <item>
      <title>Re: what is wrong in my code? (proc sql, having clause)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/what-is-wrong-in-my-code-proc-sql-having-clause/m-p/273095#M54398</link>
      <description>&lt;P&gt;Thank you, Reeze. Log file already overwritten.&lt;/P&gt;
&lt;P&gt;I ended up with 2 steps, first step select the 30 days data and second step to do the summaries.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
create table test.first30days as
select * 
from anawager.dupi_wager_b_all
group by DUPI
having datepart(wagerdatetime) &amp;lt;= min(datepart(wagerdatetime))+30;
quit;


proc sql;
create table test.first30days_stat as
	select DUPI, 
	       count(distinct provider) as sites_wagered,
		   count(distinct datepart(wagerdatetime) ) as betting_days,
		   min(total_wager) as min_wager,
	       max(total_wager) as max_wager,
		   mean(total_wager) as mean_wager,
		   std(total_wager) as std_wager,
		   sum(total_wager) as sum_wager,
		   count(total_wager) as count_wager
	from test.first30days
	where total_wager&amp;gt;0   /* some total_wager&amp;lt;0; so many =0 */
	group by DUPI;
quit;&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 May 2016 18:10:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/what-is-wrong-in-my-code-proc-sql-having-clause/m-p/273095#M54398</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2016-05-25T18:10:49Z</dc:date>
    </item>
    <item>
      <title>Re: what is wrong in my code? (proc sql, having clause)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/what-is-wrong-in-my-code-proc-sql-having-clause/m-p/273106#M54404</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/56807"&gt;@fengyuwuzu﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The issue was that the HAVING clause applied to the result of the SELECT statement up to and including the GROUP BY clause. That is, the various summary statistics before the FROM clause were &lt;EM&gt;not&lt;/EM&gt; restricted to the 30-days time window. The first occurrence of WAGERDATETIME in the HAVING clause then required remerging summary statistics to the original data. Finally, the HAVING clause selected part of the remerged records (most probably many duplicates).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead of creating an intermediate dataset test.first30days you could write the SELECT&amp;nbsp;statement of the new first PROC SQL step as an inline view into the FROM clause of the second PROC SQL step. (Also, the SELECT statement could be restricted to the variables needed in the second query:&amp;nbsp;DUPI, provider, wagerdatetime, total_wager.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In any case, please note that it makes a difference, in general, whether the WHERE clause is applied in the first query (or inline view, resp.) or in the second query.&lt;/P&gt;</description>
      <pubDate>Wed, 25 May 2016 18:49:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/what-is-wrong-in-my-code-proc-sql-having-clause/m-p/273106#M54404</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-05-25T18:49:07Z</dc:date>
    </item>
    <item>
      <title>Re: what is wrong in my code? (proc sql, having clause)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/what-is-wrong-in-my-code-proc-sql-having-clause/m-p/273123#M54406</link>
      <description>&lt;P&gt;Thank you for the explanation and suggestion.&lt;/P&gt;</description>
      <pubDate>Wed, 25 May 2016 20:10:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/what-is-wrong-in-my-code-proc-sql-having-clause/m-p/273123#M54406</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2016-05-25T20:10:52Z</dc:date>
    </item>
  </channel>
</rss>

