<?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: Aggregating a column and keeping rows with max in same step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-a-column-and-keeping-rows-with-max-in-same-step/m-p/691027#M210277</link>
    <description>Many thanks, I seem to missing the part where I had to rename the summed variable. I was using the same name(replacing Leakage_Amount) and it was throwing error, so had to go a long route.</description>
    <pubDate>Mon, 12 Oct 2020 17:45:37 GMT</pubDate>
    <dc:creator>thepushkarsingh</dc:creator>
    <dc:date>2020-10-12T17:45:37Z</dc:date>
    <item>
      <title>Aggregating a column and keeping rows with max in same step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-a-column-and-keeping-rows-with-max-in-same-step/m-p/690997#M210263</link>
      <description>&lt;P&gt;I have following data :&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; _tmp_;&lt;/P&gt;&lt;P&gt;length Customer $6. Type $4.;&lt;/P&gt;&lt;P&gt;input&lt;/P&gt;&lt;P&gt;Customer $ Type $ Leakage_amount year;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;ABC123 Mid 1500 2019&lt;/P&gt;&lt;P&gt;ABC123 Mid 7000 2020&lt;/P&gt;&lt;P&gt;ABC123 SB&amp;amp;A 2000 2020&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;And I want my output as, summing Leakage_Amount and picking Type with highest Leakage_Amount -&lt;/P&gt;&lt;P&gt;ABC123 Mid 1500 2019&lt;/P&gt;&lt;P&gt;ABC123 Mid 9000 2020&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can get this by&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt; &lt;STRONG&gt;sql&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;select a.*,b.Type from&lt;/P&gt;&lt;P&gt;(select Customer, year, sum(Leakage_amount) as Leakage_amount from _tmp_ group by &lt;STRONG&gt;1&lt;/STRONG&gt;,&lt;STRONG&gt;2&lt;/STRONG&gt;) as a&lt;/P&gt;&lt;P&gt;left join&lt;/P&gt;&lt;P&gt;(select Customer, year, Type from _tmp_ group by &lt;STRONG&gt;1&lt;/STRONG&gt;,&lt;STRONG&gt;2&lt;/STRONG&gt; having Leakage_amount = max(Leakage_amount)) as b&lt;/P&gt;&lt;P&gt;on A.Customer=B.Customer and A.Year=B.Year&lt;/P&gt;&lt;P&gt;;&lt;STRONG&gt;quit&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But this seems like overkill, there must be an easier and neater way to do this. Please help.&lt;/P&gt;</description>
      <pubDate>Mon, 12 Oct 2020 15:36:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregating-a-column-and-keeping-rows-with-max-in-same-step/m-p/690997#M210263</guid>
      <dc:creator>thepushkarsingh</dc:creator>
      <dc:date>2020-10-12T15:36:58Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregating a column and keeping rows with max in same step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-a-column-and-keeping-rows-with-max-in-same-step/m-p/691003#M210266</link>
      <description>&lt;P&gt;You can use PROC MEANS or PROC SUMMARY with the SUM output statistic and the MAXID output statistic.&lt;/P&gt;
&lt;P&gt;Example: &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=proc&amp;amp;docsetTarget=n18axszmd7up03n1densx922oe8y.htm&amp;amp;locale=en#n18axszmd7up03n1densx922oe8y" target="_blank"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=proc&amp;amp;docsetTarget=n18axszmd7up03n1densx922oe8y.htm&amp;amp;locale=en#n18axszmd7up03n1densx922oe8y&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Oct 2020 15:56:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregating-a-column-and-keeping-rows-with-max-in-same-step/m-p/691003#M210266</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-10-12T15:56:59Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregating a column and keeping rows with max in same step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-a-column-and-keeping-rows-with-max-in-same-step/m-p/691004#M210267</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data _tmp_;

length Customer $6. Type $4.;

input

Customer $ Type $ Leakage_amount year;

datalines;

ABC123 Mid 1500 2019

ABC123 Mid 7000 2020

ABC123 SB&amp;amp;A 2000 2020

;run;

proc sql;
 create table want as
 select *, sum(Leakage_amount) as sum_Leakage_amount 
 from _tmp_
 group by customer,year
 having Leakage_amount=max(Leakage_amount);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;A good use case to take advantage of auto-remerge by the SQL processor.&lt;/P&gt;</description>
      <pubDate>Mon, 12 Oct 2020 15:59:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregating-a-column-and-keeping-rows-with-max-in-same-step/m-p/691004#M210267</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-10-12T15:59:12Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregating a column and keeping rows with max in same step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-a-column-and-keeping-rows-with-max-in-same-step/m-p/691006#M210268</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe double DoW-loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
length Customer $6. Type $4.;
input
Customer $ Type $ Leakage_amount year;
datalines4;
ABC123 Mid 1500 2019
ABC123 Mid 7000 2020
ABC123 SB&amp;amp;A 2000 2020
;;;;
run;

data want;
  _max=.;
  _out=0;
  _tot=0;
  do _N_ = 1 by 1 until(last.year);
    set have;
    by year;

    if Leakage_amount &amp;gt; _max then
      do;
        _max = Leakage_amount;
        _out = _N_;
      end;
    _tot + Leakage_amount;
  end;

  do _N_ = 1 to _N_;
    set have;

    if _out = _N_ then
      do;
        Leakage_amount = _tot;
        output;
      end;
  end;
  drop _:;
run;
proc print;
run;


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 12 Oct 2020 16:12:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregating-a-column-and-keeping-rows-with-max-in-same-step/m-p/691006#M210268</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-10-12T16:12:49Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregating a column and keeping rows with max in same step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-a-column-and-keeping-rows-with-max-in-same-step/m-p/691025#M210276</link>
      <description>This is more of overkill!</description>
      <pubDate>Mon, 12 Oct 2020 17:44:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregating-a-column-and-keeping-rows-with-max-in-same-step/m-p/691025#M210276</guid>
      <dc:creator>thepushkarsingh</dc:creator>
      <dc:date>2020-10-12T17:44:25Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregating a column and keeping rows with max in same step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-a-column-and-keeping-rows-with-max-in-same-step/m-p/691027#M210277</link>
      <description>Many thanks, I seem to missing the part where I had to rename the summed variable. I was using the same name(replacing Leakage_Amount) and it was throwing error, so had to go a long route.</description>
      <pubDate>Mon, 12 Oct 2020 17:45:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregating-a-column-and-keeping-rows-with-max-in-same-step/m-p/691027#M210277</guid>
      <dc:creator>thepushkarsingh</dc:creator>
      <dc:date>2020-10-12T17:45:37Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregating a column and keeping rows with max in same step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-a-column-and-keeping-rows-with-max-in-same-step/m-p/691798#M210625</link>
      <description>Many thanks Paige, yours is a quite neat way. I really appreciate this very informative link. I wasn't even aware of MAXID.</description>
      <pubDate>Thu, 15 Oct 2020 12:26:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregating-a-column-and-keeping-rows-with-max-in-same-step/m-p/691798#M210625</guid>
      <dc:creator>thepushkarsingh</dc:creator>
      <dc:date>2020-10-15T12:26:03Z</dc:date>
    </item>
  </channel>
</rss>

