<?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 how do i get max1 and max2 values from each group using grouping(first. and last.) in datastep in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-max1-and-max2-values-from-each-group-using-grouping/m-p/552691#M153644</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;My requirement is i want to display max1 and max2 values from each group using grouping in datastep. Here is my dataset:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data devil;&lt;BR /&gt;infile datalines dlm=' ' dsd;&lt;BR /&gt;input pincode order num_total ;&lt;BR /&gt;datalines;&lt;BR /&gt;12345 1 99&lt;BR /&gt;23451 1 1&lt;BR /&gt;34512 1 10&lt;BR /&gt;45123 2 10&lt;BR /&gt;51234 2 13&lt;BR /&gt;67890 2 10&lt;BR /&gt;78901 3 11&lt;BR /&gt;89012 3 10&lt;BR /&gt;90123 3 20&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Expected output:&lt;/P&gt;&lt;P&gt;pincode&amp;nbsp; order num_total&lt;/P&gt;&lt;P&gt;12345 1 99&lt;BR /&gt;34512 1 10&lt;/P&gt;&lt;P&gt;51234 2 13&lt;BR /&gt;45123 2 10&lt;BR /&gt;67890 2 10&lt;/P&gt;&lt;P&gt;90123 3 20&lt;BR /&gt;78901 3 11&lt;/P&gt;</description>
    <pubDate>Sun, 21 Apr 2019 12:02:27 GMT</pubDate>
    <dc:creator>ravi_2710</dc:creator>
    <dc:date>2019-04-21T12:02:27Z</dc:date>
    <item>
      <title>how do i get max1 and max2 values from each group using grouping(first. and last.) in datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-max1-and-max2-values-from-each-group-using-grouping/m-p/552691#M153644</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;My requirement is i want to display max1 and max2 values from each group using grouping in datastep. Here is my dataset:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data devil;&lt;BR /&gt;infile datalines dlm=' ' dsd;&lt;BR /&gt;input pincode order num_total ;&lt;BR /&gt;datalines;&lt;BR /&gt;12345 1 99&lt;BR /&gt;23451 1 1&lt;BR /&gt;34512 1 10&lt;BR /&gt;45123 2 10&lt;BR /&gt;51234 2 13&lt;BR /&gt;67890 2 10&lt;BR /&gt;78901 3 11&lt;BR /&gt;89012 3 10&lt;BR /&gt;90123 3 20&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Expected output:&lt;/P&gt;&lt;P&gt;pincode&amp;nbsp; order num_total&lt;/P&gt;&lt;P&gt;12345 1 99&lt;BR /&gt;34512 1 10&lt;/P&gt;&lt;P&gt;51234 2 13&lt;BR /&gt;45123 2 10&lt;BR /&gt;67890 2 10&lt;/P&gt;&lt;P&gt;90123 3 20&lt;BR /&gt;78901 3 11&lt;/P&gt;</description>
      <pubDate>Sun, 21 Apr 2019 12:02:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-max1-and-max2-values-from-each-group-using-grouping/m-p/552691#M153644</guid>
      <dc:creator>ravi_2710</dc:creator>
      <dc:date>2019-04-21T12:02:27Z</dc:date>
    </item>
    <item>
      <title>Re: how do i get max1 and max2 values from each group using grouping(first. and last.) in datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-max1-and-max2-values-from-each-group-using-grouping/m-p/552697#M153650</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=devil;
    by order descending num_total;
run;

data want;
    set devil;
    by order;
    if first.order then seq=0;
    seq+1;
    if seq&amp;lt;=2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This may or may not handle ties the way you want, but you didn't really say what you want to do in the case of ties.&lt;/P&gt;</description>
      <pubDate>Sun, 21 Apr 2019 16:35:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-max1-and-max2-values-from-each-group-using-grouping/m-p/552697#M153650</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-04-21T16:35:55Z</dc:date>
    </item>
    <item>
      <title>Re: how do i get max1 and max2 values from each group using grouping(first. and last.) in datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-max1-and-max2-values-from-each-group-using-grouping/m-p/552699#M153652</link>
      <description>&lt;P&gt;Here is one way. But why do you want to use a data step?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data devil;
infile datalines dlm=' ' dsd;
input pincode order num_total ;
datalines;
12345 1 99
23451 1 1
34512 1 10
45123 2 10
51234 2 13
67890 2 10
78901 3 11
89012 3 10
90123 3 20
;
run;

proc rank data=devil descending out=temp ties=dense;
   by order;
	var num_total;
   ranks rank;
run;

data want(drop=rank);
   set temp;
   where rank in (1,2);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 21 Apr 2019 13:34:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-max1-and-max2-values-from-each-group-using-grouping/m-p/552699#M153652</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-04-21T13:34:44Z</dc:date>
    </item>
    <item>
      <title>Re: how do i get max1 and max2 values from each group using grouping(first. and last.) in datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-max1-and-max2-values-from-each-group-using-grouping/m-p/552704#M153657</link>
      <description>&lt;P&gt;Another Data Step Version. This solution can work for more than 3 observations in each Group. Sorting is made to sort by ORDER and in descending order of NUM_TOTAL. It leads to choosing the first two observations giving the First Max and Second Max.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data devil;
input pincode order num_total ;
datalines;
12345 1 99
23451 1 1
34512 1 10
45123 2 10
51234 2 13
67890 2 10
78901 3 11
89012 3 10
90123 3 20
;
run;


proc sort data = devil out = temp;
by order descending num_total ;
run;


data want;
   do i = 1 by 1 until(last.order);
      set temp;
      by order;
      if i in(1,2) then output;
   end;
drop i;
run;

Obs 	pincode 	order 	num_total
1 	12345 	1 	99
2 	34512 	1 	10
3 	51234 	2 	13
4 	45123 	2 	10
5 	90123 	3 	20
6 	78901 	3 	11
&lt;/PRE&gt;</description>
      <pubDate>Sun, 21 Apr 2019 16:29:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-max1-and-max2-values-from-each-group-using-grouping/m-p/552704#M153657</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-04-21T16:29:04Z</dc:date>
    </item>
    <item>
      <title>Re: how do i get max1 and max2 values from each group using grouping(first. and last.) in datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-max1-and-max2-values-from-each-group-using-grouping/m-p/552712#M153664</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I think that to handle the "order=2" case you have to change the datastep a little bit:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    
   _tmp_ = 0;
   do i = 1 by 1 until(last.order);
      set temp;
      by order descending num_total;
      _tmp_ + last.num_total;
      put _all_;
      if _tmp_ = 2 then _leave_ = i;
   end;
   put;
   put _all_;

   
   do i = 1 by 1 until(last.order);
      set temp;
      by order;
      if i &amp;lt;= _leave_ then output;
   end;

drop i _tmp_ _leave_;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;all the best&lt;/P&gt;&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Sun, 21 Apr 2019 18:50:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-max1-and-max2-values-from-each-group-using-grouping/m-p/552712#M153664</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2019-04-21T18:50:07Z</dc:date>
    </item>
    <item>
      <title>Re: how do i get max1 and max2 values from each group using grouping(first. and last.) in datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-max1-and-max2-values-from-each-group-using-grouping/m-p/552772#M153697</link>
      <description>&lt;P&gt;Hi Bart,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your suggestion gets 3 rows for ORDER = 2.&amp;nbsp; In this case, we get MAX1 = 13 and MAX2 = 10. The next value which is MAX3, is also 10.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The OP is interested to get MAX1 and MAX2. Hence I stopped with the 2 values for each order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I missing to see what you are telling, kindly explain.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;DataSP&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Apr 2019 08:45:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-max1-and-max2-values-from-each-group-using-grouping/m-p/552772#M153697</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-04-22T08:45:09Z</dc:date>
    </item>
    <item>
      <title>Re: how do i get max1 and max2 values from each group using grouping(first. and last.) in datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-max1-and-max2-values-from-each-group-using-grouping/m-p/552779#M153699</link>
      <description>&lt;P&gt;Hi DataSP,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I suggested the extension looking at OP's expected result. It looks like OP wants all results in case of ties.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;all the best&lt;/P&gt;&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 22 Apr 2019 10:11:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-max1-and-max2-values-from-each-group-using-grouping/m-p/552779#M153699</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2019-04-22T10:11:58Z</dc:date>
    </item>
    <item>
      <title>Re: how do i get max1 and max2 values from each group using grouping(first. and last.) in datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-max1-and-max2-values-from-each-group-using-grouping/m-p/552798#M153702</link>
      <description>&lt;P&gt;Hi Bart,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Somehow I missed OP's requirement for ties. The OP may use your suggested code for his requirements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried to use one DO-LOOP to achieve such a result. So I have modified test data set to check for tie-conditions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data devil;
input pincode order num_total ;
datalines;
12345 1 99
23451 1 1
34512 1 10
45123 2 10
51234 2 13
67890 2 10
78901 3 15
89012 3 15
90123 3 10
90123 3 10
90123 3  5
;
run;

proc sort data = devil out = temp;
by order descending num_total ;
run;

data want;
   t = 0;
   do until(last.order);
      set temp;
      by order;
      if t = 0 then do;
         t = num_total;
         count = 1;
         output;
      end;
      else if t = num_total then output;
      else if t &amp;gt; num_total then do;
         t = num_total;
         if count &amp;gt; 2 then leave;
         else if count = 1 then do; count + 1; output; end;
     end;
   end;
drop count t;
run;


Obs 	pincode 	order 	num_total
1 	12345 	1 	99
2 	34512 	1 	10
3 	51234 	2 	13
4 	45123 	2 	10
5 	67890 	2 	10
6 	78901 	3 	15
7 	89012 	3 	15
8 	90123 	3 	10
9 	90123 	3 	10

&lt;/PRE&gt;
&lt;P&gt;Best regards,&lt;/P&gt;
&lt;P&gt;DataSP&lt;/P&gt;</description>
      <pubDate>Mon, 22 Apr 2019 11:52:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-max1-and-max2-values-from-each-group-using-grouping/m-p/552798#M153702</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-04-22T11:52:59Z</dc:date>
    </item>
    <item>
      <title>Re: how do i get max1 and max2 values from each group using grouping(first. and last.) in datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-max1-and-max2-values-from-each-group-using-grouping/m-p/552805#M153703</link>
      <description>&lt;P&gt;Apparently, I missed the requirement about handling ties, which was not stated, but appears in the output clearly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In that case, I endorse the solution by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;using PROC RANK. I would not advise programming this yourself in a data step, it's already done for you in PROC RANK.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Apr 2019 12:22:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-max1-and-max2-values-from-each-group-using-grouping/m-p/552805#M153703</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-04-22T12:22:10Z</dc:date>
    </item>
    <item>
      <title>Re: how do i get max1 and max2 values from each group using grouping(first. and last.) in datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-max1-and-max2-values-from-each-group-using-grouping/m-p/552840#M153719</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data devil;
infile datalines dlm=' ' dsd;
input pincode order num_total ;
datalines;
12345 1 99
23451 1 1
34512 1 10
45123 2 10
51234 2 13
67890 2 10
78901 3 11
89012 3 10
90123 3 20
;
run;
proc sort data=devil out=temp;
by order descending num_total;
run;
data want;
 set temp;
 by order descending num_total notsorted;
 if first.order then n=0;
 if first.num_total then do;n+1;if n in (1 2) then output;end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 22 Apr 2019 14:06:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-i-get-max1-and-max2-values-from-each-group-using-grouping/m-p/552840#M153719</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-04-22T14:06:06Z</dc:date>
    </item>
  </channel>
</rss>

