<?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: Changing value of previous record based on value in current record in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689434#M209598</link>
    <description>&lt;P&gt;As in the last time we talked, I think the hop macro from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/51532"&gt;@LeonidBatkhan&lt;/a&gt;&amp;nbsp;would work well here.&amp;nbsp; As you read each record, you want to read the daysupp_new variable from the next record.&amp;nbsp; If daysupp_new is populated, you would want to update daysupp in the current record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
    <pubDate>Wed, 07 Oct 2020 04:24:08 GMT</pubDate>
    <dc:creator>jimbarbour</dc:creator>
    <dc:date>2020-10-07T04:24:08Z</dc:date>
    <item>
      <title>Changing value of previous record based on value in current record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689419#M209590</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Patient ID&lt;/TD&gt;&lt;TD&gt;svcdate&lt;/TD&gt;&lt;TD&gt;daysupp&lt;/TD&gt;&lt;TD&gt;prodnme&lt;/TD&gt;&lt;TD&gt;daysupp_new&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;9/29/14&lt;/TD&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;TD&gt;ZUBSOLV&lt;/TD&gt;&lt;TD&gt;-&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;10/2/14&lt;/TD&gt;&lt;TD&gt;30&lt;/TD&gt;&lt;TD&gt;ZUBSOLV&lt;/TD&gt;&lt;TD&gt;-&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;10/9/14&lt;/TD&gt;&lt;TD&gt;30&lt;/TD&gt;&lt;TD&gt;NALTREXONE HCL&lt;/TD&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've computed daysupp_new as 7 for when prodnme =&amp;nbsp;NALTREXONE HCL&lt;/P&gt;&lt;P&gt;Now I want to update the daysupp value in row #2 with the daysupp_new value from row #3 so that the data looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Patient ID&lt;/TD&gt;&lt;TD&gt;svcdate&lt;/TD&gt;&lt;TD&gt;daysupp&lt;/TD&gt;&lt;TD&gt;prodnme&lt;/TD&gt;&lt;TD&gt;daysupp_new&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;9/29/14&lt;/TD&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;TD&gt;ZUBSOLV&lt;/TD&gt;&lt;TD&gt;-&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;10/2/14&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;7&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;ZUBSOLV&lt;/TD&gt;&lt;TD&gt;-&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;10/9/14&lt;/TD&gt;&lt;TD&gt;30&lt;/TD&gt;&lt;TD&gt;NALTREXONE HCL&lt;/TD&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I do this?&lt;/P&gt;</description>
      <pubDate>Wed, 07 Oct 2020 03:27:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689419#M209590</guid>
      <dc:creator>alaxman</dc:creator>
      <dc:date>2020-10-07T03:27:01Z</dc:date>
    </item>
    <item>
      <title>Re: Changing value of previous record based on value in current record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689429#M209594</link>
      <description>What’s the logic/rule? That just for that single row or does it need to generalize to other data?</description>
      <pubDate>Wed, 07 Oct 2020 04:02:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689429#M209594</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-10-07T04:02:57Z</dc:date>
    </item>
    <item>
      <title>Re: Changing value of previous record based on value in current record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689434#M209598</link>
      <description>&lt;P&gt;As in the last time we talked, I think the hop macro from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/51532"&gt;@LeonidBatkhan&lt;/a&gt;&amp;nbsp;would work well here.&amp;nbsp; As you read each record, you want to read the daysupp_new variable from the next record.&amp;nbsp; If daysupp_new is populated, you would want to update daysupp in the current record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Wed, 07 Oct 2020 04:24:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689434#M209598</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-10-07T04:24:08Z</dc:date>
    </item>
    <item>
      <title>Re: Changing value of previous record based on value in current record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689436#M209599</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think the below graphic may illustrate what (I think)&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/341454"&gt;@alaxman&lt;/a&gt;&amp;nbsp;is after.&amp;nbsp; Basically, when on row 2, if the daysupp_new variable is populated on the next record (row 3), update daysupp on row 2 with the value in daysupp_new on row three.&amp;nbsp; I'm recommending the "hop" macro from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/51532"&gt;@LeonidBatkhan&lt;/a&gt;&amp;nbsp;which performs a read ahead (the equivalent of a LEAD() function if there were one in SAS).&amp;nbsp; Link:&amp;nbsp;&amp;nbsp;&lt;A href="https://blogs.sas.com/content/sgf/2017/11/01/hopping-for-the-best-calculations-across-sas-dataset-observations/" target="_blank" rel="noopener"&gt;https://blogs.sas.com/content/sgf/2017/11/01/hopping-for-the-best-calculations-across-sas-dataset-observations/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1602044993367.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/50250i3818F09471DF8DAF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jimbarbour_0-1602044993367.png" alt="jimbarbour_0-1602044993367.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Oct 2020 04:30:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689436#M209599</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-10-07T04:30:02Z</dc:date>
    </item>
    <item>
      <title>Re: Changing value of previous record based on value in current record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689503#M209624</link>
      <description>&lt;P&gt;Your Q is ambiguous . better post more data to illustrate .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs ;
input PatientID	svcdate	: $20.daysupp	prodnme : $20.	daysupp_new;
cards;
123	9/29/14	7	ZUBSOLV	.
123	10/2/14	30	ZUBSOLV	.
123	10/9/14	30	NALTREXONE_HCL	7
;

data want;
 set have;
 if _n_=2 then do;
    temp=3;
    set have(keep=daysupp_new rename=(daysupp_new=daysupp)) point=temp;
 end;
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Oct 2020 11:32:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689503#M209624</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-10-07T11:32:03Z</dc:date>
    </item>
    <item>
      <title>Re: Changing value of previous record based on value in current record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689509#M209628</link>
      <description>&lt;P&gt;You cannot change a value in a &lt;EM&gt;previous&lt;/EM&gt; observation, but you can change a value in a &lt;EM&gt;current&lt;/EM&gt; observation based on the &lt;EM&gt;following&lt;/EM&gt; observation ("look-ahead"):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm=',' dsd truncover;
input Id $ svcdate :mmddyy10. daysupp prodnme :$20. daysupp_new;
format svcdate yymmdd10.;
datalines;
123,9/29/14,7,ZUBSOLV,
123,10/2/14,30,ZUBSOLV,
123,10/9/14,30,NALTREXONE HCL,7
;

data want;
merge
  have
  have (
    firstobs=2
    keep=id daysupp_new
    rename=(id=_id daysupp_new = _dn)
  )
;
if id = _id and _dn ne . then daysupp = _dn;
drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can incorporate the logic that sets daysupp_new in this step, and avoid creating that column altogether.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Oct 2020 11:54:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689509#M209628</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-07T11:54:46Z</dc:date>
    </item>
    <item>
      <title>Re: Changing value of previous record based on value in current record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689544#M209636</link>
      <description>&lt;P&gt;Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37107"&gt;@jimbarbour&lt;/a&gt;!&lt;/P&gt;
&lt;P&gt;While the %hop macro should work fine here, an alternative way of calculating DAYSUPP_NEW could be:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;sort the data set in reverse chronological order;&lt;/LI&gt;
&lt;LI&gt;use lag() function, as now your "next" raw becomes your "previous" row;&lt;/LI&gt;
&lt;LI&gt;restore/sort the data set in chronological order.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Here is a code example illustrating this alternative method:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=HAVE out=REVERSE;
   by PATIENT_ID descending SVCDATE;
run;

data WANT (drop=PREDATE);
   set REVERSE;
   PREDATE = lag(SVCDATE);
   if PRODNME eq 'NALTREXONE HCL' then DAYSUPP_NEW = PREDATE;
run;

proc sort data=WANT;
   by PATIENT_ID SVCDATE;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hope this helps.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Oct 2020 13:30:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689544#M209636</guid>
      <dc:creator>LeonidBatkhan</dc:creator>
      <dc:date>2020-10-07T13:30:24Z</dc:date>
    </item>
    <item>
      <title>Re: Changing value of previous record based on value in current record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689565#M209643</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37107"&gt;@jimbarbour&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How would I use the "hop" function within this macro that i've written to get the "daysupp_new" - in order to change "daysupp" -- exactly how you have illustrated in the table below.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt;&lt;/SPAN&gt; hop(d,x,y,j);&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;_p_ = _n_ + &amp;amp;j;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;if (&lt;SPAN class="s2"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/SPAN&gt; le _p_ le _o_) then set &amp;amp;d(keep=&amp;amp;x rename=(&amp;amp;x=&amp;amp;y)) point=_p_ nobs=_o_;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt;&lt;/SPAN&gt; hop;&lt;/P&gt;&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/SPAN&gt; pers_3M_12M_30DAY (&lt;SPAN class="s3"&gt;drop&lt;/SPAN&gt;=TEMP1);&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s3"&gt;set&lt;/SPAN&gt; pers_3M_12M_30DAY;&lt;/P&gt;&lt;P class="p3"&gt;&lt;SPAN class="s3"&gt;if&lt;/SPAN&gt;&lt;SPAN class="s4"&gt; prodnme in (&lt;/SPAN&gt;"VIVITROL"&lt;SPAN class="s4"&gt;, &lt;/SPAN&gt;"REVIA"&lt;SPAN class="s4"&gt;, &lt;/SPAN&gt;"NALTREXONE HCL"&lt;SPAN class="s4"&gt;, &lt;/SPAN&gt;"NALTREXONE"&lt;SPAN class="s4"&gt;)&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s3"&gt;and&lt;/SPAN&gt; svcdate_new le lag(svcdate_new)+ lag(daysupp)&lt;/P&gt;&lt;P class="p4"&gt;thendo&lt;SPAN class="s4"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;%&lt;STRONG&gt;&lt;I&gt;hop&lt;/I&gt;&lt;/STRONG&gt;(pers_3M_12M_30DAY,SVCDATE_NEW, TEMP1,-&lt;SPAN class="s2"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/SPAN&gt;)&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;DAYSUPP_NEW= SVCDATE-TEMP1;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s3"&gt;end&lt;/SPAN&gt;;&lt;/P&gt;&lt;P class="p5"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;SPAN class="s4"&gt;;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Oct 2020 14:42:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689565#M209643</guid>
      <dc:creator>alaxman</dc:creator>
      <dc:date>2020-10-07T14:42:31Z</dc:date>
    </item>
    <item>
      <title>Re: Changing value of previous record based on value in current record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689588#M209652</link>
      <description>Do you have SAS ETS licensed? If so, you can also tweak things so PROC EXPAND and LEAD work for what you need.</description>
      <pubDate>Wed, 07 Oct 2020 15:27:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689588#M209652</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-10-07T15:27:35Z</dc:date>
    </item>
    <item>
      <title>Re: Changing value of previous record based on value in current record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689603#M209659</link>
      <description>Needs to generalize to other data</description>
      <pubDate>Wed, 07 Oct 2020 15:51:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689603#M209659</guid>
      <dc:creator>alaxman</dc:creator>
      <dc:date>2020-10-07T15:51:00Z</dc:date>
    </item>
    <item>
      <title>Re: Changing value of previous record based on value in current record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689606#M209661</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37107"&gt;@jimbarbour&lt;/a&gt;&lt;BR /&gt;&lt;BR /&gt;Sorry I meant to ask this as a question &amp;amp; forgot the question mark. I have used the "hop" function as I note in my code that I recently posted to get the "7" figure as illustrated in your table. But how would I then change "daysupp" to "7" next to the svcdate "10/2" in the table?&lt;BR /&gt;&lt;BR /&gt;Thank you!</description>
      <pubDate>Wed, 07 Oct 2020 15:54:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689606#M209661</guid>
      <dc:creator>alaxman</dc:creator>
      <dc:date>2020-10-07T15:54:43Z</dc:date>
    </item>
    <item>
      <title>Re: Changing value of previous record based on value in current record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689644#M209669</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/341454"&gt;@alaxman&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data I have is as follows below.&amp;nbsp; If you have more data, I'm happy to run it through, but the program I've written (I'll get to that in a minute)&amp;nbsp;&lt;EM&gt;should&lt;/EM&gt; be able to handle different data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA	Drug_Data;
	INFILE	Datalines	DSD	DLM='09'x	MISSOVER;

	INPUT	Patient_ID	$
			SvcDate		:	ANYDTDTE8.
			DaySupp
			ProdNme		:	$25.
			;
	FORMAT	SvcDate		mmddyys10.;

Datalines;
123	9/29/14	7	ZUBSOLV
123	10/2/14	30	ZUBSOLV
123	10/9/14	30	NALTREXONE HCL
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Next, with apologies to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/51532"&gt;@LeonidBatkhan&lt;/a&gt;, I modified the Hop macro slightly to set a Boolean, _Found.&amp;nbsp; If a record is returned, _Found =1, otherwise, _Found = 0.&amp;nbsp; I wanted to NOT execute the code to update DaySupp if no next record were found.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO	Hop(d, x, y, j);
	_p_	=	_n_ + &amp;amp;j;
	IF	(1 &amp;lt;= _p_ &amp;lt;= _o_)	THEN
		DO;
			_Found	=	1;
			SET	&amp;amp;d(KEEP=&amp;amp;x RENAME=(&amp;amp;x=&amp;amp;y)) POINT=_p_ NOBS=_o_;
		END;
	ELSE
		DO;
			_Found	=	0;
		END;
%MEND	Hop;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Next, I modified the program I shared with you earlier to incorporate the Hop macro.&amp;nbsp; The code is actually fairly simple and is a single step, a big advantage to the Hop macro.&amp;nbsp; You could even make it simpler by deleting all my "_Prior" logic and replace it with LAG() based logic.&amp;nbsp; I personally don't like LAG().&amp;nbsp; When I've used LAG() with large files, performance has been adversely affected.&amp;nbsp; Manually setting _Prior type variables is more work, but performance is much better in my experience.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The trick with the program is that I'm doing a look forward not a look back.&amp;nbsp; I do &lt;EM&gt;not&lt;/EM&gt; read record 3 and try to back update record 2.&amp;nbsp; That's not typically how SAS works.&amp;nbsp; Instead, when I read record 2, I read ahead to record 3 and update record 2 in place.&amp;nbsp; Variables prefixed with "_Next" are from read aheads to the record following the current record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's the program:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA	Updated_Drug_Data;
	DROP	_:;
	SET		Drug_Data;

	LENGTH	_Prior_ID		$8;
	LENGTH	_Prior_Drug		$25.;

	LENGTH	_Next_ProdNme	$25.;

	RETAIN	_Prior_ID		' ';
	RETAIN	_Prior_Date		0;
	RETAIN	_Prior_Supp		0;
	RETAIN	_Prior_Drug		' ';

	IF	Patient_ID					=	_Prior_ID												THEN
		DO;
			IF	ProdNme				=	_Prior_Drug												THEN
				DO;
					SvcDate			=	INTNX('DAYS', _Prior_Date, _Prior_Supp);
				END;

			%Hop(Drug_Data, ProdNme, _Next_ProdNme, 1);
			IF	_Found																			THEN
				IF	_Next_ProdNme	IN	("VIVITROL", "REVIA", "NALTREXONE HCL", "NALTREXONE")	THEN
					DO;
						%Hop(Drug_Data, SvcDate, _Next_SvcDate, 1);
						DaySupp		=	INTCK('DAYS', SvcDate, _Next_SvcDate);
					END;
		END;

	_Prior_ID						=	Patient_ID;
	_Prior_Date						=	SvcDate;
	_Prior_Supp						=	DaySupp;
	_Prior_Drug						=	ProdNme;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The program takes the input data:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1602092450462.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/50279iEBA3A5082C3B7CBF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jimbarbour_0-1602092450462.png" alt="jimbarbour_0-1602092450462.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And makes the following changes:&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The SvcDate on record 2 is adjusted per the DaySupp on record 1.&amp;nbsp; The SvcDate on record 2 is set to 7 days after the SvcDate on record 1.&amp;nbsp; This is conventional processing and does not use the Hop macro.&lt;/LI&gt;
&lt;LI&gt;The DaySupp on record 2 is adjusted per the SvcDate on record 3.&amp;nbsp; I make the update when record 2 is the current record.&amp;nbsp; I use the Hop macro to read ahead and get a) the ProdName and b) the SvcDate from record 3 while I am processing record 2.&amp;nbsp; Record 2 is updated based on the data from record 3.&amp;nbsp; You'll notice that the DaySupp on record two is&amp;nbsp;&lt;STRONG&gt;3&lt;/STRONG&gt;&amp;nbsp;(not &lt;STRONG&gt;7&lt;/STRONG&gt;).&amp;nbsp; This is because I updated the SvcDate of record 2 based on the DaySupp of record 1.&amp;nbsp; If we don't update the SvcDate, then DaySupp on record 2 would be 7 (10/02/2014 to 10/09/2014).&amp;nbsp; I believe the logic to be correct.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_1-1602092583338.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/50280i04418EEB4CE1FCC3/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jimbarbour_1-1602092583338.png" alt="jimbarbour_1-1602092583338.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This reflects my understanding of what you're trying to do.&amp;nbsp; Please correct me if I'm off, and we can adjust the program accordingly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Oct 2020 17:56:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689644#M209669</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-10-07T17:56:22Z</dc:date>
    </item>
    <item>
      <title>Re: Changing value of previous record based on value in current record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689849#M209747</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37107"&gt;@jimbarbour&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much for this thorough explanation, really appreciate it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;While your code in its entirety did not work, your modified "hop" macro was really key, so thank you!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is what worked:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data pers_3M_12M_30DAY ;
set pers_3M_12M_30DAY;
%Hop1(pers_3M_12M_30DAY, ProdNme, _Next_ProdNme, 1);
			IF	_Found	THEN
				IF	_Next_ProdNme	IN	("VIVITROL", "REVIA", "NALTREXONE HCL", "NALTREXONE")
				THEN 
				%Hop1(pers_3M_12M_30DAY, DAYSUPP_NEW, _Next_DAYSUPP_NEW, 1);
				IF _Next_DAYSUPP_NEW ne . then do;
				daysupp = _Next_DAYSUPP_NEW;
drop _:;
				end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P class="p4"&gt;Many thanks again!&lt;/P&gt;
&lt;P class="p4"&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Oct 2020 03:55:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689849#M209747</guid>
      <dc:creator>alaxman</dc:creator>
      <dc:date>2020-10-08T03:55:46Z</dc:date>
    </item>
    <item>
      <title>Re: Changing value of previous record based on value in current record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689857#M209753</link>
      <description>&lt;P&gt;Actually, this is even better than if I had just handed you a solution.&amp;nbsp; You took the things you needed from my code and arranged them to fulfill your requirements.&amp;nbsp; Good job!&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":thumbs_up:"&gt;👍&lt;/span&gt;&amp;nbsp; Far better to understand code advice and apply it than to simply play "monkey see, monkey do."&amp;nbsp; Hopefully some of this code will come in handy as well for future applications.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck, and glad I was (hopefully) able to pass on a couple of good SAS tips.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Thu, 08 Oct 2020 04:12:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-record-based-on-value-in-current/m-p/689857#M209753</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-10-08T04:12:41Z</dc:date>
    </item>
  </channel>
</rss>

