<?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: Calculating Difference between two dates returning a negative number in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Difference-between-two-dates-returning-a-negative/m-p/313208#M68020</link>
    <description>&lt;P&gt;Try running this code..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input a_hosp_dt visit_date;
   informat a_hosp_dt visit_date mmddyy8.;
   format a_hosp_dt visit_date mmddyy8.;

   days_btwn_hosp= intck('day', a_hosp_dt,visit_date);
   days_btwn_hospx=datdif(a_hosp_dt, visit_date, 'act/act');

   datalines;
   03/14/15 03/20/15
   08/05/15 08/06/15 
   ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;It seems to be a problem with how your data is stored because your code is ok &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza﻿&lt;/a&gt;&amp;nbsp;says, just subtract the dates..&lt;/P&gt;</description>
    <pubDate>Mon, 21 Nov 2016 19:23:15 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2016-11-21T19:23:15Z</dc:date>
    <item>
      <title>Calculating Difference between two dates returning a negative number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Difference-between-two-dates-returning-a-negative/m-p/313197#M68013</link>
      <description>&lt;P&gt;I am calculating the difference between two dates using the INTCK and DATDIF functions:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;days_btwn_hosp= intck('day', a_hosp_dt,visit_date);
days_btwn_hospx=datdif(a_hosp_dt, visit_date, 'act/act');&lt;/PRE&gt;&lt;P&gt;It seems to generally work except a few are producing a negative number when the difference in dates should be positive, for example:&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/5935iAEDD5B29B842F955/image-size/original?v=v2&amp;amp;px=-1" border="0" alt="2016-11-21_14-01-18.png" title="2016-11-21_14-01-18.png" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone know why this would happen? And how to fix it?&lt;/P&gt;</description>
      <pubDate>Mon, 21 Nov 2016 19:03:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Difference-between-two-dates-returning-a-negative/m-p/313197#M68013</guid>
      <dc:creator>einstein</dc:creator>
      <dc:date>2016-11-21T19:03:50Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Difference between two dates returning a negative number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Difference-between-two-dates-returning-a-negative/m-p/313207#M68019</link>
      <description>&lt;P&gt;Just subtract the dates.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Want = visit_date - hosp_date;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Nov 2016 19:22:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Difference-between-two-dates-returning-a-negative/m-p/313207#M68019</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-11-21T19:22:10Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Difference between two dates returning a negative number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Difference-between-two-dates-returning-a-negative/m-p/313208#M68020</link>
      <description>&lt;P&gt;Try running this code..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input a_hosp_dt visit_date;
   informat a_hosp_dt visit_date mmddyy8.;
   format a_hosp_dt visit_date mmddyy8.;

   days_btwn_hosp= intck('day', a_hosp_dt,visit_date);
   days_btwn_hospx=datdif(a_hosp_dt, visit_date, 'act/act');

   datalines;
   03/14/15 03/20/15
   08/05/15 08/06/15 
   ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;It seems to be a problem with how your data is stored because your code is ok &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza﻿&lt;/a&gt;&amp;nbsp;says, just subtract the dates..&lt;/P&gt;</description>
      <pubDate>Mon, 21 Nov 2016 19:23:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Difference-between-two-dates-returning-a-negative/m-p/313208#M68020</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2016-11-21T19:23:15Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Difference between two dates returning a negative number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Difference-between-two-dates-returning-a-negative/m-p/313221#M68028</link>
      <description>&lt;P&gt;I wonder if you have datetime instead of data variable?&lt;/P&gt;</description>
      <pubDate>Mon, 21 Nov 2016 19:37:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Difference-between-two-dates-returning-a-negative/m-p/313221#M68028</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-11-21T19:37:12Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Difference between two dates returning a negative number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Difference-between-two-dates-returning-a-negative/m-p/313226#M68029</link>
      <description>&lt;P&gt;I tried subtracting the dates and still get a negative number for just a few. &amp;nbsp;I also tried changing the formats to&amp;nbsp;mmddyy8. but that doesn't seem to change anything. &amp;nbsp;What's odd is that this is only happening for a few cases, the rest are&amp;nbsp;calculating correctly.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Nov 2016 19:52:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Difference-between-two-dates-returning-a-negative/m-p/313226#M68029</guid>
      <dc:creator>einstein</dc:creator>
      <dc:date>2016-11-21T19:52:24Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Difference between two dates returning a negative number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Difference-between-two-dates-returning-a-negative/m-p/313234#M68032</link>
      <description>&lt;P&gt;Strip the formats and show the data for the records that are negative.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something like the following, post a portion of the log:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data check;&lt;/P&gt;
&lt;P&gt;set want;&lt;/P&gt;
&lt;P&gt;Where diff &amp;lt; 0;&lt;/P&gt;
&lt;P&gt;format vist_date hospitaldate;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;put 'Visit Date:' visit_date;&lt;/P&gt;
&lt;P&gt;put 'Hospital Date:' hospital_date;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Nov 2016 20:17:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Difference-between-two-dates-returning-a-negative/m-p/313234#M68032</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-11-21T20:17:23Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Difference between two dates returning a negative number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Difference-between-two-dates-returning-a-negative/m-p/313238#M68033</link>
      <description>&lt;P&gt;Format has nothing to do with the value, just how the value is displayed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Order of appearance in INTCK is important.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   x = intck('day', '01JAN2016'd, '20JAN2016'd);
   y = intck('day', '20JAN2016'd, '01JAN2016'd);
   put x= y=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Negative values indicate the the first parameter is larger (later ) than the second.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you get this behavior for "just a few" I suggest that your initial data is flawed, likely from data entry. I would pull those records with the negative values and look for a pattern such as type of procedure. It may also be that a single admittance date is associated with multiple procedures.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am wondering if anywhere in your process you are modifying either a_hosp_date or visit_date &lt;STRONG&gt;After&lt;/STRONG&gt; calculating your days_betwen variables as the values you show are not possible from the dates shown.&lt;/P&gt;
&lt;P&gt;You might want to show the entire data step code for calculating the days_between instead of just those two lines.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Nov 2016 20:25:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Difference-between-two-dates-returning-a-negative/m-p/313238#M68033</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-11-21T20:25:49Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Difference between two dates returning a negative number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Difference-between-two-dates-returning-a-negative/m-p/313241#M68035</link>
      <description>&lt;P&gt;I just asked our analyst to resend the raw file and with the new file,&amp;nbsp;the difference in dates are correct now. &amp;nbsp;In either case, it was strange to see negative numbers and I appreciate everyones help on this!&lt;/P&gt;</description>
      <pubDate>Mon, 21 Nov 2016 20:36:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Difference-between-two-dates-returning-a-negative/m-p/313241#M68035</guid>
      <dc:creator>einstein</dc:creator>
      <dc:date>2016-11-21T20:36:41Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Difference between two dates returning a negative number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Difference-between-two-dates-returning-a-negative/m-p/313256#M68037</link>
      <description>&lt;P&gt;I have some data files from 1994 that I inherited as part of project. Some of the clinic visit "dates" were nearly 30 years in the future and we had some visits that occured 70 years before the program began. Or at least that's what was in the database extract. Sloppy data entry and entry programs that miss constraints like "patient release date cannot be before admit date".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Nov 2016 20:58:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Difference-between-two-dates-returning-a-negative/m-p/313256#M68037</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-11-21T20:58:14Z</dc:date>
    </item>
  </channel>
</rss>

