<?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: summarize data and sum in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/summarize-data-and-sum/m-p/663455#M198048</link>
    <description>&lt;P&gt;The stat genius&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp; is right about design, however for your requirement I would prefer Proc SQL-&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input date mmddyy10. 	balance	flag;
format date mmddyy10.;
cards;
01.04.2020	10	0
01.04.2020	20	1
02.04.2020	10	0
02.04.2020	10	0
02.04.2020	10	1
;

proc sql;
create table want as
select date, sum(balance) as total,sum((flag=0)*balance) as flag0,sum((flag=1)*balance) as flag1
from have
group by date;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 19 Jun 2020 12:32:57 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2020-06-19T12:32:57Z</dc:date>
    <item>
      <title>summarize data and sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summarize-data-and-sum/m-p/663451#M198045</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a really easy question but somehow I dont get the right result. I have a table like this:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;date&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;balance&lt;/TD&gt;&lt;TD&gt;flag&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01.04.2020&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01.04.2020&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;02.04.2020&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;02.04.2020&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;02.04.2020&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;And I want to summarize it, so that my result table looks like this:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;total&lt;/TD&gt;&lt;TD&gt;flag_0&lt;/TD&gt;&lt;TD&gt;flag_1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01.04.2020&lt;/TD&gt;&lt;TD&gt;30&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;02.04.2020&lt;/TD&gt;&lt;TD&gt;30&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you help me with posting the correct SAS code,thank you for the help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jun 2020 12:25:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summarize-data-and-sum/m-p/663451#M198045</guid>
      <dc:creator>aguilar_john</dc:creator>
      <dc:date>2020-06-19T12:25:54Z</dc:date>
    </item>
    <item>
      <title>Re: summarize data and sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summarize-data-and-sum/m-p/663454#M198047</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have nway;
    class date flag;
    var balance;
    output out=want sum=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This gives you the sums. If you absolutely have to have it with flag_0 and flag_1 columns (why? it's usually not a good idea to make data wider) then PROC TRANSPOSE gets you there.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jun 2020 12:27:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summarize-data-and-sum/m-p/663454#M198047</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-06-19T12:27:14Z</dc:date>
    </item>
    <item>
      <title>Re: summarize data and sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summarize-data-and-sum/m-p/663455#M198048</link>
      <description>&lt;P&gt;The stat genius&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp; is right about design, however for your requirement I would prefer Proc SQL-&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input date mmddyy10. 	balance	flag;
format date mmddyy10.;
cards;
01.04.2020	10	0
01.04.2020	20	1
02.04.2020	10	0
02.04.2020	10	0
02.04.2020	10	1
;

proc sql;
create table want as
select date, sum(balance) as total,sum((flag=0)*balance) as flag0,sum((flag=1)*balance) as flag1
from have
group by date;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 Jun 2020 12:32:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summarize-data-and-sum/m-p/663455#M198048</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-06-19T12:32:57Z</dc:date>
    </item>
    <item>
      <title>Re: summarize data and sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summarize-data-and-sum/m-p/663456#M198049</link>
      <description>Thanks a lot!!&lt;BR /&gt;Also the PROC TRANSPOSE did work out. That is just the requested format...</description>
      <pubDate>Fri, 19 Jun 2020 12:33:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summarize-data-and-sum/m-p/663456#M198049</guid>
      <dc:creator>aguilar_john</dc:creator>
      <dc:date>2020-06-19T12:33:39Z</dc:date>
    </item>
    <item>
      <title>Re: summarize data and sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summarize-data-and-sum/m-p/663458#M198050</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The stat genius&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp; is right about design, however for your requirement I would prefer Proc SQL-&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input date mmddyy10. 	balance	flag;
format date mmddyy10.;
cards;
01.04.2020	10	0
01.04.2020	20	1
02.04.2020	10	0
02.04.2020	10	0
02.04.2020	10	1
;

proc sql;
create table want as
select date, sum(balance) as total,sum((flag=0)*balance) as flag0,sum((flag=1)*balance) as flag1
from have
group by date;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Let me explain why I prefer PROC SUMMARY over PROC SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have lots of values of FLAG (instead of just two), the PROC SQL solution is a lot more typing. The PROC SUMMARY/PROC TRANSPOSE solution requires no additional typing, it works for any number of values of FLAG.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jun 2020 12:35:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summarize-data-and-sum/m-p/663458#M198050</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-06-19T12:35:57Z</dc:date>
    </item>
    <item>
      <title>Re: summarize data and sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summarize-data-and-sum/m-p/663460#M198052</link>
      <description>&lt;P&gt;I fully agree. I did think of it, however since I noticed Flag variable is &lt;EM&gt;&lt;STRONG&gt;"binary" , &lt;/STRONG&gt;&lt;/EM&gt;I was pretty confident there to suggest the SQL approach. That being said, the two step solution is certainly the one that will scale for any number of ID value suffix.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jun 2020 12:39:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summarize-data-and-sum/m-p/663460#M198052</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-06-19T12:39:24Z</dc:date>
    </item>
    <item>
      <title>Re: summarize data and sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summarize-data-and-sum/m-p/663489#M198067</link>
      <description>Many Thanks! that is also a nice way to do it</description>
      <pubDate>Fri, 19 Jun 2020 13:50:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summarize-data-and-sum/m-p/663489#M198067</guid>
      <dc:creator>aguilar_john</dc:creator>
      <dc:date>2020-06-19T13:50:25Z</dc:date>
    </item>
  </channel>
</rss>

