<?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: Extracting ID with consecutive dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extracting-ID-with-consecutive-dates/m-p/440464#M282519</link>
    <description>&lt;P&gt;Something like (assumes sorted):&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  retain lst_v;
  if _n_=1 then lst_v=visit_date;
  else do;
    if year(visit_date)=year(lst_v)+1 then output;
  end;
  lst_v=visit_date;
run;
proc sort data=want nodupkey;
  by patient_id;
run;&lt;/PRE&gt;</description>
    <pubDate>Tue, 27 Feb 2018 11:42:33 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2018-02-27T11:42:33Z</dc:date>
    <item>
      <title>Extracting ID with consecutive dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-ID-with-consecutive-dates/m-p/440460#M282518</link>
      <description>&lt;P&gt;Hi everyone&lt;/P&gt;
&lt;P&gt;I have a dataset that looks like this:&lt;/P&gt;
&lt;TABLE width="248"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="87"&gt;Patient_ID&lt;/TD&gt;
&lt;TD width="161"&gt;Visit_Date&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;13-08-2016&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;28-05-2013&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;24-12-2013&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt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;5&lt;/TD&gt;
&lt;TD&gt;08-01-2015&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;5&lt;/TD&gt;
&lt;TD&gt;21-09-2012&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What i want to do is to extract a list of patients who have been seen in any 2 consecutive&amp;nbsp; years.&lt;/P&gt;
&lt;P&gt;Could anyone&amp;nbsp;help please?&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Feb 2018 11:30:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-ID-with-consecutive-dates/m-p/440460#M282518</guid>
      <dc:creator>ammarhm</dc:creator>
      <dc:date>2018-02-27T11:30:25Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting ID with consecutive dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-ID-with-consecutive-dates/m-p/440464#M282519</link>
      <description>&lt;P&gt;Something like (assumes sorted):&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  retain lst_v;
  if _n_=1 then lst_v=visit_date;
  else do;
    if year(visit_date)=year(lst_v)+1 then output;
  end;
  lst_v=visit_date;
run;
proc sort data=want nodupkey;
  by patient_id;
run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Feb 2018 11:42:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-ID-with-consecutive-dates/m-p/440464#M282519</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-02-27T11:42:33Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting ID with consecutive dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-ID-with-consecutive-dates/m-p/440485#M282520</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs truncover;
input Patient_ID	Visit_Date : ddmmyy12.;
year=year(visit_date);
format visit_date ddmmyy10.;
cards;
1	13-08-2016
1	28-05-2013
1	24-12-2013
2	23-05-2012
2	11-01-2014
2	03-10-2017
2	16-02-2015
3	03-08-2013
3	28-10-2017
3	27-10-2013
3	18-04-2015
4	28-04-2016
4	07-08-2012
4	19-03-2013
4	25-05-2017
5	21-11-2014
5	20-03-2016
5	08-01-2015
5	21-09-2012
;
proc sort data=have out=temp nodupkey;
by Patient_ID year;
run;
data temp;
 set temp;
 by Patient_ID;
 dif=dif(year);
 if first.Patient_ID then call missing(dif);
run;
proc sql;
select distinct Patient_ID
 from temp
  group by Patient_ID
   having sum(dif=1) ne 0;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Feb 2018 12:49:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-ID-with-consecutive-dates/m-p/440485#M282520</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-02-27T12:49:35Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting ID with consecutive dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-ID-with-consecutive-dates/m-p/440490#M282521</link>
      <description>&lt;P&gt;You don't really tell us how the desired result should look like - especially for cases where you have two visits in year 1 and two visits in year 2.&lt;/P&gt;
&lt;P&gt;Below one way to go:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile cards expandtabs truncover;
  input Patient_ID  Visit_Date : ddmmyy12.;
  year=year(visit_date);
  format visit_date ddmmyy10.;
  cards;
1 13-08-2016
1 28-05-2013
1 24-12-2013
2 23-05-2012
2 11-01-2014
2 03-10-2017
2 16-02-2015
3 03-08-2013
3 28-10-2017
3 27-10-2013
3 18-04-2015
4 28-04-2016
4 07-08-2012
4 19-03-2013
4 25-05-2017
5 21-11-2014
5 20-03-2016
5 08-01-2015
5 21-09-2012
;

proc sql;
  create table want as
    select l.*, r.visit_date as next_years_date format=ddmmyy10.
    from have as l inner join have as r
      on 
        l.patient_id=r.patient_id 
        and intck('year',l.Visit_Date,r.Visit_Date)=1
  ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Feb 2018 13:01:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-ID-with-consecutive-dates/m-p/440490#M282521</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-02-27T13:01:16Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting ID with consecutive dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-ID-with-consecutive-dates/m-p/440762#M282522</link>
      <description>&lt;P&gt;I suggest a merge of HAVE with itself, in which one of the merged datasteams starts with firstobs=2.&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 (keep=patient year1 year2);
  set have (keep=patient);
  by patient;

  merge have
        have (firstobs=2 keep=date rename=(date=nxt_date));
  year1=year(date);
  year2=year(nxt_date);

  if last.patient=0 and year2=year1+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This writes out one record for each instance of two consecutive records belonging to 2 consecutive years.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Feb 2018 03:55:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-ID-with-consecutive-dates/m-p/440762#M282522</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-02-28T03:55:10Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting ID with consecutive dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-ID-with-consecutive-dates/m-p/440804#M282523</link>
      <description>Patrick's approach is the way to go especially if the input could sit in memory.</description>
      <pubDate>Wed, 28 Feb 2018 08:57:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-ID-with-consecutive-dates/m-p/440804#M282523</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2018-02-28T08:57:21Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting ID with consecutive dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-ID-with-consecutive-dates/m-p/440867#M282524</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  used=0; /* this ID has not been output yet */
  do until(last.id);
    set have;
    by id;
    if used then continue;
    if dif(year(visit_date)) ne 1 then continue;
    if first.id then continue;
    used=1;
    output;
    end;
  keep id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 28 Feb 2018 12:56:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-ID-with-consecutive-dates/m-p/440867#M282524</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-02-28T12:56:43Z</dc:date>
    </item>
  </channel>
</rss>

