<?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: Moving 2nd, 3rd largest number per group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Moving-2nd-3rd-largest-number-per-group/m-p/830415#M328117</link>
    <description>&lt;P&gt;I'm assuming you have a typo in your data (line 7, ID=2).&lt;/P&gt;
&lt;P&gt;If true, something like this should work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input day id v1 v2 v3 v4;
datalines;
6 1 2 15 3 1
5 1 4 50 6 9
4 1 1 0 61 0
3 1 9 0 60 5
2 1 12 3 1 2
1 1 21 2 0 2
6 3 2 5 2 3
5 3 12 15 6 1
4 3 0 1 1 8
3 3 0 4 4 8
2 3 12 6 56 8
1 3 25 5 65 8
;run;


proc sort data=have;
by id day;
run;

data want;
array p{0:3} _temporary_;
set have; 
by ID; 

if first.ID then call missing(of p{*});

p{mod(_n_,4)} = v1;

if day &amp;gt; 3 then do;
Rank1 = largest(1,  of p{*});
Rank2 = largest(2,  of p{*});
Rank3 = largest(3,  of p{*});
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 25 Aug 2022 19:38:08 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2022-08-25T19:38:08Z</dc:date>
    <item>
      <title>Moving 2nd, 3rd largest number per group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-2nd-3rd-largest-number-per-group/m-p/830411#M328115</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;I have panel data with date, id and values.&lt;/P&gt;
&lt;P&gt;For a give day and id, I want to look back 3 days (so including current day, there are 4 days in total) and find the 1st, 2nd, 3rd largest. It is like moving largest(n).&lt;/P&gt;
&lt;P&gt;So for day 6 and id = 1, the desired result should be: 9, 4 , 2&lt;/P&gt;
&lt;P&gt;So for day 5 and id = 1, the desired result should be: 12, 9 , 4&lt;/P&gt;
&lt;P&gt;Can you please help?&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;HHC&lt;BR /&gt;(My work requires multiple lookback windows (say, 20, 40 days) and apply to multiple columns. For that, I guess a macro can be fine and I think I can handle that)&lt;/P&gt;
&lt;PRE&gt;data have;&lt;BR /&gt;input day id v1 v2 v3 v4;&lt;BR /&gt;datalines;&lt;BR /&gt;6 1 2 15 3 1&lt;BR /&gt;5 1 4 50 6 9&lt;BR /&gt;4 1 1 0 61 0&lt;BR /&gt;3 1 9 0 60 5&lt;BR /&gt;2 1 12 3 1 2&lt;BR /&gt;1 1 21 2 0 2&lt;BR /&gt;6 2 2 5 2 3&lt;BR /&gt;5 3 12 15 6 1&lt;BR /&gt;4 3 0 1 1 8&lt;BR /&gt;3 3 0 4 4 8&lt;BR /&gt;2 3 12 6 56 8&lt;BR /&gt;1 3 25 5 65 8&lt;BR /&gt;;run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Aug 2022 19:25:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-2nd-3rd-largest-number-per-group/m-p/830411#M328115</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-08-25T19:25:42Z</dc:date>
    </item>
    <item>
      <title>Re: Moving 2nd, 3rd largest number per group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-2nd-3rd-largest-number-per-group/m-p/830415#M328117</link>
      <description>&lt;P&gt;I'm assuming you have a typo in your data (line 7, ID=2).&lt;/P&gt;
&lt;P&gt;If true, something like this should work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input day id v1 v2 v3 v4;
datalines;
6 1 2 15 3 1
5 1 4 50 6 9
4 1 1 0 61 0
3 1 9 0 60 5
2 1 12 3 1 2
1 1 21 2 0 2
6 3 2 5 2 3
5 3 12 15 6 1
4 3 0 1 1 8
3 3 0 4 4 8
2 3 12 6 56 8
1 3 25 5 65 8
;run;


proc sort data=have;
by id day;
run;

data want;
array p{0:3} _temporary_;
set have; 
by ID; 

if first.ID then call missing(of p{*});

p{mod(_n_,4)} = v1;

if day &amp;gt; 3 then do;
Rank1 = largest(1,  of p{*});
Rank2 = largest(2,  of p{*});
Rank3 = largest(3,  of p{*});
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Aug 2022 19:38:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-2nd-3rd-largest-number-per-group/m-p/830415#M328117</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-08-25T19:38:08Z</dc:date>
    </item>
    <item>
      <title>Re: Moving 2nd, 3rd largest number per group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-2nd-3rd-largest-number-per-group/m-p/830462#M328151</link>
      <description>Thank you, Reeza.&lt;BR /&gt;It works perfectly.&lt;BR /&gt;HHC</description>
      <pubDate>Fri, 26 Aug 2022 01:58:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-2nd-3rd-largest-number-per-group/m-p/830462#M328151</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-08-26T01:58:10Z</dc:date>
    </item>
  </channel>
</rss>

