<?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 Record Extraction based on multiple fields. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Top-Record-Extraction-based-on-multiple-fields/m-p/677925#M204562</link>
    <description>&lt;P&gt;&amp;nbsp;I modified yours-&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=temp.own2_for_sample3; 
by company Year descending salary;
run;

data temp.own2_for_sample3; 
set temp.own2_for_sample3;
by company Year ;
if first.year then rnk=0;
rnk +1;
if rnk&amp;lt;=2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 19 Aug 2020 23:53:28 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2020-08-19T23:53:28Z</dc:date>
    <item>
      <title>Top Record Extraction based on multiple fields.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Top-Record-Extraction-based-on-multiple-fields/m-p/677919#M204558</link>
      <description>&lt;P&gt;I have data that I sorted by Company Year and Salary Descending.&lt;/P&gt;
&lt;P&gt;I want to extract only the top 2 salary records for each company and year to a new table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My data looks like:&lt;/P&gt;
&lt;P&gt;Company&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Year&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Salary&lt;/P&gt;
&lt;P&gt;1&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;&amp;nbsp;&amp;nbsp; 2020&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 10,000&lt;/P&gt;
&lt;P&gt;1&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;&amp;nbsp;&amp;nbsp; 2020&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5,000&lt;/P&gt;
&lt;P&gt;1&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;&amp;nbsp;&amp;nbsp; 2020&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3,000&lt;/P&gt;
&lt;P&gt;1&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;&amp;nbsp;&amp;nbsp; 2020&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2,000&lt;/P&gt;
&lt;P&gt;1&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;&amp;nbsp;&amp;nbsp; 2019&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 9,000&lt;/P&gt;
&lt;P&gt;1&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;&amp;nbsp;&amp;nbsp; 2019&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5,000&lt;/P&gt;
&lt;P&gt;1&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;&amp;nbsp;&amp;nbsp; 2019&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3,000&lt;/P&gt;
&lt;P&gt;2&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;&amp;nbsp;&amp;nbsp; 2020&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8,000&lt;/P&gt;
&lt;P&gt;2&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;&amp;nbsp;&amp;nbsp; 2020&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5,000&lt;/P&gt;
&lt;P&gt;2&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;&amp;nbsp;&amp;nbsp; 2020&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3,000&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My desired outcome is my new table will only have the records for 10,000 and 5,000 for company 1 2020, 9,000 and 5,000 for company 1 for 2019, and 8,000 and 5,000 for company 2 in 2020&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried the following:&lt;/P&gt;
&lt;P&gt;proc sort data=temp.own2_for_sample3; by company Year descending salary;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data temp.own2_for_sample3; set temp.own2_for_sample3;&lt;BR /&gt;by company Year salary;&lt;BR /&gt;if first.year then rnk=0;&lt;BR /&gt;rnk +1;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It appears to work.... however I am afraid that since I'm only doing first.year and not including the first.company it could mess up. Any easy modifications?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 19 Aug 2020 23:43:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Top-Record-Extraction-based-on-multiple-fields/m-p/677919#M204558</guid>
      <dc:creator>anweinbe</dc:creator>
      <dc:date>2020-08-19T23:43:33Z</dc:date>
    </item>
    <item>
      <title>Re: Top Record Extraction based on multiple fields.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Top-Record-Extraction-based-on-multiple-fields/m-p/677920#M204559</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 do _n_=1 by 1 until(last.year);
  set have;
  by company  year;
  if _n_&amp;lt;=2 then output;
 end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Aug 2020 23:46:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Top-Record-Extraction-based-on-multiple-fields/m-p/677920#M204559</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-08-19T23:46:55Z</dc:date>
    </item>
    <item>
      <title>Re: Top Record Extraction based on multiple fields.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Top-Record-Extraction-based-on-multiple-fields/m-p/677922#M204560</link>
      <description>&lt;P&gt;Actually your solution is neat and simple&lt;/P&gt;</description>
      <pubDate>Wed, 19 Aug 2020 23:48:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Top-Record-Extraction-based-on-multiple-fields/m-p/677922#M204560</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-08-19T23:48:05Z</dc:date>
    </item>
    <item>
      <title>Re: Top Record Extraction based on multiple fields.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Top-Record-Extraction-based-on-multiple-fields/m-p/677924#M204561</link>
      <description>&lt;P&gt;To solve for my issue... do you think if I do If last.year OR if last.company then rnk=0?&lt;/P&gt;</description>
      <pubDate>Wed, 19 Aug 2020 23:51:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Top-Record-Extraction-based-on-multiple-fields/m-p/677924#M204561</guid>
      <dc:creator>anweinbe</dc:creator>
      <dc:date>2020-08-19T23:51:06Z</dc:date>
    </item>
    <item>
      <title>Re: Top Record Extraction based on multiple fields.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Top-Record-Extraction-based-on-multiple-fields/m-p/677925#M204562</link>
      <description>&lt;P&gt;&amp;nbsp;I modified yours-&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=temp.own2_for_sample3; 
by company Year descending salary;
run;

data temp.own2_for_sample3; 
set temp.own2_for_sample3;
by company Year ;
if first.year then rnk=0;
rnk +1;
if rnk&amp;lt;=2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Aug 2020 23:53:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Top-Record-Extraction-based-on-multiple-fields/m-p/677925#M204562</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-08-19T23:53:28Z</dc:date>
    </item>
    <item>
      <title>Re: Top Record Extraction based on multiple fields.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Top-Record-Extraction-based-on-multiple-fields/m-p/677926#M204563</link>
      <description>&lt;P&gt;My worry is that I could have a situation where the company changes but not the year.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Would this fix that worry? I think it does....&lt;/P&gt;
&lt;P&gt;if first._year or first.company then rnk=0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Aug 2020 23:56:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Top-Record-Extraction-based-on-multiple-fields/m-p/677926#M204563</guid>
      <dc:creator>anweinbe</dc:creator>
      <dc:date>2020-08-19T23:56:15Z</dc:date>
    </item>
    <item>
      <title>Re: Top Record Extraction based on multiple fields.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Top-Record-Extraction-based-on-multiple-fields/m-p/677927#M204564</link>
      <description>&lt;P&gt;You know about first.year, so consider also using lag(first.year):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=temp.own2_for_sample3;
  by company Year descending salary;
run;

data temp.own2_for_sample3; 
   set temp.own2_for_sample3;
   by company Year ;
   if first.year or lag(first.year);
   rnk=first.year+2*lag(first.year);  /* Editted in the "2*" factor */
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 Aug 2020 19:38:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Top-Record-Extraction-based-on-multiple-fields/m-p/677927#M204564</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-08-20T19:38:23Z</dc:date>
    </item>
    <item>
      <title>Re: Top Record Extraction based on multiple fields.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Top-Record-Extraction-based-on-multiple-fields/m-p/677928#M204565</link>
      <description>&lt;P&gt;Basically you could have-&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. N companies&lt;/P&gt;
&lt;P&gt;2. You need to understand the SORT mechanics&lt;/P&gt;
&lt;P&gt;3.&amp;nbsp;&lt;SPAN&gt;when you sort &lt;STRONG&gt;by Company year ,&amp;nbsp;&lt;/STRONG&gt;you end up having unique combinations of company and year forming its own group&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;4. Your &lt;STRONG&gt;descending Salary&amp;nbsp;&lt;/STRONG&gt;would order the salaries for each unique group of company-year.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;5. Now, You&amp;nbsp;want to&amp;nbsp;write the 1st 2 records for each of these unique Company-year combination.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;6. Your rnk counter safely increments for each record of the company-year combination&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;7. All you need is to apply a filter to restrict the resuts to a count of 2 or less i.e. &amp;lt;=2 and then you should have your results?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;Does the above make sense?&lt;/P&gt;</description>
      <pubDate>Thu, 20 Aug 2020 00:06:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Top-Record-Extraction-based-on-multiple-fields/m-p/677928#M204565</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-08-20T00:06:17Z</dc:date>
    </item>
  </channel>
</rss>

