<?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: Top N  per group using Datastep in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Top-N-per-group-using-Datastep/m-p/773198#M39706</link>
    <description>&lt;P&gt;Can you be sure that there is always only one date per month?&lt;/P&gt;</description>
    <pubDate>Sat, 09 Oct 2021 08:14:00 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2021-10-09T08:14:00Z</dc:date>
    <item>
      <title>Top N  per group using Datastep</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Top-N-per-group-using-Datastep/m-p/772681#M39676</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Can someone help me getting the 4 most recent month per group from the below example&lt;/P&gt;&lt;P&gt;The&amp;nbsp; by condition i am using is&amp;nbsp;&lt;EM&gt; lob descending business_date descending num_excpt;&lt;/EM&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data top4;
infile datalines dlm=",";
input business_date :date9. oic_name :$10. lob :$15. lead :$21. num_excpt;
format business_date date9.;
datalines;
31Jan2021,TOMI,DEP_A,JANE,10
31Jan2021,JAKE,DEP_A,JANE,8
31Jan2021,JENY,DEP_A,JANE,5
31Jan2021,PAUL,DEP_A,JANE,4
31Dec2020,KENY,DEP_A,JANE,3
31Dec2020,LORA,DEP_A,JANE,2
31Dec2020,RAFT,DEP_A,JANE,2
31Dec2020,JOEL,DEP_A,JANE,1
30NOV2020,YANE,DEP_A,JANE,5
30NOV2020,ALLE,DEP_A,JANE,4
30NOV2020,JOHN,DEP_A,JANE,2
30NOV2020,JOEY,DEP_A,JANE,1
31OCT2020,TEEL,DEP_A,JANE,12
31OCT2020,RAVI,DEP_A,JANE,10
31OCT2020,RUEL,DEP_A,JANE,6
31OCT2020,JANE,DEP_A,JANE,4
30SEP2020,PATTY,DEP_A,JANE,6
30SEP2020,JOLY,DEP_A,JANE,2
30SEP2020,BURT,DEP_A,JANE,1
30SEP2020,TERY,DEP_A,JANE,1
31AUG2020,JEEN,DEP_A,JANE,8
31AUG2020,LORA,DEP_A,JANE,7
31AUG2020,RAFT,DEP_A,JANE,5
31AUG2020,JOEL,DEP_A,JANE,2
31JUL2020,KENY,DEP_A,JANE,15
31JUL2020,LORA,DEP_A,JANE,10
31JUL2020,RAFT,DEP_A,JANE,8
31JUL2020,JOEL,DEP_A,JANE,6
30JUN2020,KENY,DEP_A,JANE,14
30JUN2020,LORA,DEP_A,JANE,10
30JUN2020,RAFT,DEP_A,JANE,8
30JUN2020,JOEL,DEP_A,JANE,5
31Dec2020,KENY,DEP_B,MARK,3
31Dec2020,LORA,DEP_B,MARK,2
31Dec2020,RAFT,DEP_B,MARK,2
31Dec2020,JOEL,DEP_B,MARK,1
30NOV2020,YANE,DEP_B,MARK,5
30NOV2020,ALLE,DEP_B,MARK,4
30NOV2020,JOHN,DEP_B,MARK,2
30NOV2020,JOEY,DEP_B,MARK,1
31OCT2020,TEEL,DEP_B,MARK,12
31OCT2020,RAVI,DEP_B,MARK,10
31OCT2020,RUEL,DEP_B,MARK,6
31OCT2020,JANE,DEP_B,MARK,4
30SEP2020,PATTY,DEP_B,MARK,6
30SEP2020,JOLY,DEP_B,MARK,2
30SEP2020,BURT,DEP_B,MARK,1
30SEP2020,TERY,DEP_B,MARK,1
31AUG2020,JEEN,DEP_B,MARK,8
31AUG2020,LORA,DEP_B,MARK,7
31AUG2020,RAFT,DEP_B,MARK,5
31AUG2020,JOEL,DEP_B,MARK,2
31JUL2020,KENY,DEP_B,MARK,15
31JUL2020,LORA,DEP_B,MARK,10
31JUL2020,RAFT,DEP_B,MARK,8
31JUL2020,JOEL,DEP_B,MARK,6
30JUN2020,KENY,DEP_B,MARK,14
30JUN2020,LORA,DEP_B,MARK,10
30JUN2020,RAFT,DEP_B,MARK,8
30JUN2020,JOEL,DEP_B,MARK,5
30SEP2019,PATTY,DEP_C,GAEL,6
30SEP2019,JOLY,DEP_C,GAEL,2
30SEP2019,BURT,DEP_C,GAEL,1
30SEP2019,TERY,DEP_C,GAEL,1
31AUG2019,JEEN,DEP_C,GAEL,8
31AUG2019,LORA,DEP_C,GAEL,7
31AUG2019,RAFT,DEP_C,GAEL,5
31AUG2019,JOEL,DEP_C,GAEL,2
31JUL2019,KENY,DEP_C,GAEL,15
31JUL2019,LORA,DEP_C,GAEL,10
31JUL2019,RAFT,DEP_C,GAEL,8
31JUL2019,JOEL,DEP_C,GAEL,6
30JUN2019,KENY,DEP_C,GAEL,14
30JUN2019,LORA,DEP_C,GAEL,10
30JUN2019,RAFT,DEP_C,GAEL,8
30JUN2019,JOEL,DEP_C,GAEL,5
31MAY2019,KENY,DEP_C,GAEL,3
31MAY2019,LORA,DEP_C,GAEL,2
31MAY2019,RAFT,DEP_C,GAEL,2
31MAY2019,JOEL,DEP_C,GAEL,1
30APR2019,YANE,DEP_C,GAEL,5
30NOV2019,ALLE,DEP_C,GAEL,4
30NOV2019,JOHN,DEP_C,GAEL,2
30NOV2020,JOEY,DEP_C,GAEL,1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to output the below (4 most recent month for each lob)&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="YNWA1_0-1633604619381.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/64446i6DE3E367D15E87E4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="YNWA1_0-1633604619381.png" alt="YNWA1_0-1633604619381.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Oct 2021 11:09:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Top-N-per-group-using-Datastep/m-p/772681#M39676</guid>
      <dc:creator>YNWA1</dc:creator>
      <dc:date>2021-10-07T11:09:46Z</dc:date>
    </item>
    <item>
      <title>Re: Top N  per group using Datastep</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Top-N-per-group-using-Datastep/m-p/772683#M39677</link>
      <description>&lt;P&gt;PROC FREQ does this job well -- &lt;A href="https://blogs.sas.com/content/iml/2018/06/04/top-10-table-bar-chart.html" target="_self"&gt;check this example&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For more variations, &lt;A href="https://support.sas.com/kb/33/009.html" target="_self"&gt;here's another sample on the SAS support site&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Oct 2021 11:24:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Top-N-per-group-using-Datastep/m-p/772683#M39677</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2021-10-07T11:24:56Z</dc:date>
    </item>
    <item>
      <title>Re: Top N  per group using Datastep</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Top-N-per-group-using-Datastep/m-p/772687#M39678</link>
      <description>&lt;P&gt;You already sorted correctly, so you only need a RETAINed variable to keep the most recent date:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=top4;
by lob descending business_date descending num_excpt;
run;

data want;
set top4;
by lob;
retain dt;
if first.lob
then dt = business_date;
else if intck('month',business_date,dt) &amp;gt; 3 then delete;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Oct 2021 11:38:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Top-N-per-group-using-Datastep/m-p/772687#M39678</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-10-07T11:38:32Z</dc:date>
    </item>
    <item>
      <title>Re: Top N  per group using Datastep</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Top-N-per-group-using-Datastep/m-p/772923#M39693</link>
      <description>&lt;P&gt;Hi KurtBremser,&lt;/P&gt;&lt;P&gt;Thanks for the solution. It makes sense.&lt;/P&gt;&lt;P&gt;I have used the below code to generate same result. I would like to get your thought if this is less efficient compared to your solution.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=top4;
by lob descending business_date descending num_excpt;
run;

data test;
set top4;
by lob descending business_date descending num_excpt;
if first.business_date then month+1;
if first.lob then month=1;
if month&amp;lt;=6 then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Oct 2021 02:36:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Top-N-per-group-using-Datastep/m-p/772923#M39693</guid>
      <dc:creator>YNWA1</dc:creator>
      <dc:date>2021-10-08T02:36:06Z</dc:date>
    </item>
    <item>
      <title>Re: Top N  per group using Datastep</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Top-N-per-group-using-Datastep/m-p/772932#M39695</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/394439"&gt;@YNWA1&lt;/a&gt;&amp;nbsp;What would happen should there ever be a month missing in your data?&lt;/P&gt;</description>
      <pubDate>Fri, 08 Oct 2021 04:34:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Top-N-per-group-using-Datastep/m-p/772932#M39695</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-10-08T04:34:54Z</dc:date>
    </item>
    <item>
      <title>Re: Top N  per group using Datastep</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Top-N-per-group-using-Datastep/m-p/773198#M39706</link>
      <description>&lt;P&gt;Can you be sure that there is always only one date per month?&lt;/P&gt;</description>
      <pubDate>Sat, 09 Oct 2021 08:14:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Top-N-per-group-using-Datastep/m-p/773198#M39706</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-10-09T08:14:00Z</dc:date>
    </item>
    <item>
      <title>Re: Top N  per group using Datastep</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Top-N-per-group-using-Datastep/m-p/773203#M39707</link>
      <description>&lt;P&gt;If you use PROC RANK, you have options on how to handle ties. If you use a DATA step or PROC FREQ, you have no such options.&lt;/P&gt;</description>
      <pubDate>Sat, 09 Oct 2021 09:45:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Top-N-per-group-using-Datastep/m-p/773203#M39707</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-10-09T09:45:51Z</dc:date>
    </item>
    <item>
      <title>Re: Top N  per group using Datastep</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Top-N-per-group-using-Datastep/m-p/773286#M39709</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;My report is based on month end data and getting a count of exceptions for last 6 months per lob (line of business).&lt;/P&gt;&lt;P&gt;There can be cases where there are no exception at all for certain month.&lt;/P&gt;</description>
      <pubDate>Sun, 10 Oct 2021 21:27:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Top-N-per-group-using-Datastep/m-p/773286#M39709</guid>
      <dc:creator>YNWA1</dc:creator>
      <dc:date>2021-10-10T21:27:44Z</dc:date>
    </item>
    <item>
      <title>Re: Top N  per group using Datastep</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Top-N-per-group-using-Datastep/m-p/773319#M39714</link>
      <description>&lt;P&gt;Then you should use my method with the calculated month difference.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Oct 2021 06:30:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Top-N-per-group-using-Datastep/m-p/773319#M39714</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-10-11T06:30:07Z</dc:date>
    </item>
  </channel>
</rss>

