<?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: Difference between two datetime and add the difference to a variable. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-two-datetime-and-add-the-difference-to-a/m-p/452125#M114075</link>
    <description>&lt;P&gt;What is CURRENTDATE?&amp;nbsp; Do you mean the date when the code runs?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  Product='Chocolates';
  dtime1='01Jun2000:08:00:00'dt;
  dtime2=. ;
  format dtime: datetime20. ;
run;  
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How about this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
  if missing(dtime2) then dtime2=intnx('dtday',datetime(),0,'e');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Obs     Product                    dtime1                  dtime2
 1     Chocolates      01JUN2000:08:00:00      06APR2018:23:59:59&lt;/PRE&gt;</description>
    <pubDate>Fri, 06 Apr 2018 22:15:45 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2018-04-06T22:15:45Z</dc:date>
    <item>
      <title>Difference between two datetime and add the difference to a variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-two-datetime-and-add-the-difference-to-a/m-p/452074#M114054</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to get the difference between two date time values. I have a table which has Product, Production Start time, Endtime&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are few products which do not have the endtime, we need to update the Endtime if the endtime is missing when the report is run, it should be updated to "currentdate23:59:59"dt &lt;BR /&gt;I have tried using Intck but im not sure why i am getting incorrect value for Intck, and beacuse of this i am not able add the difference hours from the start time to the&amp;nbsp;"currentdate23:59:59"dt&lt;BR /&gt;&lt;BR /&gt;Kindly Suggest.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Santtosh...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;Product='Choclates';&lt;BR /&gt;dtime1='01Jun2000:08:00:00'dt;       
dtime2='02Jun2000:23:00:00'dt;  &lt;/CODE&gt;&lt;CODE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 19:53:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-two-datetime-and-add-the-difference-to-a/m-p/452074#M114054</guid>
      <dc:creator>Santt0sh</dc:creator>
      <dc:date>2018-04-06T19:53:37Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between two datetime and add the difference to a variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-two-datetime-and-add-the-difference-to-a/m-p/452084#M114057</link>
      <description>&lt;P&gt;Please post the code you've tried so far, the INTCK in particular that you say is not working.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 19:52:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-two-datetime-and-add-the-difference-to-a/m-p/452084#M114057</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-04-06T19:52:55Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between two datetime and add the difference to a variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-two-datetime-and-add-the-difference-to-a/m-p/452085#M114058</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/183704"&gt;@Santt0sh&lt;/a&gt;&amp;nbsp;I tried to clean up your question and I think it better reflects what you're asking, but please review it to make sure it's still what you're trying say.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/183704"&gt;@Santt0sh&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to get the difference between two date time values. I have a table which has Product, Production Start time, Endtime&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are few products which do not have the endtime, we need to update the Endtime if the endtime is missing when the report is run, it should be updated to "currentdate23:59:59"dt &lt;BR /&gt;I have tried using Intck but im not sure why i am getting incorrect value for Intck, and beacuse of this i am not able add the difference hours from the start time to the&amp;nbsp;"currentdate23:59:59"dt&lt;BR /&gt;&lt;BR /&gt;Kindly Suggest.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Santtosh...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;Product='Choclates';&lt;BR /&gt;dtime1='01Jun2000:08:00:00'dt;       
dtime2='02Jun2000:23:00:00'dt;  &lt;/CODE&gt;&lt;CODE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 19:54:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-two-datetime-and-add-the-difference-to-a/m-p/452085#M114058</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-04-06T19:54:20Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between two datetime and add the difference to a variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-two-datetime-and-add-the-difference-to-a/m-p/452099#M114061</link>
      <description>&lt;P&gt;Hi Reeza,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please find the below code i have tried to calculate the difference in two ways.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Starttime="05Apr2018:02:41:35"dt;&lt;/P&gt;&lt;P&gt;Endtime="05Apr2018:23:59:59"dt;&lt;/P&gt;&lt;P&gt;Int=Intck('hour',Endtime,Starttime); option 1&lt;/P&gt;&lt;P&gt;T=Round(endtime-starttime)/3600); option 2&lt;/P&gt;&lt;P&gt;Tx=Intnx('hour,Starttime,T);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;OUTPUT&lt;/P&gt;&lt;P&gt;format TX datetime23.;&lt;/P&gt;&lt;P&gt;Tx=05APR2018:23:00:00&lt;/P&gt;&lt;P&gt;T=15&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kindly Suggest.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 20:30:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-two-datetime-and-add-the-difference-to-a/m-p/452099#M114061</guid>
      <dc:creator>Santt0sh</dc:creator>
      <dc:date>2018-04-06T20:30:02Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between two datetime and add the difference to a variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-two-datetime-and-add-the-difference-to-a/m-p/452104#M114064</link>
      <description>&lt;P&gt;You have date time variables so your time intervals need DT to indicate datetime&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ie DTHOUR, not HOUR. Try that.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 20:50:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-two-datetime-and-add-the-difference-to-a/m-p/452104#M114064</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-04-06T20:50:59Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between two datetime and add the difference to a variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-two-datetime-and-add-the-difference-to-a/m-p/452111#M114067</link>
      <description>Hi Reeza,&lt;BR /&gt;&lt;BR /&gt;Thank you for your suggestions,&lt;BR /&gt;I have tried applying DTHOUR instead of HOUR for both INTCK and INTNX, unfortnately i am not able to get what i want to do.&lt;BR /&gt;&lt;BR /&gt;The resullts are unchanged.&lt;BR /&gt;Starttime="05Apr2018:02:41:35"dt;&lt;BR /&gt;&lt;BR /&gt;Endtime="05Apr2018:23:59:59"dt;&lt;BR /&gt;&lt;BR /&gt;Int=Intck('DThour',Endtime,Starttime); option 1&lt;BR /&gt;&lt;BR /&gt;T=Round(endtime-starttime)/3600); option 2&lt;BR /&gt;&lt;BR /&gt;Tx=Intnx('DThour',Starttime,T);&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;OUTPUT&lt;BR /&gt;&lt;BR /&gt;format TX datetime23.;&lt;BR /&gt;&lt;BR /&gt;Tx=05APR2018:23:00:00&lt;BR /&gt;&lt;BR /&gt;T=15&lt;BR /&gt;</description>
      <pubDate>Fri, 06 Apr 2018 21:10:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-two-datetime-and-add-the-difference-to-a/m-p/452111#M114067</guid>
      <dc:creator>Santt0sh</dc:creator>
      <dc:date>2018-04-06T21:10:32Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between two datetime and add the difference to a variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-two-datetime-and-add-the-difference-to-a/m-p/452114#M114068</link>
      <description>&lt;P&gt;Is this what you are looking for?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data work.junk;
   Starttime="05Apr2018:02:41:35"dt;
   endtime = intnx('dtday',starttime,0,'E');
   format Starttime endtime datetime20.;
   
run;&lt;/PRE&gt;
&lt;P&gt;this advances the datetime value to the end of the day for that date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 21:21:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-two-datetime-and-add-the-difference-to-a/m-p/452114#M114068</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-04-06T21:21:02Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between two datetime and add the difference to a variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-two-datetime-and-add-the-difference-to-a/m-p/452123#M114074</link>
      <description>&lt;P&gt;TIME and DATETIME are both stored in seconds so HOURS are the same intervals.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 22:09:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-two-datetime-and-add-the-difference-to-a/m-p/452123#M114074</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-04-06T22:09:45Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between two datetime and add the difference to a variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-two-datetime-and-add-the-difference-to-a/m-p/452125#M114075</link>
      <description>&lt;P&gt;What is CURRENTDATE?&amp;nbsp; Do you mean the date when the code runs?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  Product='Chocolates';
  dtime1='01Jun2000:08:00:00'dt;
  dtime2=. ;
  format dtime: datetime20. ;
run;  
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How about this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
  if missing(dtime2) then dtime2=intnx('dtday',datetime(),0,'e');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Obs     Product                    dtime1                  dtime2
 1     Chocolates      01JUN2000:08:00:00      06APR2018:23:59:59&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Apr 2018 22:15:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-two-datetime-and-add-the-difference-to-a/m-p/452125#M114075</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-04-06T22:15:45Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between two datetime and add the difference to a variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-two-datetime-and-add-the-difference-to-a/m-p/452127#M114076</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/183704"&gt;@Santt0sh&lt;/a&gt; wrote:&lt;BR /&gt;Hi Reeza,&lt;BR /&gt;Thank you for your suggestions,&lt;BR /&gt;I have tried applying DTHOUR instead of HOUR for both INTCK and INTNX, unfortnately i am not able to get what i want to do.&lt;BR /&gt;The resullts are unchanged.&lt;BR /&gt;Starttime="05Apr2018:02:41:35"dt;&lt;BR /&gt;Endtime="05Apr2018:23:59:59"dt;&lt;BR /&gt;Int=Intck('DThour',Endtime,Starttime); option 1&lt;BR /&gt;T=Round(endtime-starttime)/3600); option 2&lt;BR /&gt;Tx=Intnx('DThour',Starttime,T);&lt;BR /&gt;OUTPUT&lt;BR /&gt;format TX datetime23.;&lt;BR /&gt;Tx=05APR2018:23:00:00&lt;BR /&gt;T=15&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;What are you trying to count.&amp;nbsp; The INTCK() function is for calculating number of boundaries crossed. The INTNX() function calculates a new date/time/datetime value.&lt;/P&gt;
&lt;P&gt;The difference between two datetime values will be in seconds. You could attach a TIME format to it if you want.&lt;/P&gt;
&lt;P&gt;To convert to hours then divide by the number of seconds in an hour.&amp;nbsp; Perhaps you want to round the number of hours?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
Starttime="05Apr2018:02:41:35"dt;
Endtime="05Apr2018:23:59:59"dt;
format _all_ datetime20. ;
seconds = endtime-starttime ;
duration = seconds ;
hours = round(seconds/'01:00't,1);
format seconds comma12. duration hhmm12. hours comma5. ;
put (_all_) (=/);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Starttime=05APR2018:02:41:35
Endtime=05APR2018:23:59:59
seconds=76,704
duration=21:18
hours=21
&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Apr 2018 22:27:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-two-datetime-and-add-the-difference-to-a/m-p/452127#M114076</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-04-06T22:27:46Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between two datetime and add the difference to a variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-two-datetime-and-add-the-difference-to-a/m-p/452154#M114084</link>
      <description>&lt;P&gt;You apparently expected TX to be the same as starttime.&amp;nbsp; But because you used the INTNX function, with DTHOUR as the unit to add/subtract, the function has to have a rule for assigning which part of the dthour the resulting datetime value will get.&amp;nbsp; The default behavior is the &lt;EM&gt;&lt;STRONG&gt;beginning&lt;/STRONG&gt;&lt;/EM&gt; of the resulting DTHOUR, i.e.&amp;nbsp;you got:&lt;/P&gt;
&lt;PRE&gt; Tx=05APR2018:23:00:00
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if you want the result to have the same relative location within the DTHOUR, you should&amp;nbsp;populate the 4th argument of INTNX with the keyword "SAME", as in:&lt;/P&gt;
&lt;PRE&gt;Tx=Intnx('DThour',Starttime,T,'same');
&lt;/PRE&gt;
&lt;P&gt;which yields:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;PRE&gt;tx=05APR2018:23:41:35
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead of "SAME", you could have "BEGIN" (the default) or "END" (which would yield 05APR2018:23:59:59),&amp;nbsp; or you could use "S","B", or "E".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the INTCK function, there is&amp;nbsp;also a pair of arguments to deal with the analogous problem of specifying a user-desired alignment of the DTHOUR boundaries.&amp;nbsp; They are 'DISCRETE' (the default) and 'CONTINUOUS' (or "D" and "C").&amp;nbsp; If you use "C", then the DTHOUR boundary is not the normal boundary (i.e. on the hour), but rather the boundary is determined by&amp;nbsp;the 2nd argument.&amp;nbsp; I.e. for INTCK('dthour',endtime,starttime,'C') the&amp;nbsp;boundary crossings&amp;nbsp;to count is on the 59th second of the 59th minute of each hour, because ENDTIME="05Apr2018:23:59:59"dt.&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 07 Apr 2018 02:36:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-two-datetime-and-add-the-difference-to-a/m-p/452154#M114084</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-04-07T02:36:02Z</dc:date>
    </item>
  </channel>
</rss>

