<?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: maximum over multiple rows per person in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/maximum-over-multiple-rows-per-person/m-p/515956#M139307</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;May i ask why not a simple proc means/summary/sql?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;For each person ID I have multiple observations (multiple rows).&lt;/P&gt;
&lt;P&gt;Target is to find max weight for each customer over his/her observations.&lt;/P&gt;
&lt;P&gt;Following code is giving &amp;nbsp;bad results.&lt;/P&gt;
&lt;P&gt;why?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data rawData;
Input ID month weight;
cards;
1 1801 70
1 1802 90
1 1803 85
2 1801 90
2 1802 94
2 1803 93
3 1802 78
3 1803 80
;
run;



Data tbl1(keep=ID  max_weight) ;
set rawData;
by ID;
retain max_weight 0 ;
max_weight=max(max_weight,weight);
IF last.ID then output;
Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 26 Nov 2018 12:12:03 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-11-26T12:12:03Z</dc:date>
    <item>
      <title>maximum over multiple rows per person</title>
      <link>https://communities.sas.com/t5/SAS-Programming/maximum-over-multiple-rows-per-person/m-p/515932#M139297</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;For each person ID I have multiple observations (multiple rows).&lt;/P&gt;
&lt;P&gt;Target is to find max weight for each customer over his/her observations.&lt;/P&gt;
&lt;P&gt;Following code is giving &amp;nbsp;bad results.&lt;/P&gt;
&lt;P&gt;why?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data rawData;
Input ID month weight;
cards;
1 1801 70
1 1802 90
1 1803 85
2 1801 90
2 1802 94
2 1803 93
3 1802 78
3 1803 80
;
run;



Data tbl1(keep=ID  max_weight) ;
set rawData;
by ID;
retain max_weight 0 ;
max_weight=max(max_weight,weight);
IF last.ID then output;
Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Nov 2018 10:14:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/maximum-over-multiple-rows-per-person/m-p/515932#M139297</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2018-11-26T10:14:18Z</dc:date>
    </item>
    <item>
      <title>Re: maximum over multiple rows per person</title>
      <link>https://communities.sas.com/t5/SAS-Programming/maximum-over-multiple-rows-per-person/m-p/515941#M139298</link>
      <description>&lt;P&gt;I think you forgot to initialize the MAX_WEIGHT variable when the ID changes. Or you could use a DoW loop without a retain statement:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tbl1(keep=ID  max_weight) ;
  do until(last.id);
    set rawData;
    by ID;
    max_weight=max(max_weight,weight);
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Nov 2018 10:40:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/maximum-over-multiple-rows-per-person/m-p/515941#M139298</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-11-26T10:40:12Z</dc:date>
    </item>
    <item>
      <title>Re: maximum over multiple rows per person</title>
      <link>https://communities.sas.com/t5/SAS-Programming/maximum-over-multiple-rows-per-person/m-p/515943#M139299</link>
      <description>&lt;P&gt;I assume by "bad results" you mean it is not giving you what you expected it to give you?&amp;nbsp; It is doing what you ask correctly, your just not covering the whole by group, so the value gets retained all the way down, so each row is the maximum of the whole table to that point, not just within the by group.&amp;nbsp; To do the by group you need to reset your retained value on first.id:&lt;/P&gt;
&lt;PRE&gt;data tbl1(keep=id  max_weight) ;
  set rawdata;
  by id;
  retain max_weight;
  if first.id then max_weight=0;
  max_weight=max(max_weight,weight);
  if last.id then output;
run;&lt;/PRE&gt;
&lt;P&gt;Or if you want smaller code:&lt;/P&gt;
&lt;PRE&gt;data tbl1(keep=id  max_weight) ;
  set rawData;
  by ID;
  retain max_weight;
  max_weight=ifn(first.id,weight,max(max_weight,weight));
  if last.id then output;
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 26 Nov 2018 10:44:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/maximum-over-multiple-rows-per-person/m-p/515943#M139299</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-11-26T10:44:04Z</dc:date>
    </item>
    <item>
      <title>Re: maximum over multiple rows per person</title>
      <link>https://communities.sas.com/t5/SAS-Programming/maximum-over-multiple-rows-per-person/m-p/515956#M139307</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;May i ask why not a simple proc means/summary/sql?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;For each person ID I have multiple observations (multiple rows).&lt;/P&gt;
&lt;P&gt;Target is to find max weight for each customer over his/her observations.&lt;/P&gt;
&lt;P&gt;Following code is giving &amp;nbsp;bad results.&lt;/P&gt;
&lt;P&gt;why?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data rawData;
Input ID month weight;
cards;
1 1801 70
1 1802 90
1 1803 85
2 1801 90
2 1802 94
2 1803 93
3 1802 78
3 1803 80
;
run;



Data tbl1(keep=ID  max_weight) ;
set rawData;
by ID;
retain max_weight 0 ;
max_weight=max(max_weight,weight);
IF last.ID then output;
Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Nov 2018 12:12:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/maximum-over-multiple-rows-per-person/m-p/515956#M139307</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-11-26T12:12:03Z</dc:date>
    </item>
  </channel>
</rss>

