<?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: Add a variable value after comparing dates and ranges in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Add-a-variable-value-after-comparing-dates-and-ranges/m-p/955293#M42919</link>
    <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;In other words, if the visit (DB1) occurs in the range Range1-Range2 of DB for each patient (ID) then it will be reported in DB to obtain DB2.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't understand what this means. Can you walk us through the logic, step-by-step?&lt;/P&gt;</description>
    <pubDate>Tue, 07 Jan 2025 10:00:57 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2025-01-07T10:00:57Z</dc:date>
    <item>
      <title>Add a variable value after comparing dates and ranges</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Add-a-variable-value-after-comparing-dates-and-ranges/m-p/955292#M42918</link>
      <description>&lt;P&gt;Hi guys,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;suppose to have the following dataset:&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. Range1 :date09. Range2 :date09.; 
  format Admission date9. Discharge date9. Range1 date9. Range2 date9.;
cards;
0001 13JAN2017 25JAN2017 30DEC2016 24FEB2017 
0001 22FEB2017 07MAR2017 08FEB2017 06APR2017
0001 27APR2017 16MAY2017 13APR2017 15JUN2017
0001 30JAN2019 04MAR2019 16JAN2019 03APR2019
0002 28DEC2014 03JAN2015 14DEC2014 02FEB2015
0002 03MAR2015 12MAR2015 20FEB2015 02APR2015
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and a second dataset:&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. Visit_date :date09. Visit; 
  format Visit_date date9. ;
cards;
0001 26JAN2017 1 
0001 10FEB2017 1 
0002 22FEB2015 4
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is there a way to obtain 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 DB2;
  input ID :$20. Admission :date09. Discharge :date09. Range1 :date09. Range2 :date09. Visit; 
  format Admission date9. Discharge date9. Range1 date9. Range2 date9.;
cards;
0001 13JAN2017 25JAN2017 30DEC2016 24FEB2017  1
0001 22FEB2017 07MAR2017 08FEB2017 06APR2017  1
0001 27APR2017 16MAY2017 13APR2017 15JUN2017  0
0001 30JAN2019 04MAR2019 16JAN2019 03APR2019  0
0002 28DEC2014 03JAN2015 14DEC2014 02FEB2015  0
0002 03MAR2015 12MAR2015 20FEB2015 02APR2015  4
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In other words, if the visit_date (DB1) occurs in the range Range1-Range2 of DB for each patient (ID) then it will be reported in DB to obtain DB2.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Logic: 1) check if the visit_date in DB1 occurs between Range1 and Range2 of DB for each ID&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2) if the visit_date in DB1 occurs between&amp;nbsp; Range1 and Range2 in DB for each ID, then create DB2. DB2 is as DB + the visit value ("visit" variable of DB1) where it occurs (i.e., at the corresponding range)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jan 2025 11:35:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Add-a-variable-value-after-comparing-dates-and-ranges/m-p/955292#M42918</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2025-01-07T11:35:34Z</dc:date>
    </item>
    <item>
      <title>Re: Add a variable value after comparing dates and ranges</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Add-a-variable-value-after-comparing-dates-and-ranges/m-p/955293#M42919</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;In other words, if the visit (DB1) occurs in the range Range1-Range2 of DB for each patient (ID) then it will be reported in DB to obtain DB2.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't understand what this means. Can you walk us through the logic, step-by-step?&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jan 2025 10:00:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Add-a-variable-value-after-comparing-dates-and-ranges/m-p/955293#M42919</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-01-07T10:00:57Z</dc:date>
    </item>
    <item>
      <title>Re: Add a variable value after comparing dates and ranges</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Add-a-variable-value-after-comparing-dates-and-ranges/m-p/955295#M42920</link>
      <description>Just edited</description>
      <pubDate>Tue, 07 Jan 2025 10:20:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Add-a-variable-value-after-comparing-dates-and-ranges/m-p/955295#M42920</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2025-01-07T10:20:25Z</dc:date>
    </item>
    <item>
      <title>Re: Add a variable value after comparing dates and ranges</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Add-a-variable-value-after-comparing-dates-and-ranges/m-p/955296#M42921</link>
      <description>&lt;P&gt;I edited the &lt;STRONG&gt;DB1&lt;/STRONG&gt; data a bit. I assume you want Visit_date to have the date9 format (not Visit).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is how I interpret your logic: For each observation in &lt;STRONG&gt;DB&amp;nbsp;&lt;/STRONG&gt;you want to evaluate each Visit_date in &lt;STRONG&gt;DB1&amp;nbsp;&lt;/STRONG&gt;. If Visit_date is within the range of Range1 and Range2 for &lt;EM&gt;&lt;STRONG&gt;any&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt;observation in &lt;STRONG&gt;DB1&lt;/STRONG&gt;, set Visit = 1. If not, set Visit = 0.&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. Range1 :date09. Range2 :date09.; 
  format Admission date9. Discharge date9. Range1 date9. Range2 date9.;
cards;
0001 13JAN2017 25JAN2017 30DEC2016 24FEB2017 
0001 22FEB2017 07MAR2017 08FEB2017 06APR2017
0001 27APR2017 16MAY2017 13APR2017 15JUN2017
0001 30JAN2019 04MAR2019 16JAN2019 03APR2019
0002 28DEC2014 03JAN2015 14DEC2014 02FEB2015
0002 03MAR2015 12MAR2015 20FEB2015 02APR2015
;

data DB1;
  input ID :$20. Visit_date :date09.; 
  format Visit_date date09.;
cards;
0001 26JAN2017 
0001 10FEB2017 
0002 22FEB2015 
;

data DB2(drop = Visit_date);

   if _N_ = 1 then do;
      dcl hash h(dataset : 'DB1', multidata : 'Y');
      h.definekey('ID');
      h.definedata('Visit_date');
      h.definedone();
   end;

   set DB;

   Visit_date = .;
   Visit = 0;

   do while (h.do_over() = 0);
      if Range1 &amp;lt;= Visit_date &amp;lt;= Range2 then Visit = 1;
   end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Result:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ID    Admission  Discharge  Range1     Range2     Visit
0001  13JAN2017  25JAN2017  30DEC2016  24FEB2017  1
0001  22FEB2017  07MAR2017  08FEB2017  06APR2017  1
0001  27APR2017  16MAY2017  13APR2017  15JUN2017  0
0001  30JAN2019  04MAR2019  16JAN2019  03APR2019  0
0002  28DEC2014  03JAN2015  14DEC2014  02FEB2015  0
0002  03MAR2015  12MAR2015  20FEB2015  02APR2015  1&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jan 2025 11:08:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Add-a-variable-value-after-comparing-dates-and-ranges/m-p/955296#M42921</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2025-01-07T11:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: Add a variable value after comparing dates and ranges</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Add-a-variable-value-after-comparing-dates-and-ranges/m-p/955301#M42922</link>
      <description>Thank you very much for your help. It is not exactly what I need. First "for any observation in DB1" is not completely true because it should run ID per ID. DB1 reports visits ID per ID. 2)  "set Visit = 1. If not, set Visit = 0" not True. Instead, set the value Visit takes in DB1. It could take 2 or a value different from 1. I will edit my post to make it more clear.</description>
      <pubDate>Tue, 07 Jan 2025 11:35:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Add-a-variable-value-after-comparing-dates-and-ranges/m-p/955301#M42922</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2025-01-07T11:35:08Z</dc:date>
    </item>
    <item>
      <title>Re: Add a variable value after comparing dates and ranges</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Add-a-variable-value-after-comparing-dates-and-ranges/m-p/955314#M42923</link>
      <description>&lt;P&gt;OK, I am also pretty confused by this - I think what you're asking for is quite simple, but I could be mistaken.&amp;nbsp; One thing I don't understand -- is the value of 'Visit' in DB1 supposed to be a *count* of visits (e.g., 4 visits occurred on this date)?&amp;nbsp; I was initially thinking it was more like a visit identifier (e.g., a person's 1st visit, 2nd visit, etc.), but then I see you have Visit=1 for two separate dates for person 0001.&amp;nbsp; In any case, seems like this is just a simple SQL join:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select a.*, max(0, b.visit) as Visit
from 
    DB A
    left join
    DB1 B
    on a.patient=b.patient and a.range1&amp;lt;=b.visitdate&amp;lt;=a.range2
order by a.patient, a.admission;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Depending on what the variable 'Visit' means (see question above) it may make more sense to set Visit to missing in DB2 when there is no such visit in the corresponding range.&amp;nbsp; In that case, you'd just remove the ' else 0' part above.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What happens if there is more than one visitdate in a given range?&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jan 2025 13:51:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Add-a-variable-value-after-comparing-dates-and-ranges/m-p/955314#M42923</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-01-07T13:51:09Z</dc:date>
    </item>
    <item>
      <title>Re: Add a variable value after comparing dates and ranges</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Add-a-variable-value-after-comparing-dates-and-ranges/m-p/955320#M42924</link>
      <description>^^ just edited the above SQL - the join was wrong.</description>
      <pubDate>Tue, 07 Jan 2025 13:51:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Add-a-variable-value-after-comparing-dates-and-ranges/m-p/955320#M42924</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-01-07T13:51:44Z</dc:date>
    </item>
    <item>
      <title>Re: Add a variable value after comparing dates and ranges</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Add-a-variable-value-after-comparing-dates-and-ranges/m-p/955321#M42925</link>
      <description>Thank you very much for your comment and help. 1) "One thing I don't understand -- is the value of 'Visit' in DB1 supposed to be a *count* of visits (e.g., 4 visits occurred on this date)? " Yes it is. Only a count nothing more. 2) "What happens if there is more than one visit_date in a given range?" No cases like this in the data.</description>
      <pubDate>Tue, 07 Jan 2025 13:53:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Add-a-variable-value-after-comparing-dates-and-ranges/m-p/955321#M42925</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2025-01-07T13:53:11Z</dc:date>
    </item>
  </channel>
</rss>

