<?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 to get 2nd highest amount in group wise using data step and proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-2nd-highest-amount-in-group-wise-using-data-step-and/m-p/306860#M65669</link>
    <description>&lt;P&gt;I want to select the second highest amount for each aid i wrote below code but it is giving me error :&lt;/P&gt;&lt;P&gt;WARNING: A GROUP BY clause has been transformed into an ORDER BY clause because neither the SELECT&lt;BR /&gt;clause nor the optional HAVING clause of the associated table-expression referenced a&lt;BR /&gt;summary function&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code :&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data ds1;&lt;BR /&gt;input aid trndt mmddyy6. amount;&lt;BR /&gt;format trndt ddmmyy6.;&lt;BR /&gt;datalines;&lt;BR /&gt;1 102416 100&lt;BR /&gt;1 102216 600&lt;BR /&gt;1 102316 300&lt;BR /&gt;1 101016 500&lt;BR /&gt;2 102416 100&lt;BR /&gt;2 102616 600&lt;BR /&gt;2 102316 300&lt;BR /&gt;2 101016 500&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;select *&lt;BR /&gt;from ds1&lt;BR /&gt;where&lt;BR /&gt;trndt between today() and (today()-7)&lt;BR /&gt;and amount in (select max(amount)from ds1 where amount not in (select max(amount)from ds1))&lt;BR /&gt;group by aid,amount ;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 24 Oct 2016 16:02:32 GMT</pubDate>
    <dc:creator>atul_desh</dc:creator>
    <dc:date>2016-10-24T16:02:32Z</dc:date>
    <item>
      <title>how to get 2nd highest amount in group wise using data step and proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-2nd-highest-amount-in-group-wise-using-data-step-and/m-p/306860#M65669</link>
      <description>&lt;P&gt;I want to select the second highest amount for each aid i wrote below code but it is giving me error :&lt;/P&gt;&lt;P&gt;WARNING: A GROUP BY clause has been transformed into an ORDER BY clause because neither the SELECT&lt;BR /&gt;clause nor the optional HAVING clause of the associated table-expression referenced a&lt;BR /&gt;summary function&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code :&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data ds1;&lt;BR /&gt;input aid trndt mmddyy6. amount;&lt;BR /&gt;format trndt ddmmyy6.;&lt;BR /&gt;datalines;&lt;BR /&gt;1 102416 100&lt;BR /&gt;1 102216 600&lt;BR /&gt;1 102316 300&lt;BR /&gt;1 101016 500&lt;BR /&gt;2 102416 100&lt;BR /&gt;2 102616 600&lt;BR /&gt;2 102316 300&lt;BR /&gt;2 101016 500&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;select *&lt;BR /&gt;from ds1&lt;BR /&gt;where&lt;BR /&gt;trndt between today() and (today()-7)&lt;BR /&gt;and amount in (select max(amount)from ds1 where amount not in (select max(amount)from ds1))&lt;BR /&gt;group by aid,amount ;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Oct 2016 16:02:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-2nd-highest-amount-in-group-wise-using-data-step-and/m-p/306860#M65669</guid>
      <dc:creator>atul_desh</dc:creator>
      <dc:date>2016-10-24T16:02:32Z</dc:date>
    </item>
    <item>
      <title>Re: how to get 2nd highest amount in group wise using data step and proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-2nd-highest-amount-in-group-wise-using-data-step-and/m-p/306879#M65681</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;The amount for the condition "where trndt between today() and (today()-7)" is never the second highest.&lt;/P&gt;&lt;P&gt;I wouldn't use SQL for this.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds1;
   input aid trndt mmddyy6. amount;
   format trndt ddmmyy6.;
   datalines;
   1 102416 100
   1 102216 600
   1 102316 300
   1 101016 500
   2 102416 100
   2 102616 600
   2 102316 300
   2 101016 500
run;

proc sort data=ds1; by aid descending amount; run;

data want;
   set ds1;
   by aid descending amount;
   retain counter; 
   if first.aid then counter=0;
   counter+1;
   if (today()-7)&amp;lt;=trndt&amp;lt;=today() and counter eq 2 then want=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt;Oligolas&lt;/P&gt;</description>
      <pubDate>Mon, 24 Oct 2016 16:45:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-2nd-highest-amount-in-group-wise-using-data-step-and/m-p/306879#M65681</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2016-10-24T16:45:51Z</dc:date>
    </item>
    <item>
      <title>Re: how to get 2nd highest amount in group wise using data step and proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-2nd-highest-amount-in-group-wise-using-data-step-and/m-p/306948#M65695</link>
      <description>&lt;P&gt;thanks but what if highest 'amount' was there two times then counter will give wrong result.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Oct 2016 20:09:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-2nd-highest-amount-in-group-wise-using-data-step-and/m-p/306948#M65695</guid>
      <dc:creator>atul_desh</dc:creator>
      <dc:date>2016-10-24T20:09:58Z</dc:date>
    </item>
    <item>
      <title>Re: how to get 2nd highest amount in group wise using data step and proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-2nd-highest-amount-in-group-wise-using-data-step-and/m-p/307041#M65726</link>
      <description>&lt;P&gt;OK then just catch this case&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds1;
   input aid trndt mmddyy6. amount;
   format trndt ddmmyy6.;
   datalines;
   1 102416 100
   1 102216 600
   1 102316 300
   1 101016 500
   2 102416 100
   2 102616 600
   2 102316 300
   2 101016 500
run;

proc sort data=ds1; by aid descending amount; run;

data want;
   length lag1_amount 8;
   set ds1;
   by aid descending amount;
   retain counter; 
   lag1_amount=lag1(amount);
   if first.aid then counter=1;
   else if lag1_amount ne amount then counter+1;
   if (today()-7)&amp;lt;=trndt&amp;lt;=today() and counter eq 2 then want=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Oct 2016 06:42:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-2nd-highest-amount-in-group-wise-using-data-step-and/m-p/307041#M65726</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2016-10-25T06:42:00Z</dc:date>
    </item>
    <item>
      <title>Re: how to get 2nd highest amount in group wise using data step and proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-2nd-highest-amount-in-group-wise-using-data-step-and/m-p/307074#M65733</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds1;
input aid trndt mmddyy6. amount;
format trndt ddmmyy6.;
datalines;
1 102416 100
1 102216 600
1 102316 300
1 101016 500
2 102416 100
2 102616 600
2 102316 300
2 101016 500
;
run;
proc sql;
select * from
(
select * from ds1
except
select *
 from ds1 
  group by aid
   having amount=max(amount)
)
group by aid
 having amount=max(amount)
;
quit;



/***********/
proc sort data=ds1;
 by aid descending amount;
run;
data want;
 set ds1;
 by aid descending amount;
 if first.aid then n=0;
 if first.amount then n+1;
 if n=2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Oct 2016 09:48:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-2nd-highest-amount-in-group-wise-using-data-step-and/m-p/307074#M65733</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-10-25T09:48:31Z</dc:date>
    </item>
    <item>
      <title>Re: how to get 2nd highest amount in group wise using data step and proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-2nd-highest-amount-in-group-wise-using-data-step-and/m-p/307371#M65815</link>
      <description>&lt;P&gt;Thanks Ksharp,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but problem is I want 2nd highest from only those which happened last week "&lt;SPAN&gt;trndt between today() and (today()-7)"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if I put this above condition it won't give me any o/p becuase 2nd highest amount is not from last week.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;so i'm confused like where we can put 'where' condition.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Again Thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Oct 2016 13:08:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-2nd-highest-amount-in-group-wise-using-data-step-and/m-p/307371#M65815</guid>
      <dc:creator>atul_desh</dc:creator>
      <dc:date>2016-10-26T13:08:04Z</dc:date>
    </item>
    <item>
      <title>Re: how to get 2nd highest amount in group wise using data step and proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-2nd-highest-amount-in-group-wise-using-data-step-and/m-p/307387#M65823</link>
      <description>&lt;P&gt;data ds1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; input aid trndt mmddyy6. amount;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; format trndt ddmmyy6.;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; datalines;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; 1 102416 100&lt;BR /&gt;&amp;nbsp;&amp;nbsp; 1 102216 600&lt;BR /&gt;&amp;nbsp;&amp;nbsp; 1 102316 300&lt;BR /&gt;&amp;nbsp;&amp;nbsp; 1 101016 500&lt;BR /&gt;&amp;nbsp;&amp;nbsp; 2 102416 100&lt;BR /&gt;&amp;nbsp;&amp;nbsp; 2 102616 600&lt;BR /&gt;&amp;nbsp;&amp;nbsp; 2 102316 300&lt;BR /&gt;&amp;nbsp;&amp;nbsp; 2 101016 500&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc sort data=ds1; by aid descending amount; run;&lt;BR /&gt;&lt;BR /&gt;DATA want;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; length lag1_amount 8;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; set ds1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; where (today()-7)&amp;lt;=trndt&amp;lt;=today();&lt;BR /&gt;&amp;nbsp;&amp;nbsp; by aid descending amount;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; retain counter;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; lag1_amount=lag1(amount);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; if first.aid then counter=1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; else if lag1_amount ne amount then counter+1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; if counter eq 2 then want=1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; if want eq 1;&lt;BR /&gt;RUN;&lt;BR /&gt;&lt;BR /&gt;/*KSharp's*/&lt;BR /&gt;&lt;BR /&gt;PROC SQL;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; SELECT *&lt;BR /&gt;&amp;nbsp;&amp;nbsp; FROM&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (SELECT *&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM ds1(&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE=((today()-7)&amp;lt;=trndt&amp;lt;=today()))&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; EXCEPT SELECT *&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM ds1(&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE=((today()-7)&amp;lt;=trndt&amp;lt;=today()))&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GROUP BY aid&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HAVING amount=max(amount))&lt;BR /&gt;&amp;nbsp;&amp;nbsp; GROUP BY aid&lt;BR /&gt;&amp;nbsp;&amp;nbsp; HAVING amount=max(amount)&lt;BR /&gt;&amp;nbsp;&amp;nbsp; ;&lt;BR /&gt;QUIT;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Oct 2016 13:41:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-2nd-highest-amount-in-group-wise-using-data-step-and/m-p/307387#M65823</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2016-10-26T13:41:30Z</dc:date>
    </item>
  </channel>
</rss>

