<?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 Aggregating/Summing based on condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-Summing-based-on-condition/m-p/656352#M196790</link>
    <description>&lt;P&gt;I have a large data set which i need to agregate based on the below conditions:&lt;/P&gt;&lt;P&gt;1 The data i have:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;have;&lt;/P&gt;&lt;P&gt;input&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;Client $ transaction $ date1&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;MMDDYY10.&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;date2&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;MMDDYY10.&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;value1 value2;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;id1 a 05/10/2010 05/10/2010 100 200&lt;/P&gt;&lt;P&gt;id1 a 05/10/2010 05/10/2010 200 200&lt;/P&gt;&lt;P&gt;id1 b 05/10/2010 05/22/2013 200 300&lt;/P&gt;&lt;P&gt;id1 c 05/10/2010 08/15/2016 300 100&lt;/P&gt;&lt;P&gt;id1 a 03/10/2017 03/10/2017 700 200&lt;/P&gt;&lt;P&gt;id1 b 03/10/2017 05/22/2018 200 700&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. the data i want is as per below:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;DATA&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;WANT;&lt;/P&gt;&lt;P&gt;INPUT&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;Client $ date1&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;MMDDYY10.&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;Sum1 Sum2;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;id1 05/10/2010 300 100&lt;/P&gt;&lt;P&gt;id1 03/10/2017 700 700&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Basically Sum1 is the sum of value 1 where date1=min(date2) and Sum2 is the sum of value2 where date1=max(date2).&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;the solution can be in sas or sql.&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;Any help would be highly appreciated.&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 10 Jun 2020 11:09:21 GMT</pubDate>
    <dc:creator>inid</dc:creator>
    <dc:date>2020-06-10T11:09:21Z</dc:date>
    <item>
      <title>Aggregating/Summing based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-Summing-based-on-condition/m-p/656352#M196790</link>
      <description>&lt;P&gt;I have a large data set which i need to agregate based on the below conditions:&lt;/P&gt;&lt;P&gt;1 The data i have:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;have;&lt;/P&gt;&lt;P&gt;input&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;Client $ transaction $ date1&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;MMDDYY10.&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;date2&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;MMDDYY10.&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;value1 value2;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;id1 a 05/10/2010 05/10/2010 100 200&lt;/P&gt;&lt;P&gt;id1 a 05/10/2010 05/10/2010 200 200&lt;/P&gt;&lt;P&gt;id1 b 05/10/2010 05/22/2013 200 300&lt;/P&gt;&lt;P&gt;id1 c 05/10/2010 08/15/2016 300 100&lt;/P&gt;&lt;P&gt;id1 a 03/10/2017 03/10/2017 700 200&lt;/P&gt;&lt;P&gt;id1 b 03/10/2017 05/22/2018 200 700&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. the data i want is as per below:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;DATA&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;WANT;&lt;/P&gt;&lt;P&gt;INPUT&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;Client $ date1&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;MMDDYY10.&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;Sum1 Sum2;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;id1 05/10/2010 300 100&lt;/P&gt;&lt;P&gt;id1 03/10/2017 700 700&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Basically Sum1 is the sum of value 1 where date1=min(date2) and Sum2 is the sum of value2 where date1=max(date2).&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;the solution can be in sas or sql.&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;Any help would be highly appreciated.&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jun 2020 11:09:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregating-Summing-based-on-condition/m-p/656352#M196790</guid>
      <dc:creator>inid</dc:creator>
      <dc:date>2020-06-10T11:09:21Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregating/Summing based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-Summing-based-on-condition/m-p/656355#M196791</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have nway;
    class client date1;
    var value1 value2;
    output out=want sum=sum1 sum2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Jun 2020 11:12:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregating-Summing-based-on-condition/m-p/656355#M196791</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-06-10T11:12:49Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregating/Summing based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-Summing-based-on-condition/m-p/656368#M196793</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/239611"&gt;@inid&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;STRONG&gt;Basically Sum1 is the sum of value 1 where date1=min(date2) and Sum2 is the sum of value2 where date1=max(date2).&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In your example, for client id1, min(date2) corresponds to 05/10/2010 and max(date2) to&amp;nbsp;&lt;SPAN&gt;05/22/2018. Four records have date1 = min(date2) and no record have date1=max(date2).&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;The sum of value1 would 800 in the first case, and 0 in the second.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;So it doesn't match your 'WANT' table.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Could you please explain&amp;nbsp;further what is the exact rule?&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;Wouldn't it be:&lt;/SPAN&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;FONT color="#3366FF"&gt;&lt;SPAN&gt;Sum1 is the sum of value1 by Client and date1 having date2=min(date2)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT color="#3366FF"&gt;&lt;SPAN&gt;Sum2 is the sum of value2 by Client and date1 having date2=max(date2)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;SPAN&gt;If so, you can try this:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table want as
	select Client, date1 format=MMDDYY10., sum(v1) as sum1, sum(v2) as sum2
	from (select *, value1*(date2=min(date2)) as v1, value2*(date2=max(date2)) as v2
	 	 from have as a
	 	 group by client, date1)
	group by client, date1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture d’écran 2020-06-10 à 14.06.17.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/42136iD974BB62ECD82947/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Capture d’écran 2020-06-10 à 14.06.17.png" alt="Capture d’écran 2020-06-10 à 14.06.17.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt; &lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Best,&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jun 2020 12:06:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregating-Summing-based-on-condition/m-p/656368#M196793</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-06-10T12:06:30Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregating/Summing based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-Summing-based-on-condition/m-p/656387#M196801</link>
      <description>Thank you very much &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21414"&gt;@ed&lt;/a&gt;_sas_me for your help</description>
      <pubDate>Wed, 10 Jun 2020 12:31:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregating-Summing-based-on-condition/m-p/656387#M196801</guid>
      <dc:creator>inid</dc:creator>
      <dc:date>2020-06-10T12:31:27Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregating/Summing based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-Summing-based-on-condition/m-p/656388#M196802</link>
      <description>Proc sort data= have; by client_id date1 date2;&lt;BR /&gt;Data want;&lt;BR /&gt;Set have;&lt;BR /&gt;By client_id date1 date2;&lt;BR /&gt;Retain sum1 sum2;&lt;BR /&gt;If first.date1 then sum1= value1;&lt;BR /&gt;If last.date1 then do;&lt;BR /&gt;Sum2 = value2;&lt;BR /&gt;Output;&lt;BR /&gt;End;&lt;BR /&gt;Run;</description>
      <pubDate>Wed, 10 Jun 2020 12:34:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregating-Summing-based-on-condition/m-p/656388#M196802</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-06-10T12:34:33Z</dc:date>
    </item>
  </channel>
</rss>

