<?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 Calculate new dates based on a number of days but only if there is not an overlap in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Calculate-new-dates-based-on-a-number-of-days-but-only-if-there/m-p/955341#M42928</link>
    <description>&lt;P&gt;Hi guys,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;suppose to have the following:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. Admission :date09. Discharge :date09.; 
  format Admission date9. Discharge date9.;
cards;
0001 13JAN2017 25JAN2017 
0001 27JAN2017 07MAR2017 
0001 11MAR2017 16MAY2017 
0001 30JAN2019 04MAR2019 
0002 11SEP2014 15SEP2014 
0002 28DEC2014 03JAN2015 
0002 05JAN2015 12MAR2015 
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now I want to calculate -14 days from Admission and +30days from Discharge but: if the days discharge-next admission is &amp;lt; 44 days then insert a missing.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The desired output is the following:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data DB1;
  input ID :$20. Admission :date09. Discharge :date09. Limit1 :date09. Limit2 :date09.; 
  format Admission date9. Discharge date9. Limit1 date9. Limit2 date9.;
cards;
0001 13JAN2017 25JAN2017  30DEC2016     .
0001 27JAN2017 07MAR2017      .         .  
0001 11MAR2017 16MAY2017      .     15JUN2017 
0001 30JAN2019 04MAR2019  16JAN2019 03APR2019
0002 11SEP2014 15SEP2014  28AUG2014 15OCT2014
0002 28DEC2014 03JAN2015  14DEC2014     .
0002 05JAN2015 12MAR2015      .     11APR2015
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For example, for ID 0001 since there are only 2 days between 25JAN2017 and 27JAN2017 if I count 25JAN+30 or 27JAN -14 some overlap will occur. In this (and similar) case because of this overlap a missing should be inserted meaning that no considerations can be done.&amp;nbsp; For ID= 0001 this happens for 3 sets of adm-disch (first 3 rows) but this is completely variable. In some cases there can be 5, 6 or 2 sets of dates that are hospitalizations that occur closely.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone help me please?&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 07 Jan 2025 17:36:27 GMT</pubDate>
    <dc:creator>NewUsrStat</dc:creator>
    <dc:date>2025-01-07T17:36:27Z</dc:date>
    <item>
      <title>Calculate new dates based on a number of days but only if there is not an overlap</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Calculate-new-dates-based-on-a-number-of-days-but-only-if-there/m-p/955341#M42928</link>
      <description>&lt;P&gt;Hi guys,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;suppose to have the following:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. Admission :date09. Discharge :date09.; 
  format Admission date9. Discharge date9.;
cards;
0001 13JAN2017 25JAN2017 
0001 27JAN2017 07MAR2017 
0001 11MAR2017 16MAY2017 
0001 30JAN2019 04MAR2019 
0002 11SEP2014 15SEP2014 
0002 28DEC2014 03JAN2015 
0002 05JAN2015 12MAR2015 
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now I want to calculate -14 days from Admission and +30days from Discharge but: if the days discharge-next admission is &amp;lt; 44 days then insert a missing.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The desired output is the following:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data DB1;
  input ID :$20. Admission :date09. Discharge :date09. Limit1 :date09. Limit2 :date09.; 
  format Admission date9. Discharge date9. Limit1 date9. Limit2 date9.;
cards;
0001 13JAN2017 25JAN2017  30DEC2016     .
0001 27JAN2017 07MAR2017      .         .  
0001 11MAR2017 16MAY2017      .     15JUN2017 
0001 30JAN2019 04MAR2019  16JAN2019 03APR2019
0002 11SEP2014 15SEP2014  28AUG2014 15OCT2014
0002 28DEC2014 03JAN2015  14DEC2014     .
0002 05JAN2015 12MAR2015      .     11APR2015
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For example, for ID 0001 since there are only 2 days between 25JAN2017 and 27JAN2017 if I count 25JAN+30 or 27JAN -14 some overlap will occur. In this (and similar) case because of this overlap a missing should be inserted meaning that no considerations can be done.&amp;nbsp; For ID= 0001 this happens for 3 sets of adm-disch (first 3 rows) but this is completely variable. In some cases there can be 5, 6 or 2 sets of dates that are hospitalizations that occur closely.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone help me please?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jan 2025 17:36:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Calculate-new-dates-based-on-a-number-of-days-but-only-if-there/m-p/955341#M42928</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2025-01-07T17:36:27Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate new dates based on a number of days but only if there is not an overlap</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Calculate-new-dates-based-on-a-number-of-days-but-only-if-there/m-p/955350#M42929</link>
      <description>&lt;P&gt;Use LAG() to find the previous discharge.&lt;/P&gt;
&lt;P&gt;Finding the next admission is harder (it is much easier to remember the past than to predict the future.)&amp;nbsp; Here is a simple method using the FIRSTOBS= dataset option to re-read the dataset offset by one.&amp;nbsp; Make sure to not misuse the PREV and NEXT values on the FIRST and LAST observation for a particular ID value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. Admission :date. Discharge :date.; 
  format Admission Discharge date9.;
cards;
0001 13JAN2017 25JAN2017 
0001 27JAN2017 07MAR2017 
0001 11MAR2017 16MAY2017 
0001 30JAN2019 04MAR2019 
0002 11SEP2014 15SEP2014 
0002 28DEC2014 03JAN2015 
0002 05JAN2015 12MAR2015 
;

data want;
  set db ;
  by id admission ;
  set db(firstobs=2 keep=admission rename=(admission=next_admission)) db(obs=1 drop=_all_);
  prev_discharge=lag(discharge);
  limit1=admission-10;
  if not first.id and limit1&amp;lt;prev_discharge then limit1=.;
  limit2=discharge+30;
  if not last.id and limit2&amp;gt;next_admission then limit2=.;
  format limit1 limit2 prev_discharge next_admission date9.;
run;

proc print;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1736273751591.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/103523iF58F9B69377D6026/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1736273751591.png" alt="Tom_0-1736273751591.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jan 2025 18:16:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Calculate-new-dates-based-on-a-number-of-days-but-only-if-there/m-p/955350#M42929</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-01-07T18:16:01Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate new dates based on a number of days but only if there is not an overlap</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Calculate-new-dates-based-on-a-number-of-days-but-only-if-there/m-p/955374#M42930</link>
      <description>&lt;P&gt;If you want to keep those NEXT/PREV variables then a simple change cleans them up.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  limit1=admission-10;
  if first.id then prev_discharge=.;
  else if limit1&amp;lt;prev_discharge then limit1=.;
  limit2=discharge+30;
  if last.id then next_admission=.;
  else if limit2&amp;gt;next_admission then limit2=.;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1736277552418.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/103534iC0560E7CD8BC8C77/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1736277552418.png" alt="Tom_0-1736277552418.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jan 2025 19:19:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Calculate-new-dates-based-on-a-number-of-days-but-only-if-there/m-p/955374#M42930</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-01-07T19:19:24Z</dc:date>
    </item>
  </channel>
</rss>

