<?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: Derive the Last non-consecutive missing date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Derive-the-Last-non-consecutive-missing-date/m-p/682772#M206701</link>
    <description>&lt;P&gt;Higly appreciated for your help. However, I found maybe you missed the 'consecutive' missing, for 1002, the adt should be 1998-02-01 (of avisit=1) as for the avisit=2, 3, 4, and 5 DAT are missing consecutively for this subject. Now 1003 the value is what I want to have.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you furtherly help me. Million thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Jack2012_0-1599683138465.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/49193i1D4B98607018AEB8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Jack2012_0-1599683138465.png" alt="Jack2012_0-1599683138465.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 09 Sep 2020 20:25:59 GMT</pubDate>
    <dc:creator>Jack2012</dc:creator>
    <dc:date>2020-09-09T20:25:59Z</dc:date>
    <item>
      <title>Derive the Last non-consecutive missing date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Derive-the-Last-non-consecutive-missing-date/m-p/682593#M206623</link>
      <description>&lt;P&gt;Dear All, I have one very simple question:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Q1: How could below dataset be input successfully?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the data I have, which is created by below code:&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data have;&lt;BR /&gt;input subjid avisit dat $20.;&lt;BR /&gt;datalines;&lt;BR /&gt;1001 0 1997-01-01&lt;BR /&gt;1001 1 1997-02-01&lt;BR /&gt;1001 2 &lt;BR /&gt;1001 3 1997-05-01&lt;BR /&gt;1002 0 1998-01-01&lt;BR /&gt;1002 1 1998-02-01&lt;BR /&gt;1002 2 &lt;BR /&gt;1002 3 &lt;BR /&gt;1002 4&lt;BR /&gt;1002 5&lt;BR /&gt;1002 6 1998-12-01&lt;BR /&gt;1003 0 1999-01-01&lt;BR /&gt;1003 1 1999-02-01&lt;BR /&gt;1003 2 &lt;BR /&gt;1003 3 1999-03-08&lt;BR /&gt;1003 4&lt;BR /&gt;1003 5 1999-04-05&lt;BR /&gt;1003 6 1999-05-01&lt;BR /&gt;1003 7 &lt;BR /&gt;1003 8&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data have_;&lt;BR /&gt;set have;&lt;BR /&gt;adt=input(dat, yymmdd10.);&lt;BR /&gt;attrib adt format=yymmdd10.;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Jack2012_0-1599652991246.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/49167i5759B96F26B6A3E4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Jack2012_0-1599652991246.png" alt="Jack2012_0-1599652991246.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Q2: If you look at the last variable adt generated by below dataset WANT (which, unfortunately I can't produce by below code successfully, please assist me as well), you will see actually, I want to derive the last non-missing dat which should prior the consecutive dat, but I have no idea how to derive the last column, base on the first 3 variable correctly, hope you can help me.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data want;&lt;BR /&gt;infile datalines delimiter=",";&lt;BR /&gt;input subjid avisit dat $20. adt $20.;&lt;BR /&gt;datalines;&lt;BR /&gt;1001,0,1997-01-01,1997-05-01&lt;BR /&gt;1001,1,1997-02-01,1997-05-01&lt;BR /&gt;1001,2, ,1997-05-01&lt;BR /&gt;1001,3,1997-05-01,1997-05-01&lt;BR /&gt;1002,0,1998-01-01,1998-02-01&lt;BR /&gt;1002,1,1998-02-01,1998-02-01&lt;BR /&gt;1002,2, ,1998-02-01&lt;BR /&gt;1002,3, ,1998-02-01&lt;BR /&gt;1002,4, ,1998-02-01&lt;BR /&gt;1002,5, ,1998-02-01&lt;BR /&gt;1002,6,1998-12-01,1998-02-01&lt;BR /&gt;1003,0,1999-01-01,1999-05-01 &lt;BR /&gt;1003,1,1999-02-01,1999-05-01&lt;BR /&gt;1003,2, ,1999-05-01&lt;BR /&gt;1003,3,1999-03-08,1999-05-01&lt;BR /&gt;1003,4, ,1999-05-01&lt;BR /&gt;1003,5,1999-04-05,1999-05-01&lt;BR /&gt;1003,6,1999-05-01,1999-05-01&lt;BR /&gt;1003,7, ,1999-05-01&lt;BR /&gt;1003,8, ,1999-05-01&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Sep 2020 12:05:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Derive-the-Last-non-consecutive-missing-date/m-p/682593#M206623</guid>
      <dc:creator>Jack2012</dc:creator>
      <dc:date>2020-09-09T12:05:47Z</dc:date>
    </item>
    <item>
      <title>Re: Derive the Last non-consecutive missing date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Derive-the-Last-non-consecutive-missing-date/m-p/682605#M206628</link>
      <description>&lt;PRE&gt;data have;
infile datalines truncover;
input subjid avisit dat : yymmdd10.;
format dat yymmdd10.;
datalines;
1001 0 1997-01-01
1001 1 1997-02-01
1001 2
1001 3 1997-05-01
1002 0 1998-01-01
1002 1 1998-02-01
1002 2
1002 3
1002 4
1002 5
1002 6 1998-12-01
1003 0 1999-01-01
1003 1 1999-02-01
1003 2
1003 3 1999-03-08
1003 4
1003 5 1999-04-05
1003 6 1999-05-01
1003 7
1003 8
;
data temp;
 set have(where=(dat is not missing));
 by subjid;
 if last.subjid;
run;
data want;
 merge have temp(rename=(dat=new_dat) drop=avisit);
 by subjid;
run;
data want2;
 set want;
 if missing(dat) then call missing(new_dat);
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Sep 2020 12:29:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Derive-the-Last-non-consecutive-missing-date/m-p/682605#M206628</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-09-09T12:29:12Z</dc:date>
    </item>
    <item>
      <title>Re: Derive the Last non-consecutive missing date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Derive-the-Last-non-consecutive-missing-date/m-p/682623#M206631</link>
      <description>&lt;P&gt;Thank you, actually, in your algorithm to derive the last variable, there is bit problem in below case, I just modify the 1003 subject, and the last variable value turned to the one I did not want.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;infile datalines truncover;&lt;BR /&gt;input subjid avisit dat : yymmdd10.;&lt;BR /&gt;format dat yymmdd10.;&lt;BR /&gt;datalines;&lt;BR /&gt;1001 0 1997-01-01&lt;BR /&gt;1001 1 1997-02-01&lt;BR /&gt;1001 2&lt;BR /&gt;1001 3 1997-05-01&lt;BR /&gt;1002 0 1998-01-01&lt;BR /&gt;1002 1 1998-02-01&lt;BR /&gt;1002 2&lt;BR /&gt;1002 3&lt;BR /&gt;1002 4&lt;BR /&gt;1002 5&lt;BR /&gt;1002 6 1998-12-01&lt;BR /&gt;1003 0 1999-01-01&lt;BR /&gt;1003 1 1999-02-01&lt;BR /&gt;1003 2&lt;BR /&gt;1003 3 1999-03-08&lt;BR /&gt;1003 4&lt;BR /&gt;1003 5 &lt;BR /&gt;1003 6 1999-05-01&lt;BR /&gt;1003 7&lt;BR /&gt;1003 8&lt;BR /&gt;;&lt;BR /&gt;data temp;&lt;BR /&gt;set have(where=(dat is not missing));&lt;BR /&gt;by subjid;&lt;BR /&gt;if last.subjid;&lt;BR /&gt;run;&lt;BR /&gt;data want;&lt;BR /&gt;merge have temp(rename=(dat=new_dat) drop=avisit);&lt;BR /&gt;by subjid;&lt;BR /&gt;run;&lt;BR /&gt;data want2;&lt;BR /&gt;set want;&lt;BR /&gt;if missing(dat) then call missing(new_dat);&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Jack2012_0-1599657081703.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/49169i301B17E580674F75/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Jack2012_0-1599657081703.png" alt="Jack2012_0-1599657081703.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The right value I am looking for is 1999-03-08 for 1003, how should I change the code? Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 09 Sep 2020 13:12:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Derive-the-Last-non-consecutive-missing-date/m-p/682623#M206631</guid>
      <dc:creator>Jack2012</dc:creator>
      <dc:date>2020-09-09T13:12:09Z</dc:date>
    </item>
    <item>
      <title>Re: Derive the Last non-consecutive missing date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Derive-the-Last-non-consecutive-missing-date/m-p/682724#M206679</link>
      <description>&lt;P&gt;&lt;EM&gt;"The right value I am looking for is 1999-03-08 for 1003"&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What's the rule?&lt;/P&gt;</description>
      <pubDate>Wed, 09 Sep 2020 17:42:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Derive-the-Last-non-consecutive-missing-date/m-p/682724#M206679</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-09-09T17:42:00Z</dc:date>
    </item>
    <item>
      <title>Re: Derive the Last non-consecutive missing date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Derive-the-Last-non-consecutive-missing-date/m-p/682731#M206684</link>
      <description>&lt;P&gt;Hi, The rule is that if there is consecutive missing (missing consecutively for two more more times for each subjid) then the last non-missing DAT will be chosen as the value of ADT.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Sep 2020 18:31:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Derive-the-Last-non-consecutive-missing-date/m-p/682731#M206684</guid>
      <dc:creator>Jack2012</dc:creator>
      <dc:date>2020-09-09T18:31:55Z</dc:date>
    </item>
    <item>
      <title>Re: Derive the Last non-consecutive missing date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Derive-the-Last-non-consecutive-missing-date/m-p/682737#M206688</link>
      <description>I mean the last non-missing DAT will be chosen as the value of ADT for this SUBJID.</description>
      <pubDate>Wed, 09 Sep 2020 19:11:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Derive-the-Last-non-consecutive-missing-date/m-p/682737#M206688</guid>
      <dc:creator>Jack2012</dc:creator>
      <dc:date>2020-09-09T19:11:19Z</dc:date>
    </item>
    <item>
      <title>Re: Derive the Last non-consecutive missing date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Derive-the-Last-non-consecutive-missing-date/m-p/682762#M206700</link>
      <description>&lt;P&gt;This implements the rule: take the first dat value that follows a missing dat value :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do until(last.subjid);
    set have; by subjid;
    if missing(dat) then gap = 1;
    else if gap and missing(val) then val = dat;
    end;
do until(last.subjid);
    set have; by subjid;
    if not missing(dat) 
        then adt = val;
        else call missing(adt);
    output;
    end;
drop gap val;
format adt yymmdd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Sep 2020 20:05:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Derive-the-Last-non-consecutive-missing-date/m-p/682762#M206700</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-09-09T20:05:38Z</dc:date>
    </item>
    <item>
      <title>Re: Derive the Last non-consecutive missing date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Derive-the-Last-non-consecutive-missing-date/m-p/682772#M206701</link>
      <description>&lt;P&gt;Higly appreciated for your help. However, I found maybe you missed the 'consecutive' missing, for 1002, the adt should be 1998-02-01 (of avisit=1) as for the avisit=2, 3, 4, and 5 DAT are missing consecutively for this subject. Now 1003 the value is what I want to have.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you furtherly help me. Million thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Jack2012_0-1599683138465.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/49193i1D4B98607018AEB8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Jack2012_0-1599683138465.png" alt="Jack2012_0-1599683138465.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Sep 2020 20:25:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Derive-the-Last-non-consecutive-missing-date/m-p/682772#M206701</guid>
      <dc:creator>Jack2012</dc:creator>
      <dc:date>2020-09-09T20:25:59Z</dc:date>
    </item>
    <item>
      <title>Re: Derive the Last non-consecutive missing date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Derive-the-Last-non-consecutive-missing-date/m-p/682786#M206708</link>
      <description>&lt;P&gt;Ok then. The rule seems to be: take the first dat value that immediately precedes a gap of size 2 or more; assume there is such a gap at the end of the series. Try this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do until(last.subjid);
    set have; by subjid;
    if gap &amp;lt; 2 then
        if missing(dat) then gap = sum(1, gap);
        else val = dat;
    end;
do until(last.subjid);
    set have; by subjid;
    if not missing(dat) 
        then adt = val;
        else call missing(adt);
    output;
    end;
drop gap val;
format adt yymmdd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Sep 2020 22:06:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Derive-the-Last-non-consecutive-missing-date/m-p/682786#M206708</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-09-09T22:06:03Z</dc:date>
    </item>
    <item>
      <title>Re: Derive the Last non-consecutive missing date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Derive-the-Last-non-consecutive-missing-date/m-p/682900#M206752</link>
      <description>&lt;PRE&gt;data have;
infile datalines truncover;
input subjid avisit dat : yymmdd10.;
format dat yymmdd10.;
datalines;
1001 0 1997-01-01
1001 1 1997-02-01
1001 2
1001 3 1997-05-01
1002 0 1998-01-01
1002 1 1998-02-01
1002 2
1002 3
1002 4
1002 5
1002 6 1998-12-01
1003 0 1999-01-01
1003 1 1999-02-01
1003 2
1003 3 1999-03-08
1003 4
1003 5
1003 6 1999-05-01
1003 7
1003 8
;

data have1;
 set have;
 by subjid;
 retain found 0;
 if first.subjid then do;n=0;found=0;end;
 if missing(dat) then n+1;
  else n=0;
 if n=2 then found=1;
 if not found;
 drop n found;
run;

data temp;
set have1(where=(dat is not missing));
by subjid;
if last.subjid;
run;
data want;
merge have temp(rename=(dat=new_dat) drop=avisit);
by subjid;
run;
data want2;
set want;
if missing(dat) then call missing(new_dat);
run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 10 Sep 2020 11:19:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Derive-the-Last-non-consecutive-missing-date/m-p/682900#M206752</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-09-10T11:19:48Z</dc:date>
    </item>
    <item>
      <title>Re: Derive the Last non-consecutive missing date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Derive-the-Last-non-consecutive-missing-date/m-p/683161#M206894</link>
      <description>&lt;P&gt;Thank you very much, Sharp. Resolved perfectly.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Sep 2020 04:56:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Derive-the-Last-non-consecutive-missing-date/m-p/683161#M206894</guid>
      <dc:creator>Jack2012</dc:creator>
      <dc:date>2020-09-11T04:56:46Z</dc:date>
    </item>
  </channel>
</rss>

