<?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: Sql grouping &amp;amp; summation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sql-grouping-amp-summation/m-p/272663#M54265</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Its a good idea to pot your test data in the form of a datastep. &amp;nbsp;Not sure I got the logic for your second point right as I get a different number:&lt;/P&gt;
&lt;PRE&gt;data have;
  informat currdate date9.;
  input ID  currdate  pvecnt nvecnt;
  format currdate date9.;
datalines;
1 11apr2016      0           2
1 12apr2016     3          4
1 13apr2016      5           5
1 16apr2016      6            10
;
run;

proc sql;
  create table WANT as
  select  ID,
          sum(WE) as SUM_WE,
          sum(WD) as SUM_WD
  from    (select ID,
                  case when weekday(CURRDATE) in (1,7) then sum(PVECNT,NVECNT) else . end as WE,
                  case when weekday(CURRDATE) not in (1,7) then sum(PVECNT) else . end as WD 
           from HAVE where PVECNT &amp;gt; 0 and NVECNT &amp;gt; 0)
  group by ID;
quit;&lt;/PRE&gt;</description>
    <pubDate>Tue, 24 May 2016 10:10:22 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2016-05-24T10:10:22Z</dc:date>
    <item>
      <title>Sql grouping &amp; summation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sql-grouping-amp-summation/m-p/272657#M54264</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID &amp;nbsp;currdate &amp;nbsp;pvecnt nvecnt&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp;11apr2016 &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;1 12 apr 2016 &amp;nbsp; &amp;nbsp; 3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/P&gt;
&lt;P&gt;1 13apr2016 &amp;nbsp; &amp;nbsp; &amp;nbsp;5 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&lt;/P&gt;
&lt;P&gt;1 16apr2016 &amp;nbsp; &amp;nbsp; &amp;nbsp;6 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10&lt;/P&gt;
&lt;P&gt;i need&amp;nbsp;2 vars sum(pve/nve) &amp;nbsp;cnt on weekend and sum(pve/nve) cnt on weekday&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;logic i used&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;select id ,(case when pvecnt &amp;gt;0 and nvecnt&amp;gt;0 and weekday(currdate) in (1,7) then sum(pvecnt,nvecnt) as weekend_cnt,&lt;/P&gt;
&lt;P&gt;case when pvecnt &amp;gt;0 and nvecnt &amp;gt;0 and weekday(currdate) not in (1,7) then sum(pvecnt,nvecnt) as weekday_cnt)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;from table group by id;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;rather than getting 1 result m getting 4 rows and sum of their vars in horizontally&amp;nbsp;&lt;/P&gt;
&lt;P&gt;what i want :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;id &amp;nbsp;wknd_cnt wkdy_Cnt&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; &amp;nbsp; 16 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 17&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i'm not considering row where either of pvecnt or nvecnt is 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 24 May 2016 09:26:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sql-grouping-amp-summation/m-p/272657#M54264</guid>
      <dc:creator>LearnByMistk</dc:creator>
      <dc:date>2016-05-24T09:26:26Z</dc:date>
    </item>
    <item>
      <title>Re: Sql grouping &amp; summation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sql-grouping-amp-summation/m-p/272663#M54265</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Its a good idea to pot your test data in the form of a datastep. &amp;nbsp;Not sure I got the logic for your second point right as I get a different number:&lt;/P&gt;
&lt;PRE&gt;data have;
  informat currdate date9.;
  input ID  currdate  pvecnt nvecnt;
  format currdate date9.;
datalines;
1 11apr2016      0           2
1 12apr2016     3          4
1 13apr2016      5           5
1 16apr2016      6            10
;
run;

proc sql;
  create table WANT as
  select  ID,
          sum(WE) as SUM_WE,
          sum(WD) as SUM_WD
  from    (select ID,
                  case when weekday(CURRDATE) in (1,7) then sum(PVECNT,NVECNT) else . end as WE,
                  case when weekday(CURRDATE) not in (1,7) then sum(PVECNT) else . end as WD 
           from HAVE where PVECNT &amp;gt; 0 and NVECNT &amp;gt; 0)
  group by ID;
quit;&lt;/PRE&gt;</description>
      <pubDate>Tue, 24 May 2016 10:10:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sql-grouping-amp-summation/m-p/272663#M54265</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-05-24T10:10:22Z</dc:date>
    </item>
    <item>
      <title>Re: Sql grouping &amp; summation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sql-grouping-amp-summation/m-p/272664#M54266</link>
      <description>&lt;P&gt;Your SQL fails with syntax errors.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data step solution;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards;
informat
  id 1.
  currdate date9.
  pvecnt 3.
  nvecnt 3.
;
format currdate date9.;
input
  ID 
  currdate
  pvecnt
  nvecnt
;
cards;
1 11apr2016 0 2
1 12apr2016 3 4
1 13apr2016 5 5
1 16apr2016 6 10
;
run;

data want (keep=ID weekend_cnt weekday_cnt);
set have;
by ID;
retain weekend_cnt weekday_cnt;
if first.ID
then do;
  weekend_cnt = 0;
  weekday_cnt = 0;
end;
if pvecnt &amp;gt; 0 and nvecnt &amp;gt; 0
then do;
  if weekday(currdate) in (1,7)
  then weekend_cnt + sum(pvecnt,nvecnt);
  else weekday_cnt + sum(pvecnt,nvecnt);
end;
if last.ID then output;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 24 May 2016 10:19:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sql-grouping-amp-summation/m-p/272664#M54266</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-05-24T10:19:55Z</dc:date>
    </item>
    <item>
      <title>Re: Sql grouping &amp; summation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sql-grouping-amp-summation/m-p/272673#M54269</link>
      <description>&lt;P&gt;Sorry, did you mean my SQL Kurt, its hard to tell what response is to which post with the forum software. &amp;nbsp;Mine runs fine on my machine?&lt;/P&gt;</description>
      <pubDate>Tue, 24 May 2016 10:41:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sql-grouping-amp-summation/m-p/272673#M54269</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-05-24T10:41:18Z</dc:date>
    </item>
    <item>
      <title>Re: Sql grouping &amp; summation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sql-grouping-amp-summation/m-p/272679#M54270</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;Sorry, did you mean my SQL Kurt, its hard to tell what response is to which post with the forum software. &amp;nbsp;Mine runs fine on my machine?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Sorry, no. I meant the original post.&lt;/P&gt;</description>
      <pubDate>Tue, 24 May 2016 10:51:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sql-grouping-amp-summation/m-p/272679#M54270</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-05-24T10:51:43Z</dc:date>
    </item>
    <item>
      <title>Re: Sql grouping &amp; summation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sql-grouping-amp-summation/m-p/272684#M54271</link>
      <description>&lt;P&gt;i wrote this query using alias var names and yes sql in questions isnt correct it missed end statment .&lt;/P&gt;
&lt;P&gt;But thanks both of you for revert.&lt;/P&gt;</description>
      <pubDate>Tue, 24 May 2016 11:10:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sql-grouping-amp-summation/m-p/272684#M54271</guid>
      <dc:creator>LearnByMistk</dc:creator>
      <dc:date>2016-05-24T11:10:59Z</dc:date>
    </item>
  </channel>
</rss>

