<?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 to find the top 10 and last 10 values from a variable? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-top-10-and-last-10-values-from-a-variable/m-p/481520#M124569</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8409"&gt;@Babloo&lt;/a&gt;: From your sample data it is not clear&amp;nbsp;whether you mean the&amp;nbsp;&lt;EM&gt;first&lt;/EM&gt;&amp;nbsp;and &lt;EM&gt;last&lt;/EM&gt;&amp;nbsp;ten or the &lt;EM&gt;smallest&lt;/EM&gt; and &lt;EM&gt;largest&lt;/EM&gt; ten values. Hence the confusion and different approaches.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;, very good point. This is a very poor example, or the words used to explain the example need to be clearer.&lt;/P&gt;</description>
    <pubDate>Thu, 26 Jul 2018 14:21:58 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2018-07-26T14:21:58Z</dc:date>
    <item>
      <title>How to find the top 10 and last 10 values from a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-top-10-and-last-10-values-from-a-variable/m-p/481432#M124534</link>
      <description>&lt;P&gt;I have a data as follows and I need to find the&amp;nbsp;top 10 and last 10 values from a variable. Appreci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lt;TD&gt;20&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;21&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;22&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;23&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;24&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Desired output:&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 96pt;" border="0" width="128" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" class="xl66" style="height: 15.0pt; width: 48pt;"&gt;top_10&lt;/TD&gt;
&lt;TD width="64" class="xl66" style="border-left: none; width: 48pt;"&gt;last_10&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl65" style="height: 15.0pt; border-top: none;"&gt;1&lt;/TD&gt;
&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;15&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl65" style="height: 15.0pt; border-top: none;"&gt;2&lt;/TD&gt;
&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;16&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl65" style="height: 15.0pt; border-top: none;"&gt;3&lt;/TD&gt;
&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;17&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl65" style="height: 15.0pt; border-top: none;"&gt;4&lt;/TD&gt;
&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;18&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl65" style="height: 15.0pt; border-top: none;"&gt;5&lt;/TD&gt;
&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;19&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl65" style="height: 15.0pt; border-top: none;"&gt;6&lt;/TD&gt;
&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;20&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl65" style="height: 15.0pt; border-top: none;"&gt;7&lt;/TD&gt;
&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;21&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl65" style="height: 15.0pt; border-top: none;"&gt;8&lt;/TD&gt;
&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;22&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl65" style="height: 15.0pt; border-top: none;"&gt;9&lt;/TD&gt;
&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;23&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl65" style="height: 15.0pt; border-top: none;"&gt;10&lt;/TD&gt;
&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;24&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Thu, 26 Jul 2018 11:18:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-top-10-and-last-10-values-from-a-variable/m-p/481432#M124534</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2018-07-26T11:18:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the top 10 and last 10 values from a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-top-10-and-last-10-values-from-a-variable/m-p/481440#M124536</link>
      <description>&lt;P&gt;Read into an array, and use the nobs=option to help in determining the last 10 items:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=first_10 last_10);
array first {10};
array last {10};
n = 0;
do until (eof);
  n + 1;
  set have nobs=nobs end=eof;
  if n &amp;lt;= 10 then first{n} = id;
  if n &amp;gt;= nobs - 9 then last{n+10-nobs} = id;
end;
do n = 1 to 10;
  first_10 = first{n};
  last_10 = last{n};
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 Jul 2018 11:37:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-top-10-and-last-10-values-from-a-variable/m-p/481440#M124536</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-07-26T11:37:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the top 10 and last 10 values from a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-top-10-and-last-10-values-from-a-variable/m-p/481443#M124537</link>
      <description>&lt;P&gt;I ran your code and it is not producing the desired results. It is generating missing values for the variables to first_10 and last_10.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 11:48:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-top-10-and-last-10-values-from-a-variable/m-p/481443#M124537</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2018-07-26T11:48:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the top 10 and last 10 values from a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-top-10-and-last-10-values-from-a-variable/m-p/481444#M124538</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC RANK can do this, and can also handle ties if that is a problem. You could also use PROC SORT to find the top 10 and bottom 10, but it will not handle ties. The code provided by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;does not seem to handle ties either.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 11:48:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-top-10-and-last-10-values-from-a-variable/m-p/481444#M124538</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-07-26T11:48:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the top 10 and last 10 values from a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-top-10-and-last-10-values-from-a-variable/m-p/481448#M124539</link>
      <description>&lt;P&gt;Could you please help me understand how will you do in proc sort?&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 12:01:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-top-10-and-last-10-values-from-a-variable/m-p/481448#M124539</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2018-07-26T12:01:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the top 10 and last 10 values from a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-top-10-and-last-10-values-from-a-variable/m-p/481452#M124540</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8409"&gt;@Babloo&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I ran your code and it is not producing the desired results. It is generating missing values for the variables to first_10 and last_10.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Maybe that's because you once again failed to provide proper example data?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
do id = 1 to 24;
output;
end;
run;

data want (keep=first_10 last_10);
array first {10};
array last {10};
n = 0;
do until (eof);
  n + 1;
  set have nobs=nobs end=eof;
  if n &amp;lt;= 10 then first{n} = id;
  if n &amp;gt;= nobs - 9 then last{n+10-nobs} = id;
end;
do n = 1 to 10;
  first_10 = first{n};
  last_10 = last{n};
  output;
end;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;first_10    last_10

    1          15  
    2          16  
    3          17  
    4          18  
    5          19  
    6          20  
    7          21  
    8          22  
    9          23  
   10          24  
&lt;/PRE&gt;
&lt;P&gt;Just what you wanted.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 12:10:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-top-10-and-last-10-values-from-a-variable/m-p/481452#M124540</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-07-26T12:10:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the top 10 and last 10 values from a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-top-10-and-last-10-values-from-a-variable/m-p/481454#M124541</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8409"&gt;@Babloo&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Could you please help me understand how will you do in proc sort?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you run PROC SORT then the first 10 values are the lowest numbers and the last 10 values are the biggest numbers. You extract the top ten and bottom ten from the list and merge them together to get your&amp;nbsp;desired result.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 12:17:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-top-10-and-last-10-values-from-a-variable/m-p/481454#M124541</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-07-26T12:17:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the top 10 and last 10 values from a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-top-10-and-last-10-values-from-a-variable/m-p/481461#M124542</link>
      <description>&lt;P&gt;Assuming the table has been sorted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Id;
cards;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
;

data first_10 last_10;
 set have nobs=nobs;
 if _n_ lt 11 then output first_10;
 if _n_ gt nobs-10 then output last_10;
run;
data want;
 merge first_10(rename=(id=first_10)) last_10(rename=(id=last_10));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 12:38:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-top-10-and-last-10-values-from-a-variable/m-p/481461#M124542</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-07-26T12:38:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the top 10 and last 10 values from a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-top-10-and-last-10-values-from-a-variable/m-p/481507#M124562</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8409"&gt;@Babloo&lt;/a&gt;: From your sample data it is not clear&amp;nbsp;whether you mean the&amp;nbsp;&lt;EM&gt;first&lt;/EM&gt;&amp;nbsp;and &lt;EM&gt;last&lt;/EM&gt;&amp;nbsp;ten or the &lt;EM&gt;smallest&lt;/EM&gt; and &lt;EM&gt;largest&lt;/EM&gt; ten values. Hence the confusion and different approaches.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 14:00:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-top-10-and-last-10-values-from-a-variable/m-p/481507#M124562</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-07-26T14:00:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the top 10 and last 10 values from a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-top-10-and-last-10-values-from-a-variable/m-p/481520#M124569</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8409"&gt;@Babloo&lt;/a&gt;: From your sample data it is not clear&amp;nbsp;whether you mean the&amp;nbsp;&lt;EM&gt;first&lt;/EM&gt;&amp;nbsp;and &lt;EM&gt;last&lt;/EM&gt;&amp;nbsp;ten or the &lt;EM&gt;smallest&lt;/EM&gt; and &lt;EM&gt;largest&lt;/EM&gt; ten values. Hence the confusion and different approaches.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;, very good point. This is a very poor example, or the words used to explain the example need to be clearer.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 14:21:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-top-10-and-last-10-values-from-a-variable/m-p/481520#M124569</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-07-26T14:21:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the top 10 and last 10 values from a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-top-10-and-last-10-values-from-a-variable/m-p/481521#M124570</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Id;
cards;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
;

data want;
first_10=1;
last_10=nobs-9;
output;
do _n_=2 to nobs;
set have point=_n_ nobs=nobs;
first_10=_n_;
last_10=last_10+1;
if first_10&amp;gt;10 then stop;
output;
end;
drop id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 Jul 2018 14:23:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-top-10-and-last-10-values-from-a-variable/m-p/481521#M124570</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-07-26T14:23:10Z</dc:date>
    </item>
  </channel>
</rss>

