<?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: Getting max value by group - complex table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394707#M95142</link>
    <description>&lt;P&gt;Each SET statement is permitted to use a BY statement.&amp;nbsp; That sets up FIRST. and LAST. variables (regardless of whether inside a loop or not).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One of the key issues here is how the SET statement operates.&amp;nbsp; It is not a label that shows where the data is coming from.&amp;nbsp; Rather, it is an instruction to read the next observation from a SAS data set.&amp;nbsp; It only reads one observation at a time (whether inside or outside a DO loop).&amp;nbsp; So the BY statement merely identifies whether that observation is the first (or last) one for a value of GROUP.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;***** EDITED HERE:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe I shouldn't add this ... it might add to the confusion.&amp;nbsp; Yet some of the community will find this interesting.&amp;nbsp; Technically, the BY statement refers to the most recent SET / MERGE / UPDATE statement.&amp;nbsp; There is no reason that it has to immediatley follow a SET, MERGE, or UPDATE statement.&amp;nbsp; So this variation would work exactly the same way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;do until (last.group);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;maxval = max(maxval, of value1-value4);&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;by group;&lt;/P&gt;
&lt;P&gt;keep group maxval;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
    <pubDate>Mon, 11 Sep 2017 15:13:43 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2017-09-11T15:13:43Z</dc:date>
    <item>
      <title>Getting max value by group - complex table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394613#M95114</link>
      <description>&lt;P&gt;&amp;nbsp;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you please help in the following case - how to find a max within each group in one step:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
infile datalines missover;
input group $ value1 value 2 value 3 value4;
datalines;
g1 12 30
g2 14 37 15
g2 16 76 34
g3 12 22
g3 4 84
g4 7 15 10
g4 24 21 17 67
g4 14 18 94
g4 26 73 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wanted to get this output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;group&amp;nbsp;&amp;nbsp;&amp;nbsp; value&lt;/P&gt;&lt;P&gt;g1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 30&lt;/P&gt;&lt;P&gt;g2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 76&lt;/P&gt;&lt;P&gt;g3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 84&lt;/P&gt;&lt;P&gt;g4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 94&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank You!&lt;/P&gt;</description>
      <pubDate>Mon, 11 Sep 2017 10:02:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394613#M95114</guid>
      <dc:creator>DmytroYermak</dc:creator>
      <dc:date>2017-09-11T10:02:56Z</dc:date>
    </item>
    <item>
      <title>Re: Getting max value by group - complex table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394620#M95115</link>
      <description>&lt;P&gt;Something like:&lt;/P&gt;
&lt;PRE&gt;data test;&lt;BR /&gt; infile datalines missover;&lt;BR /&gt; input group $ value1 value2 value3 value4;&lt;BR /&gt;datalines;&lt;BR /&gt;g1 12 30&lt;BR /&gt;g2 14 37 15&lt;BR /&gt;g2 16 76 34&lt;BR /&gt;g3 12 22&lt;BR /&gt;g3 4 84&lt;BR /&gt;g4 7 15 10&lt;BR /&gt;g4 24 21 17 67&lt;BR /&gt;g4 14 18 94&lt;BR /&gt;g4 26 73 &lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;proc sql;&lt;BR /&gt; create table WANT as&lt;BR /&gt; select GROUP,&lt;BR /&gt; sum(sum(VALUE1,VALUE2,VALUE3,VALUE4)) as VALUE&lt;BR /&gt; from TEST&lt;BR /&gt; group by GROUP;&lt;BR /&gt;quit; &lt;/PRE&gt;
&lt;P&gt;However your output want dataset doesn't seem to make sense to me. &amp;nbsp;Why would G1=30, when there is 12 and 30?&lt;/P&gt;
&lt;P&gt;Much like G4, how can you get 94 when there are 9 values to add together?&lt;/P&gt;</description>
      <pubDate>Mon, 11 Sep 2017 10:21:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394620#M95115</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-09-11T10:21:26Z</dc:date>
    </item>
    <item>
      <title>Re: Getting max value by group - complex table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394623#M95116</link>
      <description>&lt;P&gt;I have received the output where &amp;nbsp;sums were calculated but I need &lt;STRONG&gt;max values within each group&lt;/STRONG&gt;. That is the case.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="branch"&gt;&lt;TABLE border="0" cellspacing="1" cellpadding="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;The SAS System&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;DIV align="center"&gt;Obs group VALUE 1 2 3 4 &lt;TABLE cellspacing="0" cellpadding="5"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;g1&lt;/TD&gt;&lt;TD&gt;42&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;g2&lt;/TD&gt;&lt;TD&gt;192&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;g3&lt;/TD&gt;&lt;TD&gt;122&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;g4&lt;/TD&gt;&lt;TD&gt;386&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 11 Sep 2017 10:34:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394623#M95116</guid>
      <dc:creator>DmytroYermak</dc:creator>
      <dc:date>2017-09-11T10:34:01Z</dc:date>
    </item>
    <item>
      <title>Re: Getting max value by group - complex table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394633#M95118</link>
      <description>&lt;P&gt;Ah, I see, then:&lt;/P&gt;
&lt;PRE&gt;data test;
  infile datalines missover;
  input group $ value1 value2 value3 value4;
datalines;
g1 12 30
g2 14 37 15
g2 16 76 34
g3 12 22
g3 4 84
g4 7 15 10
g4 24 21 17 67
g4 14 18 94
g4 26 73 
;
run;

data want (keep=group max_val);
  set test;
  by group;
  retain max_val;
  if first.group then max_val=.;
  if max(of value:) &amp;gt; max_val then max_val=max(of value:);
  if last.group then output;
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Sep 2017 11:09:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394633#M95118</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-09-11T11:09:06Z</dc:date>
    </item>
    <item>
      <title>Re: Getting max value by group - complex table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394637#M95119</link>
      <description>&lt;P&gt;I'm not sure this is all that different:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;do until (last.group);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;by group;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;maxval = max(maxval, of value1-value4);&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;keep group maxval;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Sep 2017 11:15:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394637#M95119</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-09-11T11:15:24Z</dc:date>
    </item>
    <item>
      <title>Re: Getting max value by group - complex table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394658#M95125</link>
      <description>It is definitely a solution as well. Thank you!</description>
      <pubDate>Mon, 11 Sep 2017 13:09:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394658#M95125</guid>
      <dc:creator>DmytroYermak</dc:creator>
      <dc:date>2017-09-11T13:09:32Z</dc:date>
    </item>
    <item>
      <title>Re: Getting max value by group - complex table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394659#M95126</link>
      <description>Thank you, very useful!</description>
      <pubDate>Mon, 11 Sep 2017 13:09:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394659#M95126</guid>
      <dc:creator>DmytroYermak</dc:creator>
      <dc:date>2017-09-11T13:09:59Z</dc:date>
    </item>
    <item>
      <title>Re: Getting max value by group - complex table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394687#M95135</link>
      <description>&lt;P&gt;That looks clever, but I don't understand it. &amp;nbsp;How does BY function inside a DO loop? &amp;nbsp;I've never seen that before.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;--Dav&lt;/P&gt;</description>
      <pubDate>Mon, 11 Sep 2017 14:08:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394687#M95135</guid>
      <dc:creator>Davanden</dc:creator>
      <dc:date>2017-09-11T14:08:41Z</dc:date>
    </item>
    <item>
      <title>Re: Getting max value by group - complex table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394693#M95136</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/69701"&gt;@Davanden&lt;/a&gt;, here is how i understand it :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;do until (last.group);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;by group;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;maxval = max(maxval, of value1-value4);&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The interest of this loop is to treat an entire group for each iteration of the datastep.&lt;/P&gt;
&lt;P&gt;By putting the set ... by instructions inside the loop, you can read each record&lt;/P&gt;
&lt;P&gt;of the group without outputting or reinitializing the PDV so that the value of MAXVAL for the group is kept&lt;/P&gt;
&lt;P&gt;between records..&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is only at the run instruction that the result is output and the PDV is reset before handling the next group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You end up with a shorter program than if you explicitely reset Maxval on first.group and output&lt;/P&gt;
&lt;P&gt;on last.group.&lt;/P&gt;
&lt;P&gt;Clever indeed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Sep 2017 14:36:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394693#M95136</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2017-09-11T14:36:14Z</dc:date>
    </item>
    <item>
      <title>Re: Getting max value by group - complex table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394707#M95142</link>
      <description>&lt;P&gt;Each SET statement is permitted to use a BY statement.&amp;nbsp; That sets up FIRST. and LAST. variables (regardless of whether inside a loop or not).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One of the key issues here is how the SET statement operates.&amp;nbsp; It is not a label that shows where the data is coming from.&amp;nbsp; Rather, it is an instruction to read the next observation from a SAS data set.&amp;nbsp; It only reads one observation at a time (whether inside or outside a DO loop).&amp;nbsp; So the BY statement merely identifies whether that observation is the first (or last) one for a value of GROUP.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;***** EDITED HERE:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe I shouldn't add this ... it might add to the confusion.&amp;nbsp; Yet some of the community will find this interesting.&amp;nbsp; Technically, the BY statement refers to the most recent SET / MERGE / UPDATE statement.&amp;nbsp; There is no reason that it has to immediatley follow a SET, MERGE, or UPDATE statement.&amp;nbsp; So this variation would work exactly the same way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;do until (last.group);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;maxval = max(maxval, of value1-value4);&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;by group;&lt;/P&gt;
&lt;P&gt;keep group maxval;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Sep 2017 15:13:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394707#M95142</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-09-11T15:13:43Z</dc:date>
    </item>
    <item>
      <title>Re: Getting max value by group - complex table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394780#M95146</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/58513"&gt;@DmytroYermak&lt;/a&gt;&amp;nbsp; My apologies for the late entry to the party. Proc sql is the best solution for this kind of problem coz you can avoid a sort and can do it one select query-&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; test;&lt;/P&gt;&lt;P&gt;infile datalines missover;&lt;/P&gt;&lt;P&gt;input group $ value1 value2 value3 value4;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;g1 12 30&lt;/P&gt;&lt;P&gt;g2 14 37 15&lt;/P&gt;&lt;P&gt;g2 16 76 34&lt;/P&gt;&lt;P&gt;g3 12 22&lt;/P&gt;&lt;P&gt;g3 4 84&lt;/P&gt;&lt;P&gt;g4 7 15 10&lt;/P&gt;&lt;P&gt;g4 24 21 17 67&lt;/P&gt;&lt;P&gt;g4 14 18 94&lt;/P&gt;&lt;P&gt;g4 26 73&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;proc&lt;/STRONG&gt; &lt;STRONG&gt;sql&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;create table want as&lt;/P&gt;&lt;P&gt;select group, max(value1, value2, value3, value4) as value&lt;/P&gt;&lt;P&gt;from test&lt;/P&gt;&lt;P&gt;group by group&lt;/P&gt;&lt;P&gt;having value=max(value);&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;Regards,&lt;/P&gt;&lt;P&gt;Naveen Srinivasan&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Sep 2017 17:41:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394780#M95146</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2017-09-11T17:41:26Z</dc:date>
    </item>
    <item>
      <title>Re: Getting max value by group - complex table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394793#M95148</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One thing to be wary of ... I believe the SQL approach can give you multiple observations for a GROUP, if you have a tie for the largest value.&amp;nbsp; If you're good enough at SQL (which rules me out), you might be able to get around that using DISCREET.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Sep 2017 17:58:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-max-value-by-group-complex-table/m-p/394793#M95148</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-09-11T17:58:43Z</dc:date>
    </item>
  </channel>
</rss>

