<?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: Find the N biggest numbers in a day up to current time in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-the-N-biggest-numbers-in-a-day-up-to-current-time/m-p/846935#M334827</link>
    <description>&lt;P&gt;Thank you all for helping.&lt;/P&gt;
&lt;P&gt;These are amazing code.&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
    <pubDate>Tue, 29 Nov 2022 22:05:34 GMT</pubDate>
    <dc:creator>hhchenfx</dc:creator>
    <dc:date>2022-11-29T22:05:34Z</dc:date>
    <item>
      <title>Find the N biggest numbers in a day up to current time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-N-biggest-numbers-in-a-day-up-to-current-time/m-p/846695#M334692</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;My data has date, time, ID and value.&lt;/P&gt;
&lt;P&gt;For each row, say date=1, time =12 and ID = 1, I want to find the 1st highest, 2nd highest and 3rd highest values for that id, of that date, and up to that time (&amp;lt;12).&lt;/P&gt;
&lt;P&gt;The illustration of that row at the end.&lt;/P&gt;
&lt;P&gt;Can you please help me?&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input date time ID value;
cards;
0 1 1 88888
0 1 9 585
1 1 1 999
1 2 1 7
1 3 1 3
1 4 1 101
1 7 1 102
1 9 1 12
1 12 1 9999
1 13 1 105
1 14 1 1
2 12 1 5
2 13 1 8 
1 2 124
1 2 11
1 2 9999
1 2 9
;


data temp; set have;
if id=1 and date=1 and       time&amp;lt;12 ;run;

proc sort data=temp; by id descending value;run;

*pick to 3;
data temp; set temp;
if _N_&amp;lt;=3;
run;

*spread horizontally;
proc transpose data=temp out=want (drop = _NAME_);
by date id;
var value;run;

data want; set want;
rename col1=top1 col2=top2 col3=top3;
time=12;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Nov 2022 22:24:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-N-biggest-numbers-in-a-day-up-to-current-time/m-p/846695#M334692</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-11-28T22:24:50Z</dc:date>
    </item>
    <item>
      <title>Re: Find the N biggest numbers in a day up to current time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-N-biggest-numbers-in-a-day-up-to-current-time/m-p/846721#M334709</link>
      <description>&lt;P&gt;My first thought about this is to use an ordered hash object, but actually you can more neatly combine a RETAIN statement with a CALL SORTN:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input date time ID value  ;
cards;
0 1 1 88888
0 1 9 585  
1 1 1 999  
1 2 1 7    
1 3 1 3    
1 4 1 101  
1 7 1 102  
1 9 1 12   
1 12 1 9999
1 13 1 105 
1 14 1 1   
2 12 1 5   
2 13 1 8   
1 2 124    
1 2 11     
1 2 9999   
1 2 9      
run;

proc sort ; 
  by id date time ;
run;

data want (drop=_:);
  set have;
  by id date;
  retain topvalue1-topvalue3  _dummy ;

  array sortvars _dummy topvalue3 topvalue2 topvalue1 ;

  if first.date then call missing(of sortvars{*});
  call sortn(of sortvars{*});
  output;
  _dummy=value;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Reset the array sortvars (containing a history of VALUEs) to missing at the start of each date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The call sortn routine will put the highest value in that history at the rightmost element of the sortvars array&amp;nbsp; (i.e. TOPVALUE1).&amp;nbsp; That's why the last three elements of sortvars array are in "reverse" order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;After sorting, output the observations.&amp;nbsp; &lt;EM&gt;&lt;STRONG&gt;Only after that&lt;/STRONG&gt;&lt;/EM&gt;, replace the 4th largest historic value (i.e. variable _DUMMY)&amp;nbsp; &amp;nbsp;If the new value will be part of the top 3, that will be determined by the call sortn during the next observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I list the variables in the RETAIN statement in desired order, and place the RETAIN prior to the ARRAY statement (in reverse order).&amp;nbsp; The variable order of the first statement will prevail in the output dataset, even though the call sortn uses them in another order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Nov 2022 01:44:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-N-biggest-numbers-in-a-day-up-to-current-time/m-p/846721#M334709</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-11-29T01:44:42Z</dc:date>
    </item>
    <item>
      <title>Re: Find the N biggest numbers in a day up to current time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-N-biggest-numbers-in-a-day-up-to-current-time/m-p/846733#M334719</link>
      <description>&lt;P&gt;Didn't you ask this question last week?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input date time ID value;
cards;
0 1 1 88888
0 1 9 585
1 1 1 999
1 2 1 7
1 3 1 3
1 4 1 101
1 7 1 102
1 9 1 12
1 12 1 9999
1 13 1 105
1 14 1 1
2 12 1 5
2 13 1 8 
1 2 124 .
1 2 11 .
1 2 9999 .
1 2 9 .
;

proc sort;
  by date time;
run;

data want ;
  set have;
  by date time;
  array history [0:99] _temporary_;
  array big largest1-largest5 ;
  if first.date then call missing(of row history[*] big[*]);
  row+1;
  history[mod(row,100)]=value;
  do index=1 to dim(big);
    big[index]=largest(index,of history[*]);
  end;
  drop index;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs    date    time      ID    value    largest1    largest2    largest3    largest4    largest5    row

  1      0       1        1    88888      88888          .           .           .           .        1
  2      0       1        9      585      88888        585           .           .           .        2
  3      1       1        1      999        999          .           .           .           .        1
  4      1       2        1        7        999          7           .           .           .        2
  5      1       2      124        .        999          7           .           .           .        3
  6      1       2       11        .        999          7           .           .           .        4
  7      1       2     9999        .        999          7           .           .           .        5
  8      1       2        9        .        999          7           .           .           .        6
  9      1       3        1        3        999          7           3           .           .        7
 10      1       4        1      101        999        101           7           3           .        8
 11      1       7        1      102        999        102         101           7           3        9
 12      1       9        1       12        999        102         101          12           7       10
 13      1      12        1     9999       9999        999         102         101          12       11
 14      1      13        1      105       9999        999         105         102         101       12
 15      1      14        1        1       9999        999         105         102         101       13
 16      2      12        1        5          5          .           .           .           .        1
 17      2      13        1        8          8          5           .           .           .        2
&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Nov 2022 04:29:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-N-biggest-numbers-in-a-day-up-to-current-time/m-p/846733#M334719</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-11-29T04:29:42Z</dc:date>
    </item>
    <item>
      <title>Re: Find the N biggest numbers in a day up to current time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-N-biggest-numbers-in-a-day-up-to-current-time/m-p/846767#M334737</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/49486"&gt;@hhchenfx&lt;/a&gt;&amp;nbsp;, it seems that you've asked this quested 2 times before, but with slightly different input data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this version of the question, it seems that the difference is that the date variable is not strictly increasing for each observation? Am I correct to assume that your requirement is the same as previous, but that you want to maintain the original order?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please be very specific. Makes it much easier to provide an accurate answer &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Nov 2022 10:31:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-N-biggest-numbers-in-a-day-up-to-current-time/m-p/846767#M334737</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-11-29T10:31:51Z</dc:date>
    </item>
    <item>
      <title>Re: Find the N biggest numbers in a day up to current time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-N-biggest-numbers-in-a-day-up-to-current-time/m-p/846935#M334827</link>
      <description>&lt;P&gt;Thank you all for helping.&lt;/P&gt;
&lt;P&gt;These are amazing code.&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
      <pubDate>Tue, 29 Nov 2022 22:05:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-N-biggest-numbers-in-a-day-up-to-current-time/m-p/846935#M334827</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-11-29T22:05:34Z</dc:date>
    </item>
    <item>
      <title>Re: Find the N biggest numbers in a day up to current time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-N-biggest-numbers-in-a-day-up-to-current-time/m-p/846936#M334828</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;, thanks for reminding me.&lt;/P&gt;
&lt;P&gt;I will try to make it as clear as I can.&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Nov 2022 22:07:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-N-biggest-numbers-in-a-day-up-to-current-time/m-p/846936#M334828</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-11-29T22:07:32Z</dc:date>
    </item>
  </channel>
</rss>

