<?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: Update records of one file based on another file in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Update-records-of-one-file-based-on-another-file/m-p/832676#M35710</link>
    <description>&lt;P&gt;I changed the date variables from character to numeric SAS dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Feel free to ask.&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. (Start End)(:date9.) Hospital :$20.;
format Start End date9.;
cards;
0001 01JAN2015 06FEB2016 C 
0001 07FEB2016 25APR2016 C 
0001 26APR2016 31DEC2019 D 
0002 01JAN2017 12JUL2017 C 
0002 13JUL2017 31DEC2019 D 
0002 01JAN2020 31DEC2020 D 
;

data DB2;
input ID :$20. (Start End)(:date9.);
format Start End date9.;
cards;
0001 28JAN2016 08FEB2016 
0001 24APR2016 28APR2016 
0001 28NOV2019 13DEC2019 
0002 11JUL2017 14JUL2017 
0002 15JUL2017 01OCT2019 
0002 14AUG2020 13SEP2020 
;

data DB11(drop = s);
   set DB1;
   by ID Hospital notsorted;
   if first.Hospital then s = Start;
   if last.Hospital then do;
      Start = s;
      output;
   end;
   retain s;
run;

data want(keep = ID Start End Hospital);

   if _N_ = 1 then do;
      dcl hash h(dataset : 'DB11(rename = (start = s end = e))', multidata : 'Y');
      h.definekey('ID');
      h.definedata('Hospital', 's', 'e');
      h.definedone();
   end;

   set DB2;

   if 0 then set DB1(rename = (start = s end = e));
   call missing(s, e, hospital);

   do while (h.do_over() = 0);

      if d = 1 then start = s ;   

      if s &amp;lt;= Start &amp;lt;= e &amp;amp; End &amp;gt; e then do;
         d = 1;
         _end = end;
         end = e;
         output;
         end = _end;
      end;

      else do;
         d = 0;
         if s &amp;lt;= Start &amp;lt;= e then output;
      end;
      
   end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Result:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ID    Start      End        Hospital
0001  28JAN2016  08FEB2016  C
0001  24APR2016  25APR2016  C
0001  26APR2016  28APR2016  D
0001  28NOV2019  13DEC2019  D
0002  11JUL2017  12JUL2017  C
0002  13JUL2017  14JUL2017  D
0002  15JUL2017  01OCT2019  D
0002  14AUG2020  13SEP2020  D&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 10 Sep 2022 19:09:00 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2022-09-10T19:09:00Z</dc:date>
    <item>
      <title>Update records of one file based on another file</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Update-records-of-one-file-based-on-another-file/m-p/832673#M35709</link>
      <description>&lt;P&gt;Hi guys,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;suppose to have the following:&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. Start :$20. End :$20. Hospital :$20.;
     cards;
     0001 01JAN2015  06FEB2016  C
     0001 07FEB2016  25APR2016  C
     0001 26APR2016  31DEC2019  D
     0002 01JAN2017  12JUL2017  C
     0002 13JUL2017  31DEC2019  D
     0002 01JAN2020  31DEC2020  D
         ;

data DB2;
     input ID :$20. Start :$20. End :$20.;
     cards;
     0001 28JAN2016  08FEB2016
     0001 24APR2016  28APR2016
     0001 28NOV2019  13DEC2019
     0002 11JUL2017  14JUL2017 
     0002 15JUL2017  01OCT2019
     0002 14AUG2020  13SEP2020
         ;

expected: 


data DB3;
     input ID :$20. Start :$20. End :$20. Hospital :$20.;
     cards;
     0001 28JAN2016  08FEB2016  C
     0001 24APR2016  25APR2016  C 
     0001 26APR2016  28APR2019  D
     0001 28NOV2019  13DEC2019  D
     0002 11JUL2017  12JUL2017  C
     0002 13JUL2017  14JUL2017  D
     0002 15JUL2017  01OCT2019  D
     0002 14AUG2020  13SEP2020  D
         ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="s-prose js-post-body"&gt;
&lt;P&gt;In other words I would like to add the Hospital information to DB2 to obtain DB3 based on the range of its periods on DB1. When the period of DB2 is in between the hospital change of DB1 the period of DB2 would be splitted as shown in the output to take track of the hospital change. Can anyone help me please? thank you in advance.&lt;/P&gt;
&lt;/DIV&gt;
&lt;DIV class="mt24 mb12"&gt;
&lt;DIV class="post-taglist d-flex gs4 gsy fd-column"&gt;
&lt;DIV class="d-flex ps-relative fw-wrap"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Sat, 10 Sep 2022 18:38:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Update-records-of-one-file-based-on-another-file/m-p/832673#M35709</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2022-09-10T18:38:12Z</dc:date>
    </item>
    <item>
      <title>Re: Update records of one file based on another file</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Update-records-of-one-file-based-on-another-file/m-p/832676#M35710</link>
      <description>&lt;P&gt;I changed the date variables from character to numeric SAS dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Feel free to ask.&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. (Start End)(:date9.) Hospital :$20.;
format Start End date9.;
cards;
0001 01JAN2015 06FEB2016 C 
0001 07FEB2016 25APR2016 C 
0001 26APR2016 31DEC2019 D 
0002 01JAN2017 12JUL2017 C 
0002 13JUL2017 31DEC2019 D 
0002 01JAN2020 31DEC2020 D 
;

data DB2;
input ID :$20. (Start End)(:date9.);
format Start End date9.;
cards;
0001 28JAN2016 08FEB2016 
0001 24APR2016 28APR2016 
0001 28NOV2019 13DEC2019 
0002 11JUL2017 14JUL2017 
0002 15JUL2017 01OCT2019 
0002 14AUG2020 13SEP2020 
;

data DB11(drop = s);
   set DB1;
   by ID Hospital notsorted;
   if first.Hospital then s = Start;
   if last.Hospital then do;
      Start = s;
      output;
   end;
   retain s;
run;

data want(keep = ID Start End Hospital);

   if _N_ = 1 then do;
      dcl hash h(dataset : 'DB11(rename = (start = s end = e))', multidata : 'Y');
      h.definekey('ID');
      h.definedata('Hospital', 's', 'e');
      h.definedone();
   end;

   set DB2;

   if 0 then set DB1(rename = (start = s end = e));
   call missing(s, e, hospital);

   do while (h.do_over() = 0);

      if d = 1 then start = s ;   

      if s &amp;lt;= Start &amp;lt;= e &amp;amp; End &amp;gt; e then do;
         d = 1;
         _end = end;
         end = e;
         output;
         end = _end;
      end;

      else do;
         d = 0;
         if s &amp;lt;= Start &amp;lt;= e then output;
      end;
      
   end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Result:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ID    Start      End        Hospital
0001  28JAN2016  08FEB2016  C
0001  24APR2016  25APR2016  C
0001  26APR2016  28APR2016  D
0001  28NOV2019  13DEC2019  D
0002  11JUL2017  12JUL2017  C
0002  13JUL2017  14JUL2017  D
0002  15JUL2017  01OCT2019  D
0002  14AUG2020  13SEP2020  D&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 10 Sep 2022 19:09:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Update-records-of-one-file-based-on-another-file/m-p/832676#M35710</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-09-10T19:09:00Z</dc:date>
    </item>
    <item>
      <title>Re: Update records of one file based on another file</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Update-records-of-one-file-based-on-another-file/m-p/832732#M35716</link>
      <description>Thank you very much!</description>
      <pubDate>Sun, 11 Sep 2022 08:54:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Update-records-of-one-file-based-on-another-file/m-p/832732#M35716</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2022-09-11T08:54:40Z</dc:date>
    </item>
    <item>
      <title>Re: Update records of one file based on another file</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Update-records-of-one-file-based-on-another-file/m-p/832745#M35717</link>
      <description>&lt;P&gt;Anytime &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 11 Sep 2022 11:06:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Update-records-of-one-file-based-on-another-file/m-p/832745#M35717</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-09-11T11:06:45Z</dc:date>
    </item>
  </channel>
</rss>

