<?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 line up the dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-line-up-the-dates/m-p/290438#M60116</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/97768"&gt;@aninda_metal﻿&lt;/a&gt;&lt;/P&gt;&lt;P&gt;Like&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9﻿&lt;/a&gt;, I think your expected output is wrong. I&amp;nbsp;&amp;nbsp;can't understand why your expected new data doesn't have 7 rows and&amp;nbsp;include a row with new_stdt &amp;amp; new_end=29-Dec-11&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PTID &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new_stdt &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; new_end&lt;/P&gt;&lt;P&gt;P3747440 &amp;nbsp; &amp;nbsp;2-Nov-11 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2-Nov-11&lt;BR /&gt;P3747440 &amp;nbsp; &amp;nbsp;3-Nov-11 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;30-Nov-11&lt;BR /&gt;P3747440 &amp;nbsp; &amp;nbsp;1-Dec-11 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1-Dec-11&lt;BR /&gt;P3747440 &amp;nbsp; &amp;nbsp;2-Dec-11 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;FONT color="#FF0000"&gt;28-Dec-11&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;P3747440 &amp;nbsp; &amp;nbsp;&lt;FONT color="#FF0000"&gt;29-Dec-11 &amp;nbsp; &amp;nbsp; &amp;nbsp;29-Dec-11&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;P3747440 &amp;nbsp; &amp;nbsp;30-Dec-11 &amp;nbsp; &amp;nbsp; &amp;nbsp;30-Dec-11&lt;BR /&gt;P3747440 &amp;nbsp; &amp;nbsp;31-Dec-11 &amp;nbsp; &amp;nbsp; &amp;nbsp;31-Dec-11&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think basically you are taking all your uniques dates regardless of whether it's a start or end date, and then using those to create a new start_date. The corresponding end date is then equal to the next start date -1. Except for the last date, where it looks like you want new_end=new_stdt&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In my attempt to code this I've reversed the date order because i'm comparing to the next date. It starts with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9﻿&lt;/a&gt;'s code as far as the INTER data table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  informat new_stdt new_end date9.;
  input ptid $ new_stdt new_end;
  format new_stdt new_end date9.;
datalines;
P3747440   02Nov2011     29Dec2011
P3747440   03Nov2011     02Dec2011
P3747440   01Dec2011    30Dec2011
P3747440   30Dec2011    31Dec2011
;
run;

data inter;
  set have (keep=ptid new_stdt) have (keep=ptid new_end rename=(new_end=new_stdt));
run;&lt;BR /&gt;&lt;BR /&gt;proc sort data=inter nodup;&lt;BR /&gt; by ptid descending new_stdt;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data want (keep= ptid new_stdt new_end);&lt;BR /&gt;format new_end date9.;&lt;BR /&gt;set inter;&lt;BR /&gt;retain lagdate ;&lt;BR /&gt;by ptid;&lt;BR /&gt;diff=lagdate-new_stdt;&lt;BR /&gt;if lagdate-new_stdt in (.,1) then new_end=new_stdt;else new_end=lagdate-1;&lt;BR /&gt;output;&lt;BR /&gt;if last.ptid then lagdate=. ;else lagdate=new_stdt ;&lt;BR /&gt;run;&lt;BR /&gt;proc sort data=want; by ptid new_stdt;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;</description>
    <pubDate>Tue, 09 Aug 2016 12:41:14 GMT</pubDate>
    <dc:creator>JohnHoughton</dc:creator>
    <dc:date>2016-08-09T12:41:14Z</dc:date>
    <item>
      <title>How to line up the dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-line-up-the-dates/m-p/290174#M60018</link>
      <description>&lt;P&gt;I have a data set like below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PTID &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new_stdt &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; new_end&lt;/P&gt;&lt;P&gt;P3747440 &amp;nbsp; 02Nov2011 &amp;nbsp; &amp;nbsp; 29Dec2011&lt;BR /&gt;P3747440 &amp;nbsp; 03Nov2011 &amp;nbsp; &amp;nbsp; 02Dec2011&lt;BR /&gt;P3747440 &amp;nbsp; 01Dec2011 &amp;nbsp; &amp;nbsp;30Dec2011&lt;BR /&gt;P3747440 &amp;nbsp; 30Dec2011 &amp;nbsp; &amp;nbsp;31Dec2011&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to line up the dates .So that my new data looks like below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PTID &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new_stdt &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; new_end&lt;/P&gt;&lt;P&gt;P3747440 &amp;nbsp; &amp;nbsp;2-Nov-11 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2-Nov-11&lt;BR /&gt;P3747440 &amp;nbsp; &amp;nbsp;3-Nov-11 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;30-Nov-11&lt;BR /&gt;P3747440 &amp;nbsp; &amp;nbsp;1-Dec-11 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1-Dec-11&lt;BR /&gt;P3747440 &amp;nbsp; &amp;nbsp;2-Dec-11 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;29-Dec-11&lt;BR /&gt;P3747440 &amp;nbsp; &amp;nbsp;30-Dec-11 &amp;nbsp; &amp;nbsp; &amp;nbsp;30-Dec-11&lt;BR /&gt;P3747440 &amp;nbsp; &amp;nbsp;31-Dec-11 &amp;nbsp; &amp;nbsp; &amp;nbsp;31-Dec-11&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I do this ?Any leads will be appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Aninda&lt;/P&gt;</description>
      <pubDate>Mon, 08 Aug 2016 12:57:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-line-up-the-dates/m-p/290174#M60018</guid>
      <dc:creator>aninda_metal</dc:creator>
      <dc:date>2016-08-08T12:57:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to line up the dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-line-up-the-dates/m-p/290181#M60021</link>
      <description>&lt;P&gt;What is the logic to go from have to want, I see no logical reason why row 1 would have 2nov11 and the second row would have 30nov11.&lt;/P&gt;</description>
      <pubDate>Mon, 08 Aug 2016 13:17:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-line-up-the-dates/m-p/290181#M60021</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-08-08T13:17:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to line up the dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-line-up-the-dates/m-p/290183#M60022</link>
      <description>&lt;P&gt;Hello.&lt;/P&gt;&lt;P&gt;Thanks for your reply.My objective is to make the dates consecutive with no gaps.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Aninda&lt;/P&gt;</description>
      <pubDate>Mon, 08 Aug 2016 13:25:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-line-up-the-dates/m-p/290183#M60022</guid>
      <dc:creator>aninda_metal</dc:creator>
      <dc:date>2016-08-08T13:25:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to line up the dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-line-up-the-dates/m-p/290188#M60023</link>
      <description>&lt;P&gt;Am in a meeting so can't look too much, but I think your want data may be wrong. &amp;nbsp;This code should get you near:&lt;/P&gt;
&lt;PRE&gt;data have;
  informat new_stdt new_end date9.;
  input ptid $ new_stdt new_end;
  format new_stdt new_end date9.;
datalines;
P3747440   02Nov2011     29Dec2011
P3747440   03Nov2011     02Dec2011
P3747440   01Dec2011    30Dec2011
P3747440   30Dec2011    31Dec2011
;
run;

data inter;
  set have (keep=ptid new_stdt) have (keep=ptid new_end rename=(new_end=new_stdt));
run;
proc sort data=inter;
  by ptid new_stdt;
run;
data want;
  set inter (rename=(new_stdt=stdt));
  format new_stdt new_end date9.;
  by ptid;
  retain lstd;
  if first.ptid then lstd=stdt;
  else do;
    new_stdt=lstd;
    new_end=stdt-1;
    output;
  end;
  lstd=stdt;
run;
&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 Aug 2016 13:47:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-line-up-the-dates/m-p/290188#M60023</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-08-08T13:47:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to line up the dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-line-up-the-dates/m-p/290200#M60025</link>
      <description>&lt;P&gt;Is the rule like that:&lt;/P&gt;
&lt;P&gt;a) time ranges must not overlap&lt;/P&gt;
&lt;P&gt;b) time ranges need to be split along calender months&lt;/P&gt;
&lt;P&gt;?&lt;/P&gt;</description>
      <pubDate>Mon, 08 Aug 2016 14:16:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-line-up-the-dates/m-p/290200#M60025</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-08-08T14:16:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to line up the dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-line-up-the-dates/m-p/290438#M60116</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/97768"&gt;@aninda_metal﻿&lt;/a&gt;&lt;/P&gt;&lt;P&gt;Like&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9﻿&lt;/a&gt;, I think your expected output is wrong. I&amp;nbsp;&amp;nbsp;can't understand why your expected new data doesn't have 7 rows and&amp;nbsp;include a row with new_stdt &amp;amp; new_end=29-Dec-11&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PTID &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new_stdt &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; new_end&lt;/P&gt;&lt;P&gt;P3747440 &amp;nbsp; &amp;nbsp;2-Nov-11 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2-Nov-11&lt;BR /&gt;P3747440 &amp;nbsp; &amp;nbsp;3-Nov-11 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;30-Nov-11&lt;BR /&gt;P3747440 &amp;nbsp; &amp;nbsp;1-Dec-11 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1-Dec-11&lt;BR /&gt;P3747440 &amp;nbsp; &amp;nbsp;2-Dec-11 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;FONT color="#FF0000"&gt;28-Dec-11&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;P3747440 &amp;nbsp; &amp;nbsp;&lt;FONT color="#FF0000"&gt;29-Dec-11 &amp;nbsp; &amp;nbsp; &amp;nbsp;29-Dec-11&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;P3747440 &amp;nbsp; &amp;nbsp;30-Dec-11 &amp;nbsp; &amp;nbsp; &amp;nbsp;30-Dec-11&lt;BR /&gt;P3747440 &amp;nbsp; &amp;nbsp;31-Dec-11 &amp;nbsp; &amp;nbsp; &amp;nbsp;31-Dec-11&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think basically you are taking all your uniques dates regardless of whether it's a start or end date, and then using those to create a new start_date. The corresponding end date is then equal to the next start date -1. Except for the last date, where it looks like you want new_end=new_stdt&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In my attempt to code this I've reversed the date order because i'm comparing to the next date. It starts with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9﻿&lt;/a&gt;'s code as far as the INTER data table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  informat new_stdt new_end date9.;
  input ptid $ new_stdt new_end;
  format new_stdt new_end date9.;
datalines;
P3747440   02Nov2011     29Dec2011
P3747440   03Nov2011     02Dec2011
P3747440   01Dec2011    30Dec2011
P3747440   30Dec2011    31Dec2011
;
run;

data inter;
  set have (keep=ptid new_stdt) have (keep=ptid new_end rename=(new_end=new_stdt));
run;&lt;BR /&gt;&lt;BR /&gt;proc sort data=inter nodup;&lt;BR /&gt; by ptid descending new_stdt;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data want (keep= ptid new_stdt new_end);&lt;BR /&gt;format new_end date9.;&lt;BR /&gt;set inter;&lt;BR /&gt;retain lagdate ;&lt;BR /&gt;by ptid;&lt;BR /&gt;diff=lagdate-new_stdt;&lt;BR /&gt;if lagdate-new_stdt in (.,1) then new_end=new_stdt;else new_end=lagdate-1;&lt;BR /&gt;output;&lt;BR /&gt;if last.ptid then lagdate=. ;else lagdate=new_stdt ;&lt;BR /&gt;run;&lt;BR /&gt;proc sort data=want; by ptid new_stdt;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;</description>
      <pubDate>Tue, 09 Aug 2016 12:41:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-line-up-the-dates/m-p/290438#M60116</guid>
      <dc:creator>JohnHoughton</dc:creator>
      <dc:date>2016-08-09T12:41:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to line up the dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-line-up-the-dates/m-p/290459#M60118</link>
      <description>&lt;P&gt;Like&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9﻿&lt;/a&gt;&amp;nbsp;, I think your expected data may be wrong. I don't see why &amp;nbsp;it doesn't look like this.&lt;/P&gt;&lt;P&gt;PTID &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new_stdt &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; new_end&lt;/P&gt;&lt;P&gt;P3747440 &amp;nbsp; &amp;nbsp;2-Nov-11 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2-Nov-11&lt;BR /&gt;P3747440 &amp;nbsp; &amp;nbsp;3-Nov-11 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;30-Nov-11&lt;BR /&gt;P3747440 &amp;nbsp; &amp;nbsp;1-Dec-11 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1-Dec-11&lt;BR /&gt;P3747440 &amp;nbsp; &amp;nbsp;2-Dec-11 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;28-Dec-11&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;P3747440 &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;29-Dec-11 &amp;nbsp; &amp;nbsp; &amp;nbsp;29&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;-Dec-11&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;P3747440 &amp;nbsp; &amp;nbsp;30-Dec-11 &amp;nbsp; &amp;nbsp; &amp;nbsp;30-Dec-11&lt;BR /&gt;P3747440 &amp;nbsp; &amp;nbsp;31-Dec-11 &amp;nbsp; &amp;nbsp; &amp;nbsp;31-Dec-11&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think you are taking every available date and creating a period between each date &amp;amp; the day before the next date.&lt;/P&gt;&lt;P&gt;Then on the last date, that is the start date and the end date.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9﻿&lt;/a&gt;code does this , but needs to include NODUP when sorting data table 'INTER' .&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also add a line right at the end of the 'DATA WANT' step&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; ...
  if last.ptid then do;
  	new_end=stdt;
  	output; 
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Aug 2016 13:39:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-line-up-the-dates/m-p/290459#M60118</guid>
      <dc:creator>JohnHoughton</dc:creator>
      <dc:date>2016-08-09T13:39:09Z</dc:date>
    </item>
  </channel>
</rss>

