<?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: remove duplicate records on the same day visit in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/312051#M61376</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/40773"&gt;@Bal23﻿&lt;/a&gt;&amp;nbsp;- what code have you run ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have preceded a step to read your data and run next code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  infile datalines dlm=',' truncover;
  input id icd $ amoumt date mmddyy10.;
  format date ddmmyy10.;
datalines;
2,v234,78.91,01/01/2015
2,v234,96.05,01/01/2015
3,v44,43.74,02/03/2015
3,v44,22.04,02/05/2015
4,e34,52.26,03/06/2015
4,e34,11.27,09/05/2015
5,j23,379.08,04/01/2015
5,j23,217.12,04/02/2015
; run;

proc sort data=test; by id icd date; run;

data want;
 set test;
  by id;
     retain prev_date; drop prev_date;
     if first.id then do;
        prev_date = date;
        output;
     end; else
     if date - prev_date &amp;gt; 7 then do;
        prev_date = date;
        output;
     end; else delete;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then output contains next lines &amp;nbsp;(date format mmddyy10.):&lt;/P&gt;
&lt;P&gt;2 &amp;nbsp; &amp;nbsp;v234 &amp;nbsp; 78.91 &amp;nbsp;01/01/2015&lt;/P&gt;
&lt;P&gt;3 &amp;nbsp; &amp;nbsp;v44 &amp;nbsp; &amp;nbsp; 43.74 &amp;nbsp;02/03/2015 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;4 &amp;nbsp; &amp;nbsp;e34 &amp;nbsp; &amp;nbsp; 52.26 &amp;nbsp;03/06/2015&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;4 &amp;nbsp; &amp;nbsp;e34 &amp;nbsp; &amp;nbsp; 11.27 &amp;nbsp;09/05/2015&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;5 &amp;nbsp; &amp;nbsp;j23 &amp;nbsp; &amp;nbsp;379.08 &amp;nbsp;04/01/2015&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 16 Nov 2016 17:34:32 GMT</pubDate>
    <dc:creator>Shmuel</dc:creator>
    <dc:date>2016-11-16T17:34:32Z</dc:date>
    <item>
      <title>remove duplicate records on the same day visit</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/311702#M61352</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
PROC SQL;
CREATE TABLE ta1 AS 
SELECT DISTINCT id, icd,   feq, date 

group by id , date,icd, 
order by id, date, icd;


QUIT;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P style="margin: 0in 0in 10pt;"&gt;&lt;SPAN style="background: white; color: black; font-family: &amp;quot;Courier New&amp;quot;;"&gt;&lt;FONT size="3"&gt;Now if I want to remove some records if there are two records on same day, what should I do&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 10pt;"&gt;&lt;SPAN style="background: white; color: black; font-family: &amp;quot;Courier New&amp;quot;;"&gt;&lt;FONT size="3"&gt;I can not simply drop freq=2, because I do want to keep those have two visits on different&amp;nbsp;days with same icd code&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="margin: 0in 0in 10pt;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 14:41:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/311702#M61352</guid>
      <dc:creator>Bal23</dc:creator>
      <dc:date>2016-11-15T14:41:27Z</dc:date>
    </item>
    <item>
      <title>Re: remove duplicate records on the same day visit</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/311713#M61354</link>
      <description>&lt;P&gt;So what defines a duplicate? Id and date alone?&lt;/P&gt;
&lt;P&gt;Removing duplicates&amp;nbsp;usually makes the data less informative. Aggregation&amp;nbsp;is preferred&amp;nbsp;when possible (then you could have count column telling how many original records that contributed).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But, PROC SORT does this (remove duplicates using BY) that you is almost impossible&amp;nbsp;to do in SQL.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 14:52:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/311713#M61354</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-11-15T14:52:04Z</dc:date>
    </item>
    <item>
      <title>Re: remove duplicate records on the same day visit</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/311719#M61355</link>
      <description>&lt;P&gt;for this project, if there are two records with the same icd code, on the same day for the same patient, it might be a duplicate record that is not what I am interested in&lt;/P&gt;
&lt;P&gt;But I want to include those records with two visits, freq=2, with different dates, with same id&lt;/P&gt;
&lt;P&gt;sort is fine, then is there a function, to delete some records with same id, same icd, on the same day?&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 14:59:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/311719#M61355</guid>
      <dc:creator>Bal23</dc:creator>
      <dc:date>2016-11-15T14:59:36Z</dc:date>
    </item>
    <item>
      <title>Re: remove duplicate records on the same day visit</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/311728#M61356</link>
      <description>&lt;P&gt;Your code ask for&amp;nbsp;&lt;STRONG&gt; &amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;DISTINCT&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;id&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; icd&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;   feq&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;date&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;as much as I understand you will get duplicates only if ICD or FEQ differs between rows.&lt;/P&gt;
&lt;P&gt;In such case, &lt;U&gt;which one you want to delete&lt;/U&gt; ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Run next code and check the output dataset (contains the duplicates) :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;proc sort data=ta1; by ID DATE; run;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;data dup;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp; set ta1;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;by id &amp;nbsp;date;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if not (first.date and last.date);&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;run;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 15:11:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/311728#M61356</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-11-15T15:11:06Z</dc:date>
    </item>
    <item>
      <title>Re: remove duplicate records on the same day visit</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/311743#M61358</link>
      <description>&lt;P&gt;&amp;nbsp;I ran it, i still have same day records&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;please see table below, here only patient id 4, these two records are what i need, because these two visists are on two different dates, for id2&amp;nbsp; , because they have same icd code, same date, same patient id, i need to remove&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;by looking at the data, i notice, i also need to remove id 3, the&amp;nbsp;billing date is 2 day difference, id5, one day&amp;nbsp;difference,&amp;nbsp;it might still be about the same visit, this is NOT i am interested in, so i need to remove those records with same id, same icd, same date or two billing date difference less than one week&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thanks.&lt;/P&gt;
&lt;TABLE width="400"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="80"&gt;
&lt;P&gt;id&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="80"&gt;
&lt;P&gt;icd&lt;/P&gt;
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t;2&lt;/P&gt;
&lt;/TD&gt;
&lt;TD&gt;
&lt;P&gt;217.12&lt;/P&gt;
&lt;/TD&gt;
&lt;TD&gt;
&lt;P&gt;4/2/2015&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Tue, 15 Nov 2016 16:39:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/311743#M61358</guid>
      <dc:creator>Bal23</dc:creator>
      <dc:date>2016-11-15T16:39:44Z</dc:date>
    </item>
    <item>
      <title>Re: remove duplicate records on the same day visit</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/311833#M61362</link>
      <description>&lt;P&gt;Of course you get more than on row with same date.&lt;/P&gt;
&lt;P&gt;I wanted to show you the dupliacte date records.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Look at ID=2. On 1st row amount=78.91 while on 2nd row amount=96.05.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Which row you prefer to &lt;STRONG&gt;delete&lt;/STRONG&gt; ? or maybe you want to &lt;STRONG&gt;sum the amounts&lt;/STRONG&gt; per ID ICD BILLINGDATE ?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 19:27:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/311833#M61362</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-11-15T19:27:18Z</dc:date>
    </item>
    <item>
      <title>Re: remove duplicate records on the same day visit</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/311840#M61363</link>
      <description>&lt;P&gt;Thank you and I am sorry I have not made it clear.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;when the bill date difference is within 7 days, even it has three or records, I want to consider it as one visit only, there fore, I only want to keep the first day record, and delete all other billing records, which are after the first record, but not later than 7 days,&lt;/P&gt;
&lt;P&gt;I need to create a variable, timelapase=time2-time1, and if timelapse &amp;gt; 0 but &amp;lt;=7, then all those later records will be deleted, only the first record will be kept&lt;/P&gt;
&lt;P&gt;here, the record with 78.91&amp;nbsp; will be recorded&lt;/P&gt;
&lt;P&gt;i sort it by id, date, and icd&lt;/P&gt;
&lt;P&gt;then if first.id=1 and first.date=1 and first.icd=1, then timelaspse=0, time=date;&lt;/P&gt;
&lt;P&gt;else timelapse=date(the next record)-time,, or date3(the records after that)=date-time&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but I do not know how to do that&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 19:56:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/311840#M61363</guid>
      <dc:creator>Bal23</dc:creator>
      <dc:date>2016-11-15T19:56:15Z</dc:date>
    </item>
    <item>
      <title>Re: remove duplicate records on the same day visit</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/311842#M61364</link>
      <description>&lt;P&gt;Then try next code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
&amp;nbsp; set have; &amp;nbsp;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;by ID DATE TCD; &amp;nbsp; /* assumed data is sorted */
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; retain prev_date; drop prev_date;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if first.ID then do;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; prev_date = date;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; output;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;end; else
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if date - prev_date &amp;gt; 7 then do;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; prev_date = date;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; output;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;end; else delete;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 20:19:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/311842#M61364</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-11-15T20:19:06Z</dc:date>
    </item>
    <item>
      <title>Re: remove duplicate records on the same day visit</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/311913#M61366</link>
      <description>&lt;P&gt;Assuming data HAVE is sorted by id/date, you can use&amp;nbsp;the DIF function (where&amp;nbsp;DIF(X) is defined as&amp;nbsp;X-LAG(X)).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id ;
  if first.id or dif(date)&amp;gt;7;
run;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note: If FIRST.ID is true, it is logically superfluous to evaluate the DIF function, but SAS evaluates it anyhow-(which is good, because the DIF function needs to be evaluated for every record).&amp;nbsp; I did a test to demonstrate that it works as desired.&amp;nbsp; But if you're really nervous,&amp;nbsp;the subsetting IF statement could be modified to&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if dif(date)&amp;gt;7  or first.id;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;which would insure that DIF&amp;nbsp; is always evaluated (and the underlying lag queues are always updated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Mark&lt;/P&gt;</description>
      <pubDate>Wed, 16 Nov 2016 06:17:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/311913#M61366</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2016-11-16T06:17:17Z</dc:date>
    </item>
    <item>
      <title>Re: remove duplicate records on the same day visit</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/311916#M61367</link>
      <description>&lt;P&gt;If someone is visiting twice a week, then the 3rd or 4th visit will be after more than 7 days since first visit.&lt;/P&gt;
&lt;P&gt;Would you like to delete it or save it ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using filter like&amp;nbsp;&lt;STRONG&gt; if first.id or dif(date) &amp;gt; 7&amp;nbsp;&lt;/STRONG&gt;will delete the 3rd and the 4th visit,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;while&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;  
       by ID DATE TCD;   /* assumed data is sorted */
            retain prev_date; drop prev_date;
           
           if first.ID then do;
              prev_date = date;
              output;
           end; else
           if date - prev_date &amp;gt; 7 then do;
              prev_date = date;
              output;
           end; else delete;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;will preserve, at least the 4th visit.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Nov 2016 06:55:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/311916#M61367</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-11-16T06:55:50Z</dc:date>
    </item>
    <item>
      <title>Re: remove duplicate records on the same day visit</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/311945#M61370</link>
      <description>&lt;P&gt;And the avoidance of DIF in the alternative would also get the 7th, 10th, 13th dates if it was a multi-week series at two dates per week.&amp;nbsp; But it would not get visits 8,9,11,12.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In such a case a more likely objective would be to determine the first date (BEGDATE), last date (ENDDATE) and number of dates in each series - where a series is defined as a sequence of&amp;nbsp; dates with no internal&amp;nbsp; gaps over 7 days.&amp;nbsp; The example below produces such results using two DIF functions and a merge statement of a HAVE&amp;nbsp;with itself (with the 2nd have using FIRSTOBS=2).&amp;nbsp; Note the merge does not have an associated BY statement.&amp;nbsp; If it did, the program would fail:&lt;/P&gt;
&lt;P&gt;&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 want (drop=next_:);
  format begdate yymmddn8.;

  do ndates=1 by 1 until (dif(next_id)&amp;gt;0 or dif(next_date)&amp;gt;7);
    merge have (rename=(date=enddate))
          have (firstobs=2 keep=id date rename=(id=next_id date=next_date));
    if begdate=. then begdate=date;
  end;

run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course,&amp;nbsp; in the original problem there are other variables that are being ignored in&amp;nbsp;this new program.&amp;nbsp; But my response here is part of my campaign to make folks more informed, and more comfortable with the LAG and DIF functions.&amp;nbsp; I think they are often&amp;nbsp;unnecessarily avoided.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;regards,&lt;/P&gt;
&lt;P&gt;Mark&lt;/P&gt;</description>
      <pubDate>Wed, 16 Nov 2016 10:02:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/311945#M61370</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2016-11-16T10:02:02Z</dc:date>
    </item>
    <item>
      <title>Re: remove duplicate records on the same day visit</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/311996#M61372</link>
      <description>&lt;P&gt;Thanks. I did try your code, and I deleted all records after the first record for the same patient.&lt;/P&gt;
&lt;P&gt;I do not know where the problem is and how to fix, I still want to keep later records, which are after 7 days&lt;/P&gt;</description>
      <pubDate>Wed, 16 Nov 2016 14:34:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/311996#M61372</guid>
      <dc:creator>Bal23</dc:creator>
      <dc:date>2016-11-16T14:34:10Z</dc:date>
    </item>
    <item>
      <title>Re: remove duplicate records on the same day visit</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/312051#M61376</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/40773"&gt;@Bal23﻿&lt;/a&gt;&amp;nbsp;- what code have you run ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have preceded a step to read your data and run next code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  infile datalines dlm=',' truncover;
  input id icd $ amoumt date mmddyy10.;
  format date ddmmyy10.;
datalines;
2,v234,78.91,01/01/2015
2,v234,96.05,01/01/2015
3,v44,43.74,02/03/2015
3,v44,22.04,02/05/2015
4,e34,52.26,03/06/2015
4,e34,11.27,09/05/2015
5,j23,379.08,04/01/2015
5,j23,217.12,04/02/2015
; run;

proc sort data=test; by id icd date; run;

data want;
 set test;
  by id;
     retain prev_date; drop prev_date;
     if first.id then do;
        prev_date = date;
        output;
     end; else
     if date - prev_date &amp;gt; 7 then do;
        prev_date = date;
        output;
     end; else delete;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then output contains next lines &amp;nbsp;(date format mmddyy10.):&lt;/P&gt;
&lt;P&gt;2 &amp;nbsp; &amp;nbsp;v234 &amp;nbsp; 78.91 &amp;nbsp;01/01/2015&lt;/P&gt;
&lt;P&gt;3 &amp;nbsp; &amp;nbsp;v44 &amp;nbsp; &amp;nbsp; 43.74 &amp;nbsp;02/03/2015 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;4 &amp;nbsp; &amp;nbsp;e34 &amp;nbsp; &amp;nbsp; 52.26 &amp;nbsp;03/06/2015&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;4 &amp;nbsp; &amp;nbsp;e34 &amp;nbsp; &amp;nbsp; 11.27 &amp;nbsp;09/05/2015&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;5 &amp;nbsp; &amp;nbsp;j23 &amp;nbsp; &amp;nbsp;379.08 &amp;nbsp;04/01/2015&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Nov 2016 17:34:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/remove-duplicate-records-on-the-same-day-visit/m-p/312051#M61376</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-11-16T17:34:32Z</dc:date>
    </item>
  </channel>
</rss>

