<?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: how to check date overlap: last visit's end date is later than this visit's start date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-check-date-overlap-last-visit-s-end-date-is-later-than/m-p/327434#M73046</link>
    <description>&lt;P&gt;You're welcome!&lt;/P&gt;</description>
    <pubDate>Wed, 25 Jan 2017 16:28:41 GMT</pubDate>
    <dc:creator>collinelliot</dc:creator>
    <dc:date>2017-01-25T16:28:41Z</dc:date>
    <item>
      <title>how to check date overlap: last visit's end date is later than this visit's start date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-check-date-overlap-last-visit-s-end-date-is-later-than/m-p/327412#M73034</link>
      <description>&lt;P&gt;For example, in the following data, for ID A002, visit 1's end date is 3/25/2016, but visit 2's start date is 3/24/2016.&lt;/P&gt;
&lt;P&gt;How to identify such data issues?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="323"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;ID&lt;/TD&gt;
&lt;TD width="64"&gt;visitnum&lt;/TD&gt;
&lt;TD width="94"&gt;start_date&lt;/TD&gt;
&lt;TD width="101"&gt;end_date&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A001&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;8/26/2015&lt;/TD&gt;
&lt;TD&gt;9/3/2015&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;9/17/2015&lt;/TD&gt;
&l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t;
&lt;TD&gt;5/25/2016&lt;/TD&gt;
&lt;TD&gt;5/25/2016&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;nex&lt;/P&gt;</description>
      <pubDate>Wed, 25 Jan 2017 15:50:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-check-date-overlap-last-visit-s-end-date-is-later-than/m-p/327412#M73034</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2017-01-25T15:50:44Z</dc:date>
    </item>
    <item>
      <title>Re: how to check date overlap: last visit's end date is later than this visit's start date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-check-date-overlap-last-visit-s-end-date-is-later-than/m-p/327415#M73036</link>
      <description>&lt;P&gt;One way is to use a retain to carry the previous end date to the next observation and then flag it. Something like the below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input ID $ visitnum	start_date :mmddyy10. end_date :mmddyy10.;
    datalines;
A001 1 8/26/2015 9/3/2015
A001 2 9/17/2015 9/17/2017
A001 3 10/2/2015 10/2/2015
A001 4 10/30/2015 10/30/2015
A001 5 11/24/2015 11/24/2015
A002 1 3/17/2016 3/25/2016
A002 2 3/24/2016 3/24/2016
A002 3 3/31/2016 3/31/2016
A002 4 4/28/2016 4/28/2016
A002 5 5/25/2016 5/25/2016
;

data want;  
    retain _lst;
    set have;
    by id visitnum;
    if first.id then do;
        flag = 0;
        _lst = end_date;
    end;
    else do;
        flag = (_lst &amp;gt; end_date);
        _lst = end_date;
    end;
    format start_date end_date mmddyy10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Jan 2017 15:57:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-check-date-overlap-last-visit-s-end-date-is-later-than/m-p/327415#M73036</guid>
      <dc:creator>collinelliot</dc:creator>
      <dc:date>2017-01-25T15:57:30Z</dc:date>
    </item>
    <item>
      <title>Re: how to check date overlap: last visit's end date is later than this visit's start date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-check-date-overlap-last-visit-s-end-date-is-later-than/m-p/327416#M73037</link>
      <description>&lt;P&gt;First would be ensure that the values are SAS date values and not character. Is that the case?&lt;/P&gt;</description>
      <pubDate>Wed, 25 Jan 2017 15:59:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-check-date-overlap-last-visit-s-end-date-is-later-than/m-p/327416#M73037</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-01-25T15:59:30Z</dc:date>
    </item>
    <item>
      <title>Re: how to check date overlap: last visit's end date is later than this visit's start date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-check-date-overlap-last-visit-s-end-date-is-later-than/m-p/327425#M73041</link>
      <description>My dates are in yyyy-mm-dd format in the real SAS dateset, which can be compared differectly either in numeric format or character format. I did not pay attention when typing the example dates in excel.</description>
      <pubDate>Wed, 25 Jan 2017 16:18:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-check-date-overlap-last-visit-s-end-date-is-later-than/m-p/327425#M73041</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2017-01-25T16:18:09Z</dc:date>
    </item>
    <item>
      <title>Re: how to check date overlap: last visit's end date is later than this visit's start date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-check-date-overlap-last-visit-s-end-date-is-later-than/m-p/327429#M73043</link>
      <description>&lt;P&gt;Thanks, Collinelliot.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But somehow there is a wrong flag when I ran your code. see red arrow below.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/6882i45758F77AEAE1CE2/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="Capture.PNG" title="Capture.PNG" /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Jan 2017 16:22:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-check-date-overlap-last-visit-s-end-date-is-later-than/m-p/327429#M73043</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2017-01-25T16:22:25Z</dc:date>
    </item>
    <item>
      <title>Re: how to check date overlap: last visit's end date is later than this visit's start date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-check-date-overlap-last-visit-s-end-date-is-later-than/m-p/327431#M73044</link>
      <description>&lt;P&gt;The previous date is in 2017, which is later than the next date, so the flag seems correct. Unless I don't understand what you want.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Jan 2017 16:25:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-check-date-overlap-last-visit-s-end-date-is-later-than/m-p/327431#M73044</guid>
      <dc:creator>collinelliot</dc:creator>
      <dc:date>2017-01-25T16:25:57Z</dc:date>
    </item>
    <item>
      <title>Re: how to check date overlap: last visit's end date is later than this visit's start date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-check-date-overlap-last-visit-s-end-date-is-later-than/m-p/327433#M73045</link>
      <description>&lt;P&gt;you are right. It was a typo (2017 should be 2015). Your code works! Thank you&lt;/P&gt;</description>
      <pubDate>Wed, 25 Jan 2017 16:27:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-check-date-overlap-last-visit-s-end-date-is-later-than/m-p/327433#M73045</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2017-01-25T16:27:32Z</dc:date>
    </item>
    <item>
      <title>Re: how to check date overlap: last visit's end date is later than this visit's start date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-check-date-overlap-last-visit-s-end-date-is-later-than/m-p/327434#M73046</link>
      <description>&lt;P&gt;You're welcome!&lt;/P&gt;</description>
      <pubDate>Wed, 25 Jan 2017 16:28:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-check-date-overlap-last-visit-s-end-date-is-later-than/m-p/327434#M73046</guid>
      <dc:creator>collinelliot</dc:creator>
      <dc:date>2017-01-25T16:28:41Z</dc:date>
    </item>
    <item>
      <title>Re: how to check date overlap: last visit's end date is later than this visit's start date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-check-date-overlap-last-visit-s-end-date-is-later-than/m-p/327439#M73049</link>
      <description>&lt;P&gt;Both of these are untested.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you only want to flag the second record of the overlap then a lag function and a by statemetn are the needed tools.&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=0 and start_date&amp;lt;lag(end_date)) then flag=1;
    else flag=0;
run;
&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;It's a few more lines if you want to flag both the leading and trailing elements of the overlap.&amp;nbsp; In this case, flag will be the sequence of the record in a consecutive sequence of overlaps&amp;nbsp; (i.e. record K&amp;nbsp;overlaps&amp;nbsp;K+1 which overlaps K+3, yielding flag=1 then 2 then 3):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have (keep=id);
  by id;
  merge have
        have (firstobs=2 keep=start_date rename=(start_date=next_sd));
  if (first.id=0 and start_date&amp;lt;lag(end_date)) or 
      (end_date&amp;gt;next_sd and last.id=0) then flag+1;
  else flag=0;
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>Wed, 25 Jan 2017 16:41:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-check-date-overlap-last-visit-s-end-date-is-later-than/m-p/327439#M73049</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-01-25T16:41:37Z</dc:date>
    </item>
    <item>
      <title>Re: how to check date overlap: last visit's end date is later than this visit's start date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-check-date-overlap-last-visit-s-end-date-is-later-than/m-p/327741#M73125</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/56807"&gt;@fengyuwuzu&lt;/a&gt; wrote:&lt;BR /&gt;My dates are in yyyy-mm-dd format in the real SAS dateset, which can be compared differectly either in numeric format or character format. I did not pay attention when typing the example dates in excel.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So you show example data that is not in the form of the actual data?&lt;/P&gt;
&lt;P&gt;And while comparisons of greater than or less than may work things like "by how much to they overlap" or any kind of actual difference cannot be&amp;nbsp;done with either of those values but can be done with SAS date values.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jan 2017 15:35:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-check-date-overlap-last-visit-s-end-date-is-later-than/m-p/327741#M73125</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-01-26T15:35:49Z</dc:date>
    </item>
  </channel>
</rss>

