<?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: Change value of previous record based on value of subsequent records in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Change-value-of-previous-record-based-on-value-of-subsequent/m-p/978104#M378567</link>
    <description>&lt;P&gt;You might also try a double "look-ahead":&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
merge
  number_game
  number_game (
    firstobs=2
    keep=player result
    rename=(player=player2 result=result2)
  )
  number_game (
    firstobs=3
    keep=player result
    rename=(player=player3 result=result3)
  )
;
retain start have_streak;
format start time time5.;
if player ne lag(player)
then do;
  start = starttime;
  have_streak = 0;
end;
if
  not have_streak
  and result = "W"
  and player2 = player
  and result2 = "W"
  and player3 = player
  and result3 = "W"
then do;
  have_streak = 1;
  time = playtime - start;
  output;
end;
keep player start time;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As you can see, the parts depending on the number of wins for a streak are repetitive, so they could be auto-created with a macro if different streak lengths are needed.&lt;/P&gt;</description>
    <pubDate>Wed, 29 Oct 2025 17:03:33 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2025-10-29T17:03:33Z</dc:date>
    <item>
      <title>Change value of previous record based on value of subsequent records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-value-of-previous-record-based-on-value-of-subsequent/m-p/978070#M378554</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I know this topic appeared already in this &lt;A href="https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689419#M209590" target="_self"&gt;forum&lt;/A&gt; however either I do not understand how to apply the %hop macro to my problem or it does not fit because I would need to 'hop' over multiple entries.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the problem:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data number_game;
    length player $1 result $1;
    input player starttime :time5. playtime :time5. result $;
    format starttime playtime time5.;
    datalines;
1 12:00 12:00 L
1 12:00 12:15 L
1 12:00 12:30 W
1 12:00 12:45 W
1 12:00 13:00 W
1 12:00 13:15 W
1 12:00 13:30 W
1 12:00 13:45 W
2 12:00 12:00 W
2 12:00 12:15 L
2 12:00 12:30 L
2 12:00 12:45 W
2 12:00 13:00 L
2 12:00 13:15 W
2 12:00 13:30 W
2 12:00 13:45 W
3 12:00 12:00 L
3 12:00 12:15 W
3 12:00 12:30 L
3 12:00 12:45 L
3 12:00 13:00 W
3 12:00 13:15 W
3 12:00 13:30 W
3 12:00 13:45 W
;
run;

data number_game;
    length win_streak 3.;
    set number_game;
    by player starttime;
    retain win_streak;
    if first.player then win_streak = 0;
    if result = 'W' then win_streak + 1;
    else win_streak = 0;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In a series of games the player wins with the shortest time to a winstreak of 3 (consecutive wins, series of 3).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The time of the start of the winstreak should be calculated.&lt;/P&gt;
&lt;P&gt;E.g. for player 1 this would be 12:30-12:00 --&amp;gt; 30minutes&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance and best regards.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Oct 2025 09:46:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-value-of-previous-record-based-on-value-of-subsequent/m-p/978070#M378554</guid>
      <dc:creator>LuGa</dc:creator>
      <dc:date>2025-10-29T09:46:50Z</dc:date>
    </item>
    <item>
      <title>Re: Change value of previous record based on value of subsequent records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-value-of-previous-record-based-on-value-of-subsequent/m-p/978082#M378556</link>
      <description>&lt;P&gt;I'm sure someone will do this with a hash table, but how about this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint; 
select count(distinct player) into :np trimmed from number_game; 
quit;

data winner;
set number_game end=last;
by player starttime playtime;
length win_streak 3 tot_time 4;
array Tp {&amp;amp;np} $6 _temporary_;
array Tt {&amp;amp;np} 4 _temporary_;
retain tpos 0;
if first.player or result='L' then win_streak=0;
win_streak+(result='W');
if win_streak=3 then do;
	tot_time=playtime-starttime;
	tpos+1;
	Tp[tpos]=player;
	Tt[tpos]=tot_time;
end;
if last then do;
	best_time=min(of Tt[*]);
	do i=1 to tpos;
		if Tt[i]=best_time then do;
			player=Tp[i];
			output;
		end;
	end;
end;
keep player best_time;
run;

proc print data=winner; run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This allows ties.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Oct 2025 11:58:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-value-of-previous-record-based-on-value-of-subsequent/m-p/978082#M378556</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-10-29T11:58:55Z</dc:date>
    </item>
    <item>
      <title>Re: Change value of previous record based on value of subsequent records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-value-of-previous-record-based-on-value-of-subsequent/m-p/978083#M378557</link>
      <description>&lt;P&gt;Slightly fancier - this will spit out all the players with a win streak of 3, ranked from best to worst.&amp;nbsp; Btw, when defining the array Tp, be sure the length allocated for each bin (currently $6) is at least as long as the length of the player variable ($1 in your sample data).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint; 
select count(distinct player) into :np trimmed from number_game; 
quit;

data ranked;
set number_game end=last;
by player starttime playtime;
length win_streak 3 tot_time 4;
array Tp {&amp;amp;np} $6 _temporary_;
array Tt {&amp;amp;np} 4 _temporary_;
retain tpos 0;
if first.player or result='L' then win_streak=0;
win_streak+(result='W');
if win_streak=3 and player not in Tp then do;
	tot_time=playtime-starttime;
	tpos+1;
	Tp[tpos]=player;
	Tt[tpos]=tot_time;
end;
if last then do;
	best_time=min(of Tt[*]);
	rank=0;
	do while (best_time&amp;gt;.);
		rank+1;
		do i=1 to tpos;
			if Tt[i]=best_time then do;
				player=Tp[i];
				Tt[i]=.;
				output;
			end;
		end;
		best_time=min(of Tt[*]);
	end;
end;
keep player best_time rank;
run;

proc print data=ranked; run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Oct 2025 13:09:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-value-of-previous-record-based-on-value-of-subsequent/m-p/978083#M378557</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-10-29T13:09:19Z</dc:date>
    </item>
    <item>
      <title>Re: Change value of previous record based on value of subsequent records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-value-of-previous-record-based-on-value-of-subsequent/m-p/978085#M378558</link>
      <description>&lt;P&gt;Very cool! I am always quite hesitant when it comes to arrays in SAS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In terms of lines of codes my solution might be the simpler:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data number_game;
    set number_game;
    lag_playtime = lag2(playtime);
    if win_streak = 3 then time_to_winstreak = intck('Minutes', starttime, lag_playtime);
    drop lag_playtime;
run;

proc sql;
    select player from number_game where time_to_winstreak in (select min(time_to_winstreak) from number_game where time_to_winstreak is not null);
quit;&lt;/PRE&gt;
&lt;P&gt;However your example seems to be more versatile (lag function goes only up to 3).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 29 Oct 2025 12:58:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-value-of-previous-record-based-on-value-of-subsequent/m-p/978085#M378558</guid>
      <dc:creator>LuGa</dc:creator>
      <dc:date>2025-10-29T12:58:11Z</dc:date>
    </item>
    <item>
      <title>Re: Change value of previous record based on value of subsequent records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-value-of-previous-record-based-on-value-of-subsequent/m-p/978086#M378559</link>
      <description>&lt;P&gt;Building off of your code, to calculate the start of each player's first winning streak, you could use the lag() function to look back.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data number_game_want;
    length win_streak 3.;
    set number_game;
    by player starttime;
    retain win_streak;
    if first.player then do ;
      win_streak = 0;
      _done=0 ;
    end ;
    if result = 'W' then win_streak + 1;
    else win_streak = 0;

    lag2playtime=lag2(playtime) ;
    if win_streak=3 and not _done then do ;
      retain _done ;
      _done=1 ;
      TimeToWinStreak=lag2playtime-starttime ;
      output ;     
    end ;
    format TimeToWinStreak time5. ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Oct 2025 12:59:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-value-of-previous-record-based-on-value-of-subsequent/m-p/978086#M378559</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2025-10-29T12:59:58Z</dc:date>
    </item>
    <item>
      <title>Re: Change value of previous record based on value of subsequent records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-value-of-previous-record-based-on-value-of-subsequent/m-p/978087#M378560</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/458617"&gt;@LuGa&lt;/a&gt;&amp;nbsp;- actually there's a problem with my code -- if a person has more than one win streak of 3, the subsequent ones will overwrite earlier times, resulting in a worse time.&amp;nbsp; To fix:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;** change this line: ;
if win_streak=3 then do;

** ... to this -- this way, it will only consider a person's first win streak of 3 ;
if win_streak=3 and player not in Tp then do;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Oct 2025 13:03:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-value-of-previous-record-based-on-value-of-subsequent/m-p/978087#M378560</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-10-29T13:03:59Z</dc:date>
    </item>
    <item>
      <title>Re: Change value of previous record based on value of subsequent records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-value-of-previous-record-based-on-value-of-subsequent/m-p/978088#M378561</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;lag2 is a valid option for this use case.&lt;BR /&gt;&lt;BR /&gt;What if the winstreak would need to be e.g. at least 5 wins in a row?</description>
      <pubDate>Wed, 29 Oct 2025 13:16:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-value-of-previous-record-based-on-value-of-subsequent/m-p/978088#M378561</guid>
      <dc:creator>LuGa</dc:creator>
      <dc:date>2025-10-29T13:16:27Z</dc:date>
    </item>
    <item>
      <title>Re: Change value of previous record based on value of subsequent records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-value-of-previous-record-based-on-value-of-subsequent/m-p/978089#M378562</link>
      <description>&lt;P&gt;You can use lag for more than 2 -- here's an example using lag10.&amp;nbsp; I just tend to avoid it, but not for any great reason.&amp;nbsp; Sometimes it's useful and certainly convenient.&amp;nbsp; Just be careful not to use the lag function conditionally.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
do i=1 to 15;
	x=ranuni(0);
	l10=lag10(x);
	output;
end;
run;

proc print data=test; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Oct 2025 13:26:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-value-of-previous-record-based-on-value-of-subsequent/m-p/978089#M378562</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-10-29T13:26:31Z</dc:date>
    </item>
    <item>
      <title>Re: Change value of previous record based on value of subsequent records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-value-of-previous-record-based-on-value-of-subsequent/m-p/978102#M378566</link>
      <description>&lt;P&gt;A minor detail: do any of the "time" variables cross midnight? If you have a starttime of 23:59 and playtime values like 00:10 you have to deal with possible negative time interval values for comparisons of start of win streak.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm not quite sure how your problem would work if the same player had multiple starttime values, play sessions in effect.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For consideration, when you have a two value variable, such as your Result, you may want to consider using a numeric 1/0 value as there a many things that can be done with such much easier than with character values. For example if you code 1 as win and 0 as lose then the SUM of the result over any group of observations is the number of wins, the Mean would be the percent of wins in decimal form, i.e. .60 = 60% wins.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Oct 2025 16:33:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-value-of-previous-record-based-on-value-of-subsequent/m-p/978102#M378566</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2025-10-29T16:33:19Z</dc:date>
    </item>
    <item>
      <title>Re: Change value of previous record based on value of subsequent records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-value-of-previous-record-based-on-value-of-subsequent/m-p/978104#M378567</link>
      <description>&lt;P&gt;You might also try a double "look-ahead":&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
merge
  number_game
  number_game (
    firstobs=2
    keep=player result
    rename=(player=player2 result=result2)
  )
  number_game (
    firstobs=3
    keep=player result
    rename=(player=player3 result=result3)
  )
;
retain start have_streak;
format start time time5.;
if player ne lag(player)
then do;
  start = starttime;
  have_streak = 0;
end;
if
  not have_streak
  and result = "W"
  and player2 = player
  and result2 = "W"
  and player3 = player
  and result3 = "W"
then do;
  have_streak = 1;
  time = playtime - start;
  output;
end;
keep player start time;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As you can see, the parts depending on the number of wins for a streak are repetitive, so they could be auto-created with a macro if different streak lengths are needed.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Oct 2025 17:03:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-value-of-previous-record-based-on-value-of-subsequent/m-p/978104#M378567</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-10-29T17:03:33Z</dc:date>
    </item>
    <item>
      <title>Re: Change value of previous record based on value of subsequent records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-value-of-previous-record-based-on-value-of-subsequent/m-p/978117#M378575</link>
      <description>&lt;P&gt;Assuming I understood what you mean.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data number_game;
    length player $1 result $1;
    input player starttime :time5. playtime :time5. result $;
    format starttime playtime time5.;
    datalines;
1 12:00 12:00 L
1 12:00 12:15 L
1 12:00 12:30 W
1 12:00 12:45 W
1 12:00 13:00 W
1 12:00 13:15 W
1 12:00 13:30 W
1 12:00 13:45 W
2 12:00 12:00 W
2 12:00 12:15 L
2 12:00 12:30 L
2 12:00 12:45 W
2 12:00 13:00 L
2 12:00 13:15 W
2 12:00 13:30 W
2 12:00 13:45 W
3 12:00 12:00 L
3 12:00 12:15 W
3 12:00 12:30 L
3 12:00 12:45 L
3 12:00 13:00 W
3 12:00 13:15 W
3 12:00 13:30 W
3 12:00 13:45 W
;
run;

data temp;
 set number_game;
 by player result notsorted;
 group+first.result;
run;
proc sql;
create table want as
select *,playtime-starttime as shortest_time format=time.
 from temp
  where result='W'
   group by group
    having count(*)&amp;gt;2 and calculated shortest_time=min(calculated shortest_time);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 Oct 2025 02:02:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-value-of-previous-record-based-on-value-of-subsequent/m-p/978117#M378575</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-10-30T02:02:49Z</dc:date>
    </item>
  </channel>
</rss>

