<?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: Extract Top 10 Records of each column in an original Table. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extract-Top-10-Records-of-each-column-in-an-original-Table/m-p/416663#M102328</link>
    <description>&lt;P&gt;You could run PROC RANK one time on all variables of interest.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then it's a simple sort of the ranks to give the top N in each column.&lt;/P&gt;</description>
    <pubDate>Tue, 28 Nov 2017 12:54:29 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2017-11-28T12:54:29Z</dc:date>
    <item>
      <title>Extract Top 10 Records of each column in an original Table.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Top-10-Records-of-each-column-in-an-original-Table/m-p/416530#M102268</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;I have the following table: Description, ID #,&amp;nbsp; A, B, C, D, E, F, G. I would like one table back with the top 10 highest values of each individual column arranged the same way, ie.&amp;nbsp; &lt;SPAN&gt;Description, ID #,&amp;nbsp; Top 10 in A, Top 10 in B....Top 10 in G. How would I go about doing this? The values in A, B, C, D, E, F, G are all integers.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;Regards,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;JayCompany&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Nov 2017 22:54:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Top-10-Records-of-each-column-in-an-original-Table/m-p/416530#M102268</guid>
      <dc:creator>JayCompany</dc:creator>
      <dc:date>2017-11-27T22:54:02Z</dc:date>
    </item>
    <item>
      <title>Re: Extract Top 10 Records of each column in an original Table.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Top-10-Records-of-each-column-in-an-original-Table/m-p/416535#M102270</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/179824"&gt;@JayCompany&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;I have the following table: Description, ID #,&amp;nbsp; A, B, C, D, E, F, G. I would like one table back with the top 10 highest values of each individual column arranged the same way, ie.&amp;nbsp; &lt;SPAN&gt;Description, ID #,&amp;nbsp; Top 10 in A, Top 10 in B....Top 10 in G. How would I go about doing this? The values in A, B, C, D, E, F, G are all integers.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Show some actual example of starting data and what the result should look like. Likely only need to show the results for 2 variables.&lt;/P&gt;
&lt;P&gt;I am not sure I am visualizing your desired result very clearly&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One problem: "top 10" is not a very specific description. "top" could be the largest or the smallest depending on a ranking scale.&lt;/P&gt;
&lt;P&gt;Also, repeat values might be of concern since we can't tell if you want ties or not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Nov 2017 23:07:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Top-10-Records-of-each-column-in-an-original-Table/m-p/416535#M102270</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-11-27T23:07:35Z</dc:date>
    </item>
    <item>
      <title>Re: Extract Top 10 Records of each column in an original Table.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Top-10-Records-of-each-column-in-an-original-Table/m-p/416541#M102272</link>
      <description>&lt;P&gt;How could there be just one ID column in the final table?&amp;nbsp; Would that mean that the same ID has the top value for all variables?&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2017 00:40:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Top-10-Records-of-each-column-in-an-original-Table/m-p/416541#M102272</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-11-28T00:40:31Z</dc:date>
    </item>
    <item>
      <title>Re: Extract Top 10 Records of each column in an original Table.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Top-10-Records-of-each-column-in-an-original-Table/m-p/416542#M102273</link>
      <description>&lt;P&gt;Okay, so for example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's what I have right now:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Description, ID #,&amp;nbsp; A, B&lt;/P&gt;&lt;P&gt;Cars, X123, 500, 1000&lt;/P&gt;&lt;P&gt;Cars, X124, 600, 2000&lt;/P&gt;&lt;P&gt;Cars, X125, 200, 500&lt;/P&gt;&lt;P&gt;Cars, X126, 800, 6000&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd like this in return,&amp;nbsp; Highest 3 values from each variable A and B:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Description, ID #,&amp;nbsp; A&lt;/P&gt;&lt;P&gt;Cars, X126, 800&lt;/P&gt;&lt;P&gt;Cars, X124, 600&lt;/P&gt;&lt;P&gt;Cars, X123, 500&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Description, ID #, B&lt;/P&gt;&lt;P&gt;Cars, X126, 6000&lt;/P&gt;&lt;P&gt;Cars, X124, 2000&lt;/P&gt;&lt;P&gt;Cars, X123, 1000&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But Ideally, combined together into one Table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2017 01:00:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Top-10-Records-of-each-column-in-an-original-Table/m-p/416542#M102273</guid>
      <dc:creator>JayCompany</dc:creator>
      <dc:date>2017-11-28T01:00:28Z</dc:date>
    </item>
    <item>
      <title>Re: Extract Top 10 Records of each column in an original Table.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Top-10-Records-of-each-column-in-an-original-Table/m-p/416543#M102274</link>
      <description>&lt;P&gt;No, the same ID doesn't have to have the top value for all variables..I just want a list of all the IDS that have the top variables for each of the different columns.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2017 01:02:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Top-10-Records-of-each-column-in-an-original-Table/m-p/416543#M102274</guid>
      <dc:creator>JayCompany</dc:creator>
      <dc:date>2017-11-28T01:02:40Z</dc:date>
    </item>
    <item>
      <title>Re: Extract Top 10 Records of each column in an original Table.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Top-10-Records-of-each-column-in-an-original-Table/m-p/416556#M102283</link>
      <description>&lt;P&gt;Here is one of many ways to achieve this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Desc $ ID $ A B;
datalines;
A X123 500 1000
A X124 600 2000
A X125 200 500
A X126 800 6000
;

proc transpose data=have out=temp name=var;
by Desc id;
var a--b;
run;

proc sort data=temp; by Desc var descending col1; run;

data want; 
set temp; by desc var;
if first.var then count=0;
count + 1;
if count &amp;lt;= 3;
drop count;
rename col1=value;
run;

proc print noobs; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Nov 2017 03:43:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Top-10-Records-of-each-column-in-an-original-Table/m-p/416556#M102283</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-11-28T03:43:19Z</dc:date>
    </item>
    <item>
      <title>Re: Extract Top 10 Records of each column in an original Table.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Top-10-Records-of-each-column-in-an-original-Table/m-p/416654#M102326</link>
      <description>&lt;P&gt;One possibility is to use hash tables, you basically make an ordered hash table for each variable,&amp;nbsp;putting your values in until the number you want is reached, and&amp;nbsp;then repeatedly deleting the lowest (first) value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To keep track of the hash tables you create another hash table(TREE) , with the variable name (VAR) as key.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is an example which finds the 3 highest values of A and B in you example data:&lt;/P&gt;&lt;PRE&gt;%let n=3; /* rank that we want */
%let vars='A','B';  /* variables that we want */
%let id='Desc','ID'; /* id variable(s) */
data want;
  set have end=done;
  if _N_=1 then do;
    declare hash h;
    declare hiter i;
    length var $32 value $50;
    retain order 'A';  /* Ascending/Descending, A means highest values, D lowest */
    declare hash tree();
    tree.definekey('var');
    tree.definedata('h','i');
    tree.definedone();
    do var=&amp;amp;vars;
      h=_new_ hash(multidata: 'Y', ordered: order);
      h.definekey(var);
      h.definedata(var,&amp;amp;id);
      h.definedone();
      i=_new_ hiter('h');
      rc=tree.add();
      end;
    end;
  do var=&amp;amp;vars;
    rc=tree.find();
    rc=h.add();
    if h.num_items&amp;gt;&amp;amp;n then do;
      rc=i.first();  /* get item to delete */
      rc=i.prev(); /* release iterator so that we can remove item */
      rc=h.remove();
      end;
    if done then do;
      rc=i.last();  /* get highest/lowest item */
      do rank=1 by 1 until(i.prev());
        value=vvaluex(var); /* get formatted value */
        output;
        end;
      end;
    end;
  keep desc id var value order rank;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;I use the VVALUEX function to return formatted values. That way you can have the character representations of different variable types (e.g. character, money, integer, date) in the same character variable. If you need to find lowest values, change the ORDER variable to D.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2017 12:17:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Top-10-Records-of-each-column-in-an-original-Table/m-p/416654#M102326</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2017-11-28T12:17:01Z</dc:date>
    </item>
    <item>
      <title>Re: Extract Top 10 Records of each column in an original Table.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Top-10-Records-of-each-column-in-an-original-Table/m-p/416663#M102328</link>
      <description>&lt;P&gt;You could run PROC RANK one time on all variables of interest.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then it's a simple sort of the ranks to give the top N in each column.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2017 12:54:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Top-10-Records-of-each-column-in-an-original-Table/m-p/416663#M102328</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2017-11-28T12:54:29Z</dc:date>
    </item>
  </channel>
</rss>

