<?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: Using Lag/Lead to replace an observation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-Lead-to-replace-an-observation/m-p/830614#M328200</link>
    <description>Thank you !!</description>
    <pubDate>Fri, 26 Aug 2022 15:05:51 GMT</pubDate>
    <dc:creator>alaxman</dc:creator>
    <dc:date>2022-08-26T15:05:51Z</dc:date>
    <item>
      <title>Using Lag/Lead to replace an observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-Lead-to-replace-an-observation/m-p/830466#M328155</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have the following example data. I would like to write a conditional statement that encompasses the following:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;If last.enrolid and med_name = N;&lt;BR /&gt;and lag(svcdate_new) gt svcdate_orig&lt;BR /&gt;then I would like to replace the lag(svcdate_new) with the svcdate_orig of the "N" medication.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "N" medication will always be the last medication for each unique patient.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the example below, since 7/12/14 (the lag svcdate_new) is greater than 6/19/14 svcdate_orig of the "N" medication, then I would like to replace the 7/12/14 with 6/19/14.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have(label='Prescription Data');
   /*length id $7;*/
   infile datalines truncover;
   input patient_id:$4. med_name:$10. svcdate_orig:MMDDYY8. svcdate_new:MMDDYY8.;
   format svcdate_orig:MMDDYY8. svcdate_new:MMDDYY8.;
   
Datalines;
1 B 1/13/14 1/13/14
1 B 2/10/14 2/12/14
1 B 2/16/14 2/22/14
1 B 3/7/14 3/14/14
1 B 4/3/14 4/13/14
1 B 5/3/14 5/13/14
1 B 5/27/14 6/12/14
1 B 6/6/14 6/27/14
1 B 6/16/14 7/12/14
1 N 6/19/14 7/13/14
;
proc sort data = have;
 by patient_id svcdate_orig;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;What I want the code to do (in red below):&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;Datalines;
1 B 1/13/14 1/13/14
1 B 2/10/14 2/12/14
1 B 2/16/14 2/22/14
1 B 3/7/14 3/14/14
1 B 4/3/14 4/13/14
1 B 5/3/14 5/13/14
1 B 5/27/14 6/12/14
1 B 6/6/14 6/27/14
1 B 6/16/14 &lt;FONT color="#FF0000"&gt;6/19/14&lt;/FONT&gt;
1 N 6/19/14 7/13/14&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;Thank you for any insights you may have!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Aug 2022 02:56:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Lag-Lead-to-replace-an-observation/m-p/830466#M328155</guid>
      <dc:creator>alaxman</dc:creator>
      <dc:date>2022-08-26T02:56:03Z</dc:date>
    </item>
    <item>
      <title>Re: Using Lag/Lead to replace an observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-Lead-to-replace-an-observation/m-p/830469#M328156</link>
      <description>&lt;P&gt;Here is what I want the code to do (the above did not come out correctly!) - in &lt;FONT color="#FF0000"&gt;red&lt;/FONT&gt; below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="72"&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="54"&gt;
&lt;P&gt;B&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="102"&gt;
&lt;P&gt;1/13/14&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="96"&gt;
&lt;P&gt;1/13/14&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="72"&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="54"&gt;
&lt;P&gt;B&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="102"&gt;
&lt;P&gt;2/10/14&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="96"&gt;
&lt;P&gt;2/12/14&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="72"&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="54"&gt;
&lt;P&gt;B&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="102"&gt;
&lt;P&gt;2/16/14&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="96"&gt;
&lt;P&gt;2/22/14&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="72"&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="54"&gt;
&lt;P&gt;B&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="102"&gt;
&lt;P&gt;3/7/14&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="96"&gt;
&lt;P&gt;3/14/14&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="72"&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="54"&gt;
&lt;P&gt;B&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="102"&gt;
&lt;P&gt;4/3/14&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="96"&gt;
&lt;P&gt;4/13/14&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="72"&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="54"&gt;
&lt;P&gt;B&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="102"&gt;
&lt;P&gt;5/3/14&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="96"&gt;
&lt;P&gt;5/13/14&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="72"&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="54"&gt;
&lt;P&gt;B&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="102"&gt;
&lt;P&gt;5/27/14&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="96"&gt;
&lt;P&gt;6/12/14&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="72"&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="54"&gt;
&lt;P&gt;B&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="102"&gt;
&lt;P&gt;6/6/14&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="96"&gt;
&lt;P&gt;6/27/14&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="72"&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="54"&gt;
&lt;P&gt;B&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="102"&gt;
&lt;P&gt;6/16/14&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="96"&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;6/19/14&lt;/FONT&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="72"&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="54"&gt;
&lt;P&gt;N&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="102"&gt;
&lt;P&gt;6/19/14&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="96"&gt;
&lt;P&gt;7/13/14&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Fri, 26 Aug 2022 03:01:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Lag-Lead-to-replace-an-observation/m-p/830469#M328156</guid>
      <dc:creator>alaxman</dc:creator>
      <dc:date>2022-08-26T03:01:26Z</dc:date>
    </item>
    <item>
      <title>Re: Using Lag/Lead to replace an observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-Lead-to-replace-an-observation/m-p/830513#M328170</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&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;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/341454"&gt;@alaxman&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The "N" medication will always be the last medication for each unique patient.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If &lt;FONT face="courier new,courier"&gt;med_name="N"&lt;/FONT&gt;&amp;nbsp;reliably implies &lt;FONT face="courier new,courier"&gt;last.patient_id&lt;/FONT&gt;&amp;nbsp;(in a data step with &lt;FONT face="courier new,courier"&gt;by patient_id&lt;/FONT&gt; ...), you can use a look-ahead merge like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=_:);
merge have have(firstobs=2 keep=patient_id med_name svcdate_orig
                rename=(patient_id=_pid med_name=_mn svcdate_orig=_so));
if patient_id=_pid &amp;amp; _mn='N' &amp;amp; svcdate_new gt _so then svcdate_new=_so;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Otherwise, you would need to look one observation further ahead to ensure that the&amp;nbsp;&lt;FONT face="courier new,courier"&gt;med_name="N"&lt;/FONT&gt; observation is also the last of the patient:&lt;/P&gt;
&lt;PRE&gt;data want(drop=_:);
merge have have(firstobs=2 keep=patient_id med_name svcdate_orig
                rename=(patient_id=_pid med_name=_mn svcdate_orig=_so))
           &lt;STRONG&gt;have(firstobs=3 keep=patient_id rename=(patient_id=_pid2))&lt;/STRONG&gt;;
if patient_id=_pid&lt;STRONG&gt;~=_pid2&lt;/STRONG&gt; &amp;amp; _mn='N' &amp;amp; svcdate_new gt _so then svcdate_new=_so;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Aug 2022 09:34:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Lag-Lead-to-replace-an-observation/m-p/830513#M328170</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-08-26T09:34:19Z</dc:date>
    </item>
    <item>
      <title>Re: Using Lag/Lead to replace an observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-Lead-to-replace-an-observation/m-p/830544#M328179</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have(label='Prescription Data');
   /*length id $7;*/
   infile datalines truncover;
   input patient_id:$4. med_name:$10. svcdate_orig:MMDDYY8. svcdate_new:MMDDYY8.;
   format svcdate_orig:MMDDYY8. svcdate_new:MMDDYY8.;
   
Datalines;
1 B 1/13/14 1/13/14
1 B 2/10/14 2/12/14
1 B 2/16/14 2/22/14
1 B 3/7/14 3/14/14
1 B 4/3/14 4/13/14
1 B 5/3/14 5/13/14
1 B 5/27/14 6/12/14
1 B 6/6/14 6/27/14
1 B 6/16/14 7/12/14
1 N 6/19/14 7/13/14
;
proc sort data = have;
 by patient_id svcdate_orig;
run;

data temp;
set have ;
by patient_id;
if last.patient_id and med_name='N' and 
lag(patient_id)=patient_id and lag(svcdate_new) gt svcdate_orig then
new=svcdate_orig;
format new mmddyy8.;
run;
data want(drop=_: new);
merge temp temp(firstobs=2 keep=patient_id new rename=(patient_id=_pid ));
if patient_id=_pid and not missing(new) then svcdate_new=new;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Aug 2022 11:49:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Lag-Lead-to-replace-an-observation/m-p/830544#M328179</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-08-26T11:49:13Z</dc:date>
    </item>
    <item>
      <title>Re: Using Lag/Lead to replace an observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-Lead-to-replace-an-observation/m-p/830614#M328200</link>
      <description>Thank you !!</description>
      <pubDate>Fri, 26 Aug 2022 15:05:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Lag-Lead-to-replace-an-observation/m-p/830614#M328200</guid>
      <dc:creator>alaxman</dc:creator>
      <dc:date>2022-08-26T15:05:51Z</dc:date>
    </item>
    <item>
      <title>Re: Using Lag/Lead to replace an observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-Lead-to-replace-an-observation/m-p/830637#M328215</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;Thanks very much, your code works well! What if I wanted to add an additional condition where I also want the lag(svcdate_orig) to = 0. All other conditions are the same. Would you just add this requirement to this code or create a new data step accordingly? thanks so much!&lt;/P&gt;</description>
      <pubDate>Fri, 26 Aug 2022 16:48:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Lag-Lead-to-replace-an-observation/m-p/830637#M328215</guid>
      <dc:creator>alaxman</dc:creator>
      <dc:date>2022-08-26T16:48:01Z</dc:date>
    </item>
    <item>
      <title>Re: Using Lag/Lead to replace an observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-Lead-to-replace-an-observation/m-p/830722#M328255</link>
      <description>&lt;P&gt;You want this condition be 'AND' or 'OR' ?&lt;/P&gt;
&lt;P&gt;Assuming you want 'OR' . Here is:&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 patient_id;
if last.patient_id and med_name='N' and 
lag(patient_id)=patient_id and 
(lag(svcdate_new) gt svcdate_orig &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt; or lag(svcdate_orig)= 0&lt;/STRONG&gt;&lt;/FONT&gt;  ) then
new=svcdate_orig;
format new mmddyy8.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 27 Aug 2022 10:17:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Lag-Lead-to-replace-an-observation/m-p/830722#M328255</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-08-27T10:17:23Z</dc:date>
    </item>
  </channel>
</rss>

