<?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 find difference between multiple dates? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-difference-between-multiple-dates/m-p/952590#M372283</link>
    <description>&lt;P&gt;Assuming I understood what you mean.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input patient_id	admit_dt : mmddyy10.;
format admit_dt  mmddyy10.;
cards;
1	1/3/2012
1	11/21/2017
1	10/2/2018
1	10/3/2018
1	11/12/2018
1	8/21/2019
1	3/13/2021
1	5/2/2021
;

proc sql;
create table want as
select *,
(select count(*) from have 
where patient_id=a.patient_id and admit_dt between a.admit_dt and intnx('year',a.admit_dt,2,'s')
) &amp;gt; 1 as admit_flag
 from have as a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 05 Dec 2024 01:23:33 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2024-12-05T01:23:33Z</dc:date>
    <item>
      <title>How to find difference between multiple dates?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-difference-between-multiple-dates/m-p/952586#M372281</link>
      <description>&lt;P&gt;I have the following data that lists each person's hospital admission dates during a pre-defined study period.&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;patient_id&lt;/TD&gt;&lt;TD&gt;admit_dt&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1/3/2012&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;11/21/2017&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;10/2/2018&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;10/3/2018&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;11/12/2018&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;8/21/2019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;3/13/2021&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;5/2/2021&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to find if a person has at least 2 hospital admissions within 2 years of each other. So far, I have the following code, but it only compares each admission to the earliest admission date.&lt;/P&gt;&lt;DIV&gt;&lt;PRE&gt;data data_out;
set data_in;
retain first_admit_dt;
by patient_id;
if first.patient_id then do;
first_admit_dt=admit_dt;
end;
else do;
ddiff=admit_dt-first_admit_dt;
if ddiff&amp;lt;(365*2) then admit_flag=1;
end;
format first_admit_dt mmddyy10.;
drop first_admit_dt;
run;&lt;/PRE&gt;This is the output I'm getting:&lt;/DIV&gt;&lt;DIV&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;patient_id&lt;/TD&gt;&lt;TD&gt;admit_dt&lt;/TD&gt;&lt;TD&gt;ddiff&lt;/TD&gt;&lt;TD&gt;admit_flag&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1/3/2012&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;11/21/2017&lt;/TD&gt;&lt;TD&gt;2149&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;10/2/2018&lt;/TD&gt;&lt;TD&gt;2464&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;10/3/2018&lt;/TD&gt;&lt;TD&gt;2465&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;11/12/2018&lt;/TD&gt;&lt;TD&gt;2505&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;8/21/2019&lt;/TD&gt;&lt;TD&gt;2787&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;3/13/2021&lt;/TD&gt;&lt;TD&gt;3357&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;5/2/2021&lt;/TD&gt;&lt;TD&gt;3407&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;P&gt;However, I want to see if &lt;U&gt;any&lt;/U&gt; of the admission dates are within 2 years of each other, not just compared to the earliest admission date. From the example above, 11/21/2017 is &lt;U&gt;not&lt;/U&gt; within 2 years from the first admit date of 1/3/2012. However, the admit date on 10/2/2018 &lt;U&gt;is&lt;/U&gt; within 2 years of 11/21/2017, so this person would be considered to have at least 2 admissions within 2 year of each other.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Thu, 05 Dec 2024 00:37:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-difference-between-multiple-dates/m-p/952586#M372281</guid>
      <dc:creator>raj23</dc:creator>
      <dc:date>2024-12-05T00:37:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to find difference between multiple dates?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-difference-between-multiple-dates/m-p/952590#M372283</link>
      <description>&lt;P&gt;Assuming I understood what you mean.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input patient_id	admit_dt : mmddyy10.;
format admit_dt  mmddyy10.;
cards;
1	1/3/2012
1	11/21/2017
1	10/2/2018
1	10/3/2018
1	11/12/2018
1	8/21/2019
1	3/13/2021
1	5/2/2021
;

proc sql;
create table want as
select *,
(select count(*) from have 
where patient_id=a.patient_id and admit_dt between a.admit_dt and intnx('year',a.admit_dt,2,'s')
) &amp;gt; 1 as admit_flag
 from have as a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Dec 2024 01:23:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-difference-between-multiple-dates/m-p/952590#M372283</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-12-05T01:23:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to find difference between multiple dates?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-difference-between-multiple-dates/m-p/952619#M372288</link>
      <description>&lt;P&gt;Do you want to compare each ADMIT_DT to all other admission dates, or only to earlier admission dates?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is code that creates two flags, one to indicate whether the prior admission is within two years, and one to indicate whether the next admission is within two years.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This assumes data are sorted by admit_dt within patient_id.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input patient_id	admit_dt : mmddyy10.;
format admit_dt  mmddyy10.;
cards;
1	1/3/2012
1	11/21/2017
1	10/2/2018
1	10/3/2018
1	11/12/2018
1	8/21/2019
1	3/13/2021
1	5/2/2021
;

data want (drop=nxt_:);
  set have;
  by patient_id;

  nxt_admit=.;
  if eod2=0 then set have (firstobs=2 keep=admit_dt rename=(admit_dt=nxt_admit)) end=eod2;


  prior_admit_flag=first.patient_id=0 and intnx('year',admit_dt,-2,'s')&amp;lt;=lag(admit_dt);
  later_admit_flag=last.patient_id=0  and intnx('year',admit_dt,2,'s')&amp;gt;=nxt_admit;
run;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Dec 2024 11:47:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-difference-between-multiple-dates/m-p/952619#M372288</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-12-05T11:47:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to find difference between multiple dates?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-difference-between-multiple-dates/m-p/952632#M372289</link>
      <description>&lt;P&gt;You may want more than a flag variable.&amp;nbsp; Here is code to count the number of earlier admission dates within 2 years (nprior), and the number of later admission dates within two years (nafter):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data counts (drop=_:);
  set have (in=firstpass) have (in=secondpass);
  by patient_id;

  array dates{0:50} _temporary_;

  if first.patient_id then call missing(of dates{*},_n1,_n2);

  _n1+firstpass;
  if firstpass then dates{_n1}=admit_dt;

  if secondpass;
  _n2+1;

  _cutoff=intnx('year',admit_dt,-2,'s');  
  do nprior=0 by 1 until (dates{_n2-1-nprior}&amp;lt;_cutoff);
  end;

  _cutoff=intnx('year',admit_dt,+2,'s');
  do nafter=0 by 1 until (dates{_n2+1+nafter}&amp;gt;_cutoff);
    if dates{_n2+1+nafter}=. then leave;
  end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This assumes data are sorted by admit_dt within patient_id.&amp;nbsp; Also make sure that the lower bound of the DATES array is zero, and the upper bound is at least 1 more than the maximum number of single-patient admissions in dataset HAVE.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Dec 2024 13:43:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-difference-between-multiple-dates/m-p/952632#M372289</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-12-05T13:43:36Z</dc:date>
    </item>
  </channel>
</rss>

