<?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: proc sql &amp;amp; sum in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-amp-sum/m-p/766512#M80987</link>
    <description>&lt;P&gt;How about this?&lt;/P&gt;
&lt;P&gt;There is no format to display it as Jan-21, though, so it will be JAN21.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input date:monyy6. Type $ Count;
  format date monyy6.;
cards;
Jan-21	A	100
Jan-21	B	200
Feb-21	A	300
Feb-21	B	400
;
run;

proc sort data=have;
  by date;
run;

data want;
  set have;
  by date;
  retain sum;
  if first.date then sum=0;
  sum=sum+count;
  output;
  if last.date then do;
    Type='C';
    Count=sum;
    output;
  end;
  drop sum;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 08 Sep 2021 03:21:18 GMT</pubDate>
    <dc:creator>japelin</dc:creator>
    <dc:date>2021-09-08T03:21:18Z</dc:date>
    <item>
      <title>proc sql &amp; sum</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-amp-sum/m-p/766510#M80986</link>
      <description>&lt;P&gt;I think this is a simple one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is what I have&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 162pt;" border="0" width="216" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 14.25pt;"&gt;
&lt;TD width="72" height="19" style="height: 14.25pt; width: 54pt;"&gt;date&lt;/TD&gt;
&lt;TD width="72" style="width: 54pt;"&gt;Type&lt;/TD&gt;
&lt;TD width="72" style="width: 54pt;"&gt;Count&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.25pt;"&gt;
&lt;TD height="19" align="right" class="xl64" style="height: 14.25pt;"&gt;Jan-21&lt;/TD&gt;
&lt;TD class="xl63"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;100&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.25pt;"&gt;
&lt;TD height="19" align="right" class="xl64" style="height: 14.25pt;"&gt;Jan-21&lt;/TD&gt;
&lt;TD class="xl63"&gt;B&lt;/TD&gt;
&lt;TD align="right"&gt;200&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.25pt;"&gt;
&lt;TD height="19" align="right" class="xl64" style="height: 14.25pt;"&gt;Feb-21&lt;/TD&gt;
&lt;TD class="xl63"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;300&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.25pt;"&gt;
&lt;TD height="19" align="right" class="xl64" style="height: 14.25pt;"&gt;Feb-21&lt;/TD&gt;
&lt;TD class="xl63"&gt;B&lt;/TD&gt;
&lt;TD align="right"&gt;400&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is what I want&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 162pt;" border="0" width="216" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 14.25pt;"&gt;
&lt;TD width="72" height="19" style="height: 14.25pt; width: 54pt;"&gt;date&lt;/TD&gt;
&lt;TD width="72" style="width: 54pt;"&gt;Type&lt;/TD&gt;
&lt;TD width="72" style="width: 54pt;"&gt;Count&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.25pt;"&gt;
&lt;TD height="19" align="right" class="xl66" style="height: 14.25pt;"&gt;Jan-21&lt;/TD&gt;
&lt;TD class="xl65"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;100&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.25pt;"&gt;
&lt;TD height="19" align="right" class="xl66" style="height: 14.25pt;"&gt;Jan-21&lt;/TD&gt;
&lt;TD class="xl65"&gt;B&lt;/TD&gt;
&lt;TD align="right"&gt;200&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.25pt;"&gt;
&lt;TD height="19" align="right" class="xl66" style="height: 14.25pt;"&gt;Jan-21&lt;/TD&gt;
&lt;TD&gt;C&lt;/TD&gt;
&lt;TD align="right"&gt;300&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.25pt;"&gt;
&lt;TD height="19" align="right" class="xl66" style="height: 14.25pt;"&gt;Feb-21&lt;/TD&gt;
&lt;TD class="xl65"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;300&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.25pt;"&gt;
&lt;TD height="19" align="right" class="xl66" style="height: 14.25pt;"&gt;Feb-21&lt;/TD&gt;
&lt;TD class="xl65"&gt;B&lt;/TD&gt;
&lt;TD align="right"&gt;400&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.25pt;"&gt;
&lt;TD height="19" align="right" class="xl66" style="height: 14.25pt;"&gt;Feb-21&lt;/TD&gt;
&lt;TD class="xl65"&gt;C&lt;/TD&gt;
&lt;TD align="right"&gt;700&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 08 Sep 2021 02:18:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-amp-sum/m-p/766510#M80986</guid>
      <dc:creator>ywon111</dc:creator>
      <dc:date>2021-09-08T02:18:41Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql &amp; sum</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-amp-sum/m-p/766512#M80987</link>
      <description>&lt;P&gt;How about this?&lt;/P&gt;
&lt;P&gt;There is no format to display it as Jan-21, though, so it will be JAN21.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input date:monyy6. Type $ Count;
  format date monyy6.;
cards;
Jan-21	A	100
Jan-21	B	200
Feb-21	A	300
Feb-21	B	400
;
run;

proc sort data=have;
  by date;
run;

data want;
  set have;
  by date;
  retain sum;
  if first.date then sum=0;
  sum=sum+count;
  output;
  if last.date then do;
    Type='C';
    Count=sum;
    output;
  end;
  drop sum;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Sep 2021 03:21:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-amp-sum/m-p/766512#M80987</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2021-09-08T03:21:18Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql &amp; sum</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-amp-sum/m-p/766513#M80988</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If every date has exactly two observations as in your example, and if the data are sorted by date, then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by date ;
  output;
  type='C';
  count+lag(count);
  if last.date then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The "by date" statement not only tells SAS to expect the data to be sorted, but also generates two dummy variables: &lt;EM&gt;&lt;STRONG&gt;first.date&lt;/STRONG&gt;&lt;/EM&gt; and &lt;EM&gt;&lt;STRONG&gt;last.date&lt;/STRONG&gt;&lt;/EM&gt;, indicating whether the observation in hand is the first or last one for a given date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first &lt;EM&gt;&lt;STRONG&gt;output&lt;/STRONG&gt; &lt;/EM&gt;statement just outputs the current record, followed by modifying count to add the previous obs.&amp;nbsp; The second &lt;EM&gt;&lt;STRONG&gt;output&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp;statement only outputs that modified count variable for the last obs of each date.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Sep 2021 03:24:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-amp-sum/m-p/766513#M80988</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-09-08T03:24:00Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql &amp; sum</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-amp-sum/m-p/766525#M80989</link>
      <description>&lt;P&gt;A slightly different take, reading in a DO loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do until (last.date);
  set have;
  by date;
  output;
  _sum = sum(_sum,count);
end;
type = "C";
count = _sum;
output;
drop _sum;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that SQL is very poor at processing &lt;EM&gt;sequences&lt;/EM&gt;, it is aimed more at &lt;EM&gt;groups&lt;/EM&gt;. So the tool of choice for your task is the data step (Maxim 14).&lt;/P&gt;</description>
      <pubDate>Wed, 08 Sep 2021 08:10:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-amp-sum/m-p/766525#M80989</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-09-08T08:10:39Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql &amp; sum</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-amp-sum/m-p/766558#M80996</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input date:monyy6. Type $ Count;
  format date monyy6.;
cards;
Jan-21	A	100
Jan-21	B	200
Feb-21	A	300
Feb-21	B	400
;

proc sql;
create table want as
select * from have
union
select date,'C',sum(count) from have group by date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Sep 2021 12:26:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-amp-sum/m-p/766558#M80996</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-09-08T12:26:27Z</dc:date>
    </item>
  </channel>
</rss>

