<?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 Nth largest within each group for moving window in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-Nth-largest-within-each-group-for-moving-window/m-p/846238#M334554</link>
    <description>&lt;P&gt;Thank you, Tom, for helping.&lt;/P&gt;
&lt;P&gt;I really appreciate it.&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
    <pubDate>Fri, 25 Nov 2022 02:02:34 GMT</pubDate>
    <dc:creator>hhchenfx</dc:creator>
    <dc:date>2022-11-25T02:02:34Z</dc:date>
    <item>
      <title>Find Nth largest within each group for moving window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-Nth-largest-within-each-group-for-moving-window/m-p/845784#M334366</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;My data has date, ID and value.&lt;/P&gt;
&lt;P&gt;For each date and ID, say date=7 and ID = 1, I look back 5 days (from date = 6 to date = 2) and find the 1st highest, 2nd highest and 3rd highest values.&lt;/P&gt;
&lt;P&gt;So the wanted data should have 3 more new columns for these highest.&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;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards;
input date ID value;
cards;
1 1 1
2 1 7
3 1 3
4 1 101
5 1 102
6 1 103
7 1 104
8 1 105
8 2 1
9 2 124
10 2 11
11 2 9999
12 2 9
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;a&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Nov 2022 22:01:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-Nth-largest-within-each-group-for-moving-window/m-p/845784#M334366</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-11-22T22:01:27Z</dc:date>
    </item>
    <item>
      <title>Re: Find Nth largest within each group for moving window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-Nth-largest-within-each-group-for-moving-window/m-p/845789#M334370</link>
      <description>&lt;P&gt;If you have SAS/ETS installed use the procedures to do these things.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If not use a "circular" array.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input date ID value;
cards;
1 1 1
2 1 7
3 1 3
4 1 101
5 1 102
6 1 103
7 1 104
8 1 105
8 2 1
9 2 124
10 2 11
11 2 9999
12 2 9
;

data want;
  set have;
  by id date ;
  array history [0:4] _temporary_;
  first=largest(1,of history[*]);
  second=largest(2,of history[*]);
  third=largest(3,of history[*]);
  if first.id then call missing(of index first second third history[*]);
  index+1;
  history[mod(index,5)]=value;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Obs    date    ID    value    first    second    third    index

  1      1      1        1        .        .        .       1
  2      2      1        7        1        .        .       2
  3      3      1        3        7        1        .       3
  4      4      1      101        7        3        1       4
  5      5      1      102      101        7        3       5
  6      6      1      103      102      101        7       6
  7      7      1      104      103      102      101       7
  8      8      1      105      104      103      102       8
  9      8      2        1        .        .        .       1
 10      9      2      124        1        .        .       2
 11     10      2       11      124        1        .       3
 12     11      2     9999      124       11        1       4
 13     12      2        9     9999      124       11       5
&lt;/PRE&gt;
&lt;P&gt;If you want to include the current value in the search then re-order the statements.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id date ;
  array history [0:4] _temporary_;
  if first.id then call missing(of index first second third history[*]);
  index+1;
  history[mod(index,5)]=value;
  first=largest(1,of history[*]);
  second=largest(2,of history[*]);
  third=largest(3,of history[*]);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 22 Nov 2022 22:36:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-Nth-largest-within-each-group-for-moving-window/m-p/845789#M334370</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-11-22T22:36:28Z</dc:date>
    </item>
    <item>
      <title>Re: Find Nth largest within each group for moving window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-Nth-largest-within-each-group-for-moving-window/m-p/846238#M334554</link>
      <description>&lt;P&gt;Thank you, Tom, for helping.&lt;/P&gt;
&lt;P&gt;I really appreciate it.&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
      <pubDate>Fri, 25 Nov 2022 02:02:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-Nth-largest-within-each-group-for-moving-window/m-p/846238#M334554</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-11-25T02:02:34Z</dc:date>
    </item>
    <item>
      <title>Re: Find Nth largest within each group for moving window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-Nth-largest-within-each-group-for-moving-window/m-p/846291#M334576</link>
      <description>&lt;P&gt;You can let Proc Summary find the top 3 values for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards;
input date ID value;
cards;
1  1 1    
2  1 7    
3  1 3    
4  1 101  
5  1 102  
6  1 103  
7  1 104  
8  1 105  
8  2 1    
9  2 124  
10 2 11   
11 2 9999 
12 2 9    
;
run;

proc summary data = have nway noprint;
   class ID;
   var value;
   output out = want(drop = _:) idgroup (max(value) out[3] (value) =) / autolabel autoname;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Nov 2022 10:22:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-Nth-largest-within-each-group-for-moving-window/m-p/846291#M334576</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-11-25T10:22:29Z</dc:date>
    </item>
  </channel>
</rss>

