<?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: How can fetch 10th max and 10min records in datastep? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-can-fetch-10th-max-and-10min-records-in-datastep/m-p/700854#M214540</link>
    <description>&lt;P&gt;Do you want the 10 min/max salary values? Or the 10&lt;STRONG&gt;'th&amp;nbsp;&lt;/STRONG&gt;min/max salary values? If it is the first, take one of the solutions above. Here is the hash version for the latter.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  do id = 1 to 500000;
    salary = ceil(rand('uniform') * 100);
    output;
  end;
run;

data want;
   dcl hash h (dataset : 'have', ordered : 'Y');
   h.definekey('salary');
   h.definedata(all : 'Y');
   h.definedone();
   dcl hiter i ('h');

   if 0 then set have;

   do _N_ = 1 by 1 while (i.next() = 0);
      if _N_ = 10 then leave;
   end;
   output;

   _N_ = i.last();

   do _N_ = 1 by 1 while (i.prev() = 0);
      if _N_ = 9 then leave;
   end;
   output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 23 Nov 2020 08:34:30 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2020-11-23T08:34:30Z</dc:date>
    <item>
      <title>How can fetch 10th max and 10min records in datastep?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-fetch-10th-max-and-10min-records-in-datastep/m-p/700734#M214481</link>
      <description>&lt;P&gt;Suppose I having a table it contains Lakhs of records. I have a variable called salary. I want 10th max&amp;nbsp; and 10th min salary. Salary column having duplicate values. Should not sort the dataset twice.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Nov 2020 10:56:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-fetch-10th-max-and-10min-records-in-datastep/m-p/700734#M214481</guid>
      <dc:creator>MSK4</dc:creator>
      <dc:date>2020-11-22T10:56:42Z</dc:date>
    </item>
    <item>
      <title>Re: How can fetch 10th max and 10min records in datastep?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-fetch-10th-max-and-10min-records-in-datastep/m-p/700737#M214483</link>
      <description>&lt;P&gt;You just need value of salary ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
 set sashelp.class;
run;

proc univariate data=have nextrobs=2 nextrval=2 ;
var weight;
run;

&lt;/PRE&gt;</description>
      <pubDate>Sun, 22 Nov 2020 11:37:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-fetch-10th-max-and-10min-records-in-datastep/m-p/700737#M214483</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-11-22T11:37:54Z</dc:date>
    </item>
    <item>
      <title>Re: How can fetch 10th max and 10min records in datastep?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-fetch-10th-max-and-10min-records-in-datastep/m-p/700738#M214484</link>
      <description>Sort once. Then, in a data step, use the NOBS= option to retrieve the number of observations, and keep all observations (subsetting IF) where _N_ le 10 or _N_ ge nobs - 9.</description>
      <pubDate>Sun, 22 Nov 2020 11:39:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-fetch-10th-max-and-10min-records-in-datastep/m-p/700738#M214484</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-22T11:39:27Z</dc:date>
    </item>
    <item>
      <title>Re: How can fetch 10th max and 10min records in datastep?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-fetch-10th-max-and-10min-records-in-datastep/m-p/700739#M214485</link>
      <description>&lt;P&gt;Since the salary columns has duplicate values, neither of the above handles ties properly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need PROC RANK, and then you can choose how to handle ties.&lt;/P&gt;</description>
      <pubDate>Sun, 22 Nov 2020 11:43:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-fetch-10th-max-and-10min-records-in-datastep/m-p/700739#M214485</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-11-22T11:43:34Z</dc:date>
    </item>
    <item>
      <title>Re: How can fetch 10th max and 10min records in datastep?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-fetch-10th-max-and-10min-records-in-datastep/m-p/700740#M214486</link>
      <description>nextrval=2 &lt;BR /&gt;can handle ties.</description>
      <pubDate>Sun, 22 Nov 2020 11:53:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-fetch-10th-max-and-10min-records-in-datastep/m-p/700740#M214486</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-11-22T11:53:19Z</dc:date>
    </item>
    <item>
      <title>Re: How can fetch 10th max and 10min records in datastep?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-fetch-10th-max-and-10min-records-in-datastep/m-p/700742#M214488</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;nextrval=2 &lt;BR /&gt;can handle ties.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, it will tell you if there are more than one of a particular value, but that's not the same as finding the 10th lowest or 10th largest value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC RANK also gives you options on what to do when the 10th lowest or 10th highest is tied.&lt;/P&gt;</description>
      <pubDate>Sun, 22 Nov 2020 12:33:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-fetch-10th-max-and-10min-records-in-datastep/m-p/700742#M214488</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-11-22T12:33:19Z</dc:date>
    </item>
    <item>
      <title>Re: How can fetch 10th max and 10min records in datastep?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-fetch-10th-max-and-10min-records-in-datastep/m-p/700745#M214489</link>
      <description>&lt;P&gt;A hash can easily handle the distinct values amongst 100,000s of salary values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Read the salaries into an ordered hash and output the data from the first 10 and last 10 keys.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
  do id = 1 to 500000;
    salary = rand('integer', 1e5, 1e6);
    output;
  end;
run;

data salary_bookends(keep=salary index);
  declare hash salaries(dataset:'have(keep=salary)', ordered: 'A');
  salaries.defineKey('salary');
  salaries.defineDone();

  call missing(salary);

  declare hiter hi('salaries');

  top = salaries.num_items;
  do index = 1 by 1 while (hi.next()=0);
    if index &amp;lt; 11 or index &amp;gt; top-10 then output;
  end;

run;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="RichardADeVenezia_1-1606050092852.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/51912i7F9E5DF62CCE4A67/image-size/medium?v=v2&amp;amp;px=400" role="button" title="RichardADeVenezia_1-1606050092852.png" alt="RichardADeVenezia_1-1606050092852.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Nov 2020 13:02:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-fetch-10th-max-and-10min-records-in-datastep/m-p/700745#M214489</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-11-22T13:02:07Z</dc:date>
    </item>
    <item>
      <title>Re: How can fetch 10th max and 10min records in datastep?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-fetch-10th-max-and-10min-records-in-datastep/m-p/700789#M214505</link>
      <description>&lt;P&gt;Or rather...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  do id = 1 to 500000;
    /* Lots of duplicates */
    salary = rand('integer', 1, 100);
    output;
  end;
run;

data salary_bookends(keep=salary kind index);
  declare hash salaries(dataset:'have(keep=salary)', ordered: 'A');
  salaries.defineKey('salary');
  salaries.defineDone();

  call missing(salary);

  declare hiter hi('salaries');

  kind = "Min";
  hi.first();
  do index = 1 to 10 until (hi.next());
    output;
  end;
  
  kind = "Max";
  hi.last();
  do index = 1 to 10 until (hi.prev());
    output;
  end;

run;

proc print noobs data=salary_bookends; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 121px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/51917i47BD9428C41B6612/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Nov 2020 21:05:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-fetch-10th-max-and-10min-records-in-datastep/m-p/700789#M214505</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-11-22T21:05:10Z</dc:date>
    </item>
    <item>
      <title>Re: How can fetch 10th max and 10min records in datastep?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-fetch-10th-max-and-10min-records-in-datastep/m-p/700854#M214540</link>
      <description>&lt;P&gt;Do you want the 10 min/max salary values? Or the 10&lt;STRONG&gt;'th&amp;nbsp;&lt;/STRONG&gt;min/max salary values? If it is the first, take one of the solutions above. Here is the hash version for the latter.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  do id = 1 to 500000;
    salary = ceil(rand('uniform') * 100);
    output;
  end;
run;

data want;
   dcl hash h (dataset : 'have', ordered : 'Y');
   h.definekey('salary');
   h.definedata(all : 'Y');
   h.definedone();
   dcl hiter i ('h');

   if 0 then set have;

   do _N_ = 1 by 1 while (i.next() = 0);
      if _N_ = 10 then leave;
   end;
   output;

   _N_ = i.last();

   do _N_ = 1 by 1 while (i.prev() = 0);
      if _N_ = 9 then leave;
   end;
   output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Nov 2020 08:34:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-fetch-10th-max-and-10min-records-in-datastep/m-p/700854#M214540</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-11-23T08:34:30Z</dc:date>
    </item>
  </channel>
</rss>

