<?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: create a new date variable conditional on other dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/create-a-new-date-variable-conditional-on-other-dates/m-p/309492#M66598</link>
    <description>&lt;P&gt;Thank you for posting but you code does not produce the required output&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 05 Nov 2016 10:35:45 GMT</pubDate>
    <dc:creator>lillymaginta</dc:creator>
    <dc:date>2016-11-05T10:35:45Z</dc:date>
    <item>
      <title>create a new date variable conditional on other dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-a-new-date-variable-conditional-on-other-dates/m-p/309369#M66530</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;start date  drug end_Date 
01/01/2005 a     02/01/2005
02/02/2005 a    03/01/2005
03/08/2005  a      04/08/2005
06/01/2005  a     07/01/2005&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to create a dataset were I would have one start date and end date based on a gap of no longer than 7 days between the end of the first period and the start of the second period. For the example above, I would merge all the first three and ignore the four&lt;/P&gt;&lt;P&gt;output&amp;nbsp;&lt;/P&gt;&lt;P&gt;start date &amp;nbsp;drug end_Date&lt;/P&gt;&lt;P&gt;01/01/2005 &amp;nbsp; &amp;nbsp;a &amp;nbsp; &amp;nbsp; 04/08/2005&lt;/P&gt;</description>
      <pubDate>Fri, 04 Nov 2016 18:04:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-a-new-date-variable-conditional-on-other-dates/m-p/309369#M66530</guid>
      <dc:creator>lillymaginta</dc:creator>
      <dc:date>2016-11-04T18:04:38Z</dc:date>
    </item>
    <item>
      <title>Re: create a new date variable conditional on other dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-a-new-date-variable-conditional-on-other-dates/m-p/309379#M66533</link>
      <description>&lt;P&gt;data one;&lt;BR /&gt;informat start_date end_date mmddyy.;&lt;BR /&gt;input start_date drug $1. end_Date ;&lt;BR /&gt;datalines;&lt;BR /&gt;01/01/2005 a 02/01/2005&lt;BR /&gt;02/02/2005 a 03/01/2005&lt;BR /&gt;03/08/2005 a 04/08/2005&lt;BR /&gt;06/01/2005 a 07/01/2005&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;proc sort;&lt;BR /&gt;by drug start_date end_date;&lt;BR /&gt;run;&lt;BR /&gt;data two;&lt;BR /&gt;set one;&lt;BR /&gt;by drug;&lt;BR /&gt;if first.drug then newstartdate=start_date;&lt;BR /&gt;if last.drug then newenddate=end_date;&lt;BR /&gt;proc print;&lt;BR /&gt;run;&lt;BR /&gt;data _start;&lt;BR /&gt;set two;&lt;BR /&gt;if newstartdate ne .;&lt;BR /&gt;data _end;&lt;BR /&gt;set two;&lt;BR /&gt;if newenddate ne .;&lt;BR /&gt;run;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table join1 as select&lt;BR /&gt;s.drug, s.newstartdate, e.newenddate from _start as s&lt;BR /&gt;join _end as e&lt;BR /&gt;on s.drug=e.drug;&lt;BR /&gt;quit;&lt;BR /&gt;proc print;&lt;BR /&gt;format newstartdate newenddate mmddyy.;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Nov 2016 19:05:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-a-new-date-variable-conditional-on-other-dates/m-p/309379#M66533</guid>
      <dc:creator>GreggB</dc:creator>
      <dc:date>2016-11-04T19:05:37Z</dc:date>
    </item>
    <item>
      <title>Re: create a new date variable conditional on other dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-a-new-date-variable-conditional-on-other-dates/m-p/309439#M66556</link>
      <description>&lt;P&gt;Hi Thank you for the reply. Your code merge all the days together and does not produce the output above. The last entry for the date should not be counted because the end_Date for the period before and the start date of the second period have a gap of more than 7 days. so if the gap is more than 7 days, record the end date for the one before which is 04/08/2005. Can you please adjust your code to account for that?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Nov 2016 22:08:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-a-new-date-variable-conditional-on-other-dates/m-p/309439#M66556</guid>
      <dc:creator>lillymaginta</dc:creator>
      <dc:date>2016-11-04T22:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: create a new date variable conditional on other dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-a-new-date-variable-conditional-on-other-dates/m-p/309469#M66580</link>
      <description>&lt;P&gt;data have;&lt;/P&gt;
&lt;P&gt;infile cards;&lt;/P&gt;
&lt;P&gt;input start_date ddmmyy10. drug $1. end_date ddmmyy10.;&lt;/P&gt;
&lt;P&gt;cards;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;start_&lt;SPAN class="token function"&gt;date&lt;/SPAN&gt; drug end_Date 
&lt;SPAN class="token number"&gt;01&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;01&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;2005&lt;/SPAN&gt; a &lt;SPAN class="token number"&gt;02&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;01&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;2005&lt;/SPAN&gt;
&lt;SPAN class="token number"&gt;02&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;02&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;2005&lt;/SPAN&gt; a 0&lt;SPAN class="token number"&gt;3&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;01&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;2005&lt;/SPAN&gt;
&lt;SPAN class="token number"&gt;03&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;08&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;2005&lt;/SPAN&gt; a &lt;SPAN class="token number"&gt;04&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;08&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;2005&lt;/SPAN&gt;
&lt;SPAN class="token number"&gt;06&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;01&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;2005&lt;/SPAN&gt; a &lt;SPAN class="token number"&gt;07&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;01&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;2005&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;; run;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=have; by drug start_date; run;&lt;/P&gt;
&lt;P&gt;data want(rename = (date1 = start_date date2 = end_date));&lt;/P&gt;
&lt;P&gt;&amp;nbsp; set have; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; by drug;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;retain date1 date2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if first.drug the do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; date1 = start_date;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; date2 = end_date;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; end;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if start_date = date2 + 1 then date2 = end_date;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else do;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; output;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; date1 = start_date;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; date2 = end_date;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; end;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if last.drug then output;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; keep date1 date2;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have not run my code. In case of any difficulty please post the log, to help you more.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 05 Nov 2016 05:17:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-a-new-date-variable-conditional-on-other-dates/m-p/309469#M66580</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-11-05T05:17:18Z</dc:date>
    </item>
    <item>
      <title>Re: create a new date variable conditional on other dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-a-new-date-variable-conditional-on-other-dates/m-p/309481#M66590</link>
      <description>&lt;PRE&gt;

data one;
informat start_date end_date mmddyy.;
input start_date drug $1. end_Date ;
datalines;
01/01/2005 a 02/01/2005
02/02/2005 a 03/01/2005
03/08/2005 a 04/08/2005
06/01/2005 a 07/01/2005
;
run;
data temp;
 set one;
 by drug;
 if first.drug then group=0;
 if first.drug or start_date-lag(end_date) gt 7 then group+1;
 if group=1;
run;
data want;
 set temp;
 by drug;
 retain start;
 if first.drug then start=start_date;
 if last.drug then output;
 drop group start_date;
 format start end_date date9.;
run;
  
 

&lt;/PRE&gt;</description>
      <pubDate>Sat, 05 Nov 2016 08:53:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-a-new-date-variable-conditional-on-other-dates/m-p/309481#M66590</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-11-05T08:53:22Z</dc:date>
    </item>
    <item>
      <title>Re: create a new date variable conditional on other dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-a-new-date-variable-conditional-on-other-dates/m-p/309492#M66598</link>
      <description>&lt;P&gt;Thank you for posting but you code does not produce the required output&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 05 Nov 2016 10:35:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-a-new-date-variable-conditional-on-other-dates/m-p/309492#M66598</guid>
      <dc:creator>lillymaginta</dc:creator>
      <dc:date>2016-11-05T10:35:45Z</dc:date>
    </item>
    <item>
      <title>Re: create a new date variable conditional on other dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-a-new-date-variable-conditional-on-other-dates/m-p/309493#M66599</link>
      <description>&lt;P&gt;Thank you Ksharp, always a life saver!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 05 Nov 2016 10:36:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-a-new-date-variable-conditional-on-other-dates/m-p/309493#M66599</guid>
      <dc:creator>lillymaginta</dc:creator>
      <dc:date>2016-11-05T10:36:33Z</dc:date>
    </item>
    <item>
      <title>Re: create a new date variable conditional on other dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-a-new-date-variable-conditional-on-other-dates/m-p/309525#M66621</link>
      <description>if you have multiple ids, and you want to run the above code and generate the above output at the same time for multiple ids, how do you adjust your code for that?</description>
      <pubDate>Sat, 05 Nov 2016 19:48:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-a-new-date-variable-conditional-on-other-dates/m-p/309525#M66621</guid>
      <dc:creator>lillymaginta</dc:creator>
      <dc:date>2016-11-05T19:48:06Z</dc:date>
    </item>
    <item>
      <title>Re: create a new date variable conditional on other dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-a-new-date-variable-conditional-on-other-dates/m-p/309526#M66622</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
informat start_date end_date mmddyy.;
input id start_date drug $1. end_Date ;
datalines;
1  01/01/2005 a 02/01/2005
1  02/02/2005 a 03/01/2005
1  03/08/2005 a 04/08/2005
1  06/01/2005 a 07/01/2005
2  01/01/2005 a 02/01/2005
2  02/02/2005 a 03/01/2005
2  03/08/2005 a 04/08/2005
2 06/01/2005 a 07/01/2005
3  01/01/2005 a 02/02/2005


;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;also if the patient has one period with no other start date within 7 days (such as in patient 3), the code should assign that period as the start and end date for the output.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 05 Nov 2016 19:51:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-a-new-date-variable-conditional-on-other-dates/m-p/309526#M66622</guid>
      <dc:creator>lillymaginta</dc:creator>
      <dc:date>2016-11-05T19:51:24Z</dc:date>
    </item>
    <item>
      <title>Re: create a new date variable conditional on other dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-a-new-date-variable-conditional-on-other-dates/m-p/309567#M66643</link>
      <description>&lt;PRE&gt;

data one;
informat start_date end_date mmddyy.;
input id start_date drug $1. end_Date ;
datalines;
1  01/01/2005 a 02/01/2005
1  02/02/2005 a 03/01/2005
1  03/08/2005 a 04/08/2005
1  06/01/2005 a 07/01/2005
2  01/01/2005 a 02/01/2005
2  02/02/2005 a 03/01/2005
2  03/08/2005 a 04/08/2005
2 06/01/2005 a 07/01/2005
3  01/01/2005 a 02/02/2005
;
run;


data temp;
 set one;
 by drug id;
 if first.id then group=0;
 if first.id or start_date-lag(end_date) gt 7 then group+1;
 if group=1;
run;
data want;
 set temp;
 by drug id;
 retain start;
 if first.id then start=start_date;
 if last.id then output;
 drop group start_date;
 format start end_date date9.;
run;
  
&lt;/PRE&gt;</description>
      <pubDate>Sun, 06 Nov 2016 04:37:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-a-new-date-variable-conditional-on-other-dates/m-p/309567#M66643</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-11-06T04:37:09Z</dc:date>
    </item>
  </channel>
</rss>

