<?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: Select the top highest values in each observation and output to a new dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Select-the-top-highest-values-in-each-observation-and-output-to/m-p/818835#M323235</link>
    <description>&lt;P&gt;Hi Reeza, thank you for your reply!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is giving the following output:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Obs	id	A1	A2	A3	B1	B2	B3	C1	C2	D1	D2
1	1	10	9	8	7	6	0	0	0	0	0
2	2	10	9	8	7	6	0	0	0	0	0
3	3	6	5	4	3	0	0	0	0	0	0
&lt;/PRE&gt;
&lt;P&gt;Which is different from the desired output:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;1 10  9  8  7  6  0  0  0  0  0 
2  0  0 10  9  8  7  6  0  0  0
3  0  0  0  0  6  5  4  3  0  0&lt;/PRE&gt;
&lt;P&gt;Because the values of vars A1 and A2 in observation 2 should be 0, not 10 and 9, the values of vars A1 and A2 in observation 3 should be both 0, and so on. &amp;nbsp;The top 5 values should be placed back in the same variables as they were in the 'have' dataset.&lt;/P&gt;</description>
    <pubDate>Fri, 17 Jun 2022 14:36:19 GMT</pubDate>
    <dc:creator>tonybesas</dc:creator>
    <dc:date>2022-06-17T14:36:19Z</dc:date>
    <item>
      <title>Select the top highest values in each observation and output to a new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-the-top-highest-values-in-each-observation-and-output-to/m-p/818826#M323231</link>
      <description>&lt;P&gt;I'm working a large datataset of linguistic data for a research project, and I'd like to select the n highest values for each observation, output those, turn the other values to zeros and save the results to a new dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My dataset has over 210,000 observations and 110 variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have ;
   input id A1 A2 A3 B1 B2 B3 C1 C2 D1 D2 ;
   datalines;
1 10  9  8  7  6  5  4  3  2  1 
2  0  0 10  9  8  7  6  5  4  3
3  0  0  0  0  6  5  4  3  0  0
;
run;&lt;/PRE&gt;
&lt;P&gt;If I were to select the 5 highest values for each observation in the 'have' dataset, the new dataset would look like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;1 10 9 8 7 6 0 0 0 0 0 
2 0 0 10 9 8 7 6 0 0 0
3 0 0 0 0 6 5 4 3 0 0
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if I were to select the 3 highest values for each observation, the new dataset would look like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;1 10 9 8 0 0 0 0 0 0 0 
2 0 0 10 9 8 0 0 0 0 0
3 0 0 0 0 6 5 4 0 0 0&lt;/PRE&gt;
&lt;P&gt;Thank you all ahead for your help!&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jun 2022 13:55:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-the-top-highest-values-in-each-observation-and-output-to/m-p/818826#M323231</guid>
      <dc:creator>tonybesas</dc:creator>
      <dc:date>2022-06-17T13:55:45Z</dc:date>
    </item>
    <item>
      <title>Re: Select the top highest values in each observation and output to a new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-the-top-highest-values-in-each-observation-and-output-to/m-p/818829#M323232</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;

array have(*) A1--D2;
array top(5) top1-top5;

do i=1 to dim(top);
top(i) = largest(i, of have(*));
end;

do i=1 to dim(have);
if i&amp;lt;=dim(top) then have(i) = top(i);
else have(i) = 0;
end;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Jun 2022 14:10:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-the-top-highest-values-in-each-observation-and-output-to/m-p/818829#M323232</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-06-17T14:10:01Z</dc:date>
    </item>
    <item>
      <title>Re: Select the top highest values in each observation and output to a new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-the-top-highest-values-in-each-observation-and-output-to/m-p/818835#M323235</link>
      <description>&lt;P&gt;Hi Reeza, thank you for your reply!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is giving the following output:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Obs	id	A1	A2	A3	B1	B2	B3	C1	C2	D1	D2
1	1	10	9	8	7	6	0	0	0	0	0
2	2	10	9	8	7	6	0	0	0	0	0
3	3	6	5	4	3	0	0	0	0	0	0
&lt;/PRE&gt;
&lt;P&gt;Which is different from the desired output:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;1 10  9  8  7  6  0  0  0  0  0 
2  0  0 10  9  8  7  6  0  0  0
3  0  0  0  0  6  5  4  3  0  0&lt;/PRE&gt;
&lt;P&gt;Because the values of vars A1 and A2 in observation 2 should be 0, not 10 and 9, the values of vars A1 and A2 in observation 3 should be both 0, and so on. &amp;nbsp;The top 5 values should be placed back in the same variables as they were in the 'have' dataset.&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jun 2022 14:36:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-the-top-highest-values-in-each-observation-and-output-to/m-p/818835#M323235</guid>
      <dc:creator>tonybesas</dc:creator>
      <dc:date>2022-06-17T14:36:19Z</dc:date>
    </item>
    <item>
      <title>Re: Select the top highest values in each observation and output to a new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-the-top-highest-values-in-each-observation-and-output-to/m-p/818838#M323236</link>
      <description>Does your data have ties?&lt;BR /&gt;</description>
      <pubDate>Fri, 17 Jun 2022 14:50:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-the-top-highest-values-in-each-observation-and-output-to/m-p/818838#M323236</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-06-17T14:50:50Z</dc:date>
    </item>
    <item>
      <title>Re: Select the top highest values in each observation and output to a new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-the-top-highest-values-in-each-observation-and-output-to/m-p/818841#M323238</link>
      <description>Yes, it does</description>
      <pubDate>Fri, 17 Jun 2022 14:55:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-the-top-highest-values-in-each-observation-and-output-to/m-p/818841#M323238</guid>
      <dc:creator>tonybesas</dc:creator>
      <dc:date>2022-06-17T14:55:26Z</dc:date>
    </item>
    <item>
      <title>Re: Select the top highest values in each observation and output to a new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-the-top-highest-values-in-each-observation-and-output-to/m-p/818843#M323239</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
   input id A1 A2 A3 B1 B2 B3 C1 C2 D1 D2 ;
   datalines;
1 10  9  8  7  6  5  4  3  2  1 
2  0  0 10  9  8  7  6  5  4  3
3  0  0  0  0  6  5  4  3  0  0
;
run;

%let top_n = 5;

proc transpose data=have out=long;
by id;
var A1--D2;
run;


proc sort data=long;
by id descending col1;
run;

data long_marked;
set long;
by id;
if first.id then counter=1;
else counter+1;
if counter&amp;gt; &amp;amp;top_n then col1=0;
run;

proc sort data=long_marked;
by id _name_;
run;

proc transpose data=long_marked out=want;
by id;
id _name_;
var col1;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In general, storing your data in a long format may make this easier then.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jun 2022 15:07:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-the-top-highest-values-in-each-observation-and-output-to/m-p/818843#M323239</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-06-17T15:07:48Z</dc:date>
    </item>
    <item>
      <title>Re: Select the top highest values in each observation and output to a new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-the-top-highest-values-in-each-observation-and-output-to/m-p/818847#M323241</link>
      <description>&lt;P&gt;Hi Reeza, this is perfect! I was testing it on my full dataset and it worked really fast and really well!! &lt;span class="lia-unicode-emoji" title=":clapping_hands:"&gt;👏&lt;/span&gt; Thank you very very much!&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jun 2022 15:37:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-the-top-highest-values-in-each-observation-and-output-to/m-p/818847#M323241</guid>
      <dc:creator>tonybesas</dc:creator>
      <dc:date>2022-06-17T15:37:13Z</dc:date>
    </item>
    <item>
      <title>Re: Select the top highest values in each observation and output to a new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-the-top-highest-values-in-each-observation-and-output-to/m-p/818915#M323266</link>
      <description>&lt;P&gt;Here is a single pass solution&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
   input id A1 A2 A3 B1 B2 B3 C1 C2 D1 D2 ;
   datalines;
1 10  9  8  7  6  5  4  3  2  1 
2  0  0 10  9  8  7  6  5  4  3
3  0  0  0  0  6  5  4  3  0  0
;
run;

data want(drop = rc i val pos);

   if _N_ = 1 then do;
      dcl hash h(ordered : 'D', multidata : 'Y');
      h.definekey('val');
      h.definedata('val', 'pos');
      h.definedone();
      dcl hiter hi('h');
   end;

   set have;

   array a{*} a1 -- d2;

   do i = 1 to dim(a);
      val = a[i];
      pos = i;
      h.add();
      a[i] = 0;
   end;

   do i = 1 by 1 while (hi.next() = 0);
      a[pos] = val;
      if i = 3 then leave;
	end;

   rc = hi.last();
   rc = hi.next();
   rc = h.clear();

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Jun 2022 18:54:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-the-top-highest-values-in-each-observation-and-output-to/m-p/818915#M323266</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-06-17T18:54:36Z</dc:date>
    </item>
  </channel>
</rss>

