<?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: Check if today value belong to top 1 or top 2 when looking back 4 days in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Check-if-today-value-belong-to-top-1-or-top-2-when-looking-back/m-p/857788#M338944</link>
    <description>&lt;P&gt;I try another method and look quite short.&lt;/P&gt;
&lt;P&gt;I will run a big sample and see which one is faster.&lt;/P&gt;
&lt;P&gt;Many thanks,&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 want;
set have nobs=nobs;
by id;
i+1;
out=0;
top=1;
do j=i to i+5 until (out=1);
set have (rename =( id=X_id date=X_date value=X_value)) point=j;
	if id^=X_id then out=1;
	if value&amp;lt;X_value then top=top+1;
	if top=3 then do; exit=X_date;out=1;end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 08 Feb 2023 16:40:42 GMT</pubDate>
    <dc:creator>hhchenfx</dc:creator>
    <dc:date>2023-02-08T16:40:42Z</dc:date>
    <item>
      <title>Check if today value belong to top 1 or top 2 when looking back 4 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-today-value-belong-to-top-1-or-top-2-when-looking-back/m-p/857682#M338891</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;My data has id, date and value.&lt;/P&gt;
&lt;P&gt;For each date (current date), I lookback 4 days and check if current date's value belong to top 1 or top 2. The top position is also kept.&lt;/P&gt;
&lt;P&gt;My code below works, but I would like to have a more efficient code.&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;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input  ID date value  ;
cards;
1 1 5
1 2 8
1 3 20
1 4 0
1 5 1
1 6 10
1 7 5
2 1 6
2 2 10
2 3 10
2 4 9
2 5 1 
2 6 5
2 7 33
run;

*get prior 4 days and current date;
proc sql;
create table want as select a.id,a.date, b.date as date_2, b.value
from have as a left join have as b 
on a.id=b.id and a.date-4&amp;lt;=b.date&amp;lt;=a.date
order by a.id,a.date, b.date;
quit;

proc sort data=want; by id date descending value;run;

*top 2 values;
data want2; set want;
	by id date;
	if first.date then top=0;
	top+1;
	if top&amp;lt;=2 then output;
	run;

*check if current date and top value date is the same;
data final; set want2;
if date=date_2; run;

proc sort data=final; by id date top;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Feb 2023 00:35:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-today-value-belong-to-top-1-or-top-2-when-looking-back/m-p/857682#M338891</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2023-02-08T00:35:13Z</dc:date>
    </item>
    <item>
      <title>Re: Check if today value belong to top 1 or top 2 when looking back 4 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-today-value-belong-to-top-1-or-top-2-when-looking-back/m-p/857706#M338904</link>
      <description>&lt;P&gt;This does it in one step, but handles ties for first place differently:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id;
array last4 {4} _temporary_;
array last4_2 {4} _temporary_;
if first.id
then do;
  count = 1;
  call missing(of last4{*});
end;
else count + 1;
last4{mod(count,4)+1} = value;
do i = 1 to 4;
  last4_2{i} = last4{i};
end;
call sort(of last4_2{*});
if value = last4_2{4}
then do;
  top = 1;
  output;
end;
else if value = last4_2{3}
then do;
  top = 2;
  output;
end;
drop count;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Feb 2023 10:00:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-today-value-belong-to-top-1-or-top-2-when-looking-back/m-p/857706#M338904</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-02-08T10:00:21Z</dc:date>
    </item>
    <item>
      <title>Re: Check if today value belong to top 1 or top 2 when looking back 4 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-today-value-belong-to-top-1-or-top-2-when-looking-back/m-p/857725#M338916</link>
      <description>&lt;P&gt;Same idea with Kurt.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*
That would be easy if there is no gap between two date.
*/
data have;
  input  ID date value  ;
cards;
1 1 5
1 2 8
1 3 20
1 4 0
1 5 1
1 6 10
1 7 5
2 1 6
2 2 10
2 3 10
2 4 9
2 5 1 
2 6 5
2 7 33
;
data want;
array x{5} _temporary_;
array y{5} _temporary_;
call missing(of x{*} y{*});
 do i=1 by 1 until(last.id);
  set have;
  by id;
  x{mod(i,dim(x))+1}=value;
  do j=1 to dim(x);
    y{j}=x{j};
  end;
  call sortn(of y{*});

  top=dim(x)+1-whichn(value,of y{*});
  output;
 end;
 drop i j;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Feb 2023 11:39:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-today-value-belong-to-top-1-or-top-2-when-looking-back/m-p/857725#M338916</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-02-08T11:39:32Z</dc:date>
    </item>
    <item>
      <title>Re: Check if today value belong to top 1 or top 2 when looking back 4 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-today-value-belong-to-top-1-or-top-2-when-looking-back/m-p/857788#M338944</link>
      <description>&lt;P&gt;I try another method and look quite short.&lt;/P&gt;
&lt;P&gt;I will run a big sample and see which one is faster.&lt;/P&gt;
&lt;P&gt;Many thanks,&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 want;
set have nobs=nobs;
by id;
i+1;
out=0;
top=1;
do j=i to i+5 until (out=1);
set have (rename =( id=X_id date=X_date value=X_value)) point=j;
	if id^=X_id then out=1;
	if value&amp;lt;X_value then top=top+1;
	if top=3 then do; exit=X_date;out=1;end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Feb 2023 16:40:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-today-value-belong-to-top-1-or-top-2-when-looking-back/m-p/857788#M338944</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2023-02-08T16:40:42Z</dc:date>
    </item>
    <item>
      <title>Re: Check if today value belong to top 1 or top 2 when looking back 4 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-today-value-belong-to-top-1-or-top-2-when-looking-back/m-p/857792#M338947</link>
      <description>&lt;P&gt;Not sure about performance of this solution, the largest function may be resource intense.&lt;/P&gt;
&lt;P&gt;EDIT: tweaked indexes to match results as it's current 5 observations, not 4 observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
set have;
by id;
array _prev(0:4) _Temporary_;


if first.id then do;
    call missing (of _prev(*));
end;

*add to array;
_prev(mod(_n_, 5)) = value;

if largest(1, of _prev(*)) =value then flag=1;
if largest(2, of _prev(*)) = value then flag=2;


run;

data final3;
set temp;
if flag in (1,2) ;
run;&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Feb 2023 17:07:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-today-value-belong-to-top-1-or-top-2-when-looking-back/m-p/857792#M338947</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-02-08T17:07:44Z</dc:date>
    </item>
    <item>
      <title>Re: Check if today value belong to top 1 or top 2 when looking back 4 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-today-value-belong-to-top-1-or-top-2-when-looking-back/m-p/857827#M338955</link>
      <description>&lt;P&gt;&amp;lt;pedantic mode: on&amp;gt;&lt;/P&gt;
&lt;P&gt;Top? that means above. So processing data from "top to bottom" in the shown code means the first two values are the "top".&lt;/P&gt;
&lt;P&gt;&amp;lt;pedantic mode: off&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I really hate a value judgement like "top" being used as programming requirement. "Top" could mean the lowest score (golf for example lowest value wins or is better.&amp;nbsp; If you mean "Largest value" say so. Also if you mean to do something within a group of values, like ID, please state so.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Feb 2023 19:03:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-today-value-belong-to-top-1-or-top-2-when-looking-back/m-p/857827#M338955</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-02-08T19:03:24Z</dc:date>
    </item>
    <item>
      <title>Re: Check if today value belong to top 1 or top 2 when looking back 4 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-today-value-belong-to-top-1-or-top-2-when-looking-back/m-p/857993#M338998</link>
      <description>I think Reeza's code is most efficient. And my code didn't consider the TIE value , but Reeza care about it .</description>
      <pubDate>Thu, 09 Feb 2023 11:43:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-today-value-belong-to-top-1-or-top-2-when-looking-back/m-p/857993#M338998</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-02-09T11:43:18Z</dc:date>
    </item>
    <item>
      <title>Re: Check if today value belong to top 1 or top 2 when looking back 4 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-today-value-belong-to-top-1-or-top-2-when-looking-back/m-p/858188#M339077</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;Thank you so much for your support!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I run on 2 samples (1million row and 5 million rows) and lookback window is &lt;STRONG&gt;1000&lt;/STRONG&gt; and below are the result.&lt;BR /&gt;Somehow, the code by Kurt_Bremser return error with call sort so I can't check.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I believe that data structure matters in terms of time needed to run and the 1000 lookback is quite a stress test.&lt;BR /&gt;(With lookback =5, there is not much difference among methods.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 milion:&lt;BR /&gt;Reeze: 3.04 sec&lt;BR /&gt;Ksharp: 1:19 min&lt;BR /&gt;me: 5.98 sec&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;5 milion:&lt;BR /&gt;Reeze: 9.7 sec&lt;BR /&gt;Ksharp: more than 2 min&lt;BR /&gt;me: 12.6&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&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;</description>
      <pubDate>Fri, 10 Feb 2023 04:25:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-today-value-belong-to-top-1-or-top-2-when-looking-back/m-p/858188#M339077</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2023-02-10T04:25:57Z</dc:date>
    </item>
    <item>
      <title>Re: Check if today value belong to top 1 or top 2 when looking back 4 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-today-value-belong-to-top-1-or-top-2-when-looking-back/m-p/879469#M347430</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have nobs=nobs;
by id;
drop out i j X_: out;
i+1;
out=0;
top_value&amp;amp;look_back=1;

do j=i+1 to i+ &amp;amp;look_back until (out=1);

set have (keep = id value change_id rename =( id=X_id value=X_value change_id=X_change_id)) point=j;
	if change_id=1 then do; out=1;;leave;end;
	if X_change_id=1 then do; out=1;;leave;end;
	else
	if value&amp;lt;X_value then do; top_value&amp;amp;look_back=top_value&amp;amp;look_back+1; max_value=X_value;end;
	
	if top_value&amp;amp;look_back =&amp;amp;cutoff_top_value then do;top_value&amp;amp;look_back=0; out=1; ;end;


end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Final code&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2023 00:36:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-today-value-belong-to-top-1-or-top-2-when-looking-back/m-p/879469#M347430</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2023-06-07T00:36:37Z</dc:date>
    </item>
  </channel>
</rss>

