<?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: Need to calculate time difference from character format in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-to-calculate-time-difference-from-character-format/m-p/779670#M248367</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	start_time='20191011234432AD';
	end_time='20191012005325AD';
run;

data want;
	set have;
	start_time_sas=input(substr(start_time, 1, 14) , b8601dj.);
	end_time_sas=input(substr(end_time, 1, 14) , b8601dj.);
	format start_time_sas end_time_sas datetime22.;
	diff=end_time_sas - start_time_sas;
	format diff time8.;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;Obs	start_time	end_time	start_time_sas	end_time_sas	diff
1	20191011234432AD	20191012005325AD	11OCT2019:23:44:32	12OCT2019:00:53:25	1:08:53&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/329652"&gt;@EPANG1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;the example for data spread into 2 days are as follow:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;start time:&amp;nbsp;20191011234432AD&lt;/P&gt;
&lt;P&gt;end time:&amp;nbsp;20191012005325AD&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I use substr (starttime,9,6) = 234432&amp;nbsp; and substr(endtime,9,6) =&amp;nbsp;005325&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The output I need is 53 minutes 25 seconds plus 15minutes 28 seconds which gives the final answer as 01:08:53.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this make sense.&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>Wed, 10 Nov 2021 23:30:24 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2021-11-10T23:30:24Z</dc:date>
    <item>
      <title>Need to calculate time difference from character format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-calculate-time-difference-from-character-format/m-p/779658#M248359</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the data stored in character for start time and end time which represent yyyymmddhhmmss.&lt;/P&gt;&lt;P&gt;eg date start time&amp;nbsp; &amp;nbsp;20210218050525 ,&amp;nbsp; &amp;nbsp;date end time 20210218051729&lt;/P&gt;&lt;P&gt;I use the substr function to extract the time as below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;start time 050525&amp;nbsp; &amp;nbsp;end time 051729&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to calculate the time difference.&amp;nbsp;&lt;/P&gt;&lt;P&gt;If the date go over onto the next day. Is there a condition statement can be used ?&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;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Nov 2021 22:22:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-calculate-time-difference-from-character-format/m-p/779658#M248359</guid>
      <dc:creator>EPANG1</dc:creator>
      <dc:date>2021-11-10T22:22:21Z</dc:date>
    </item>
    <item>
      <title>Re: Need to calculate time difference from character format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-calculate-time-difference-from-character-format/m-p/779661#M248361</link>
      <description>What exactly do you want to see as the results, "12 minutes and 4 seconds" or 001204 or 00:12:04?&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 10 Nov 2021 22:19:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-calculate-time-difference-from-character-format/m-p/779661#M248361</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-11-10T22:19:06Z</dc:date>
    </item>
    <item>
      <title>Re: Need to calculate time difference from character format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-calculate-time-difference-from-character-format/m-p/779662#M248362</link>
      <description>&lt;P&gt;the answer I need is to count the time in seconds. So I can convert the answer into hours and minutes later. Also, if the time go over onto the next day, I need a condition statement in order to calculate the time.At the moment, the data is only set as character.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Nov 2021 22:25:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-calculate-time-difference-from-character-format/m-p/779662#M248362</guid>
      <dc:creator>EPANG1</dc:creator>
      <dc:date>2021-11-10T22:25:14Z</dc:date>
    </item>
    <item>
      <title>Re: Need to calculate time difference from character format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-calculate-time-difference-from-character-format/m-p/779663#M248363</link>
      <description>How do you know if it goes into the next day? &lt;BR /&gt;Please provide more details, specifically, an example of your input data and then ideally the matched output example. &lt;BR /&gt;&lt;BR /&gt;SAS stores times as seconds by default. So if you convert them to SAS times and do a mathematical subtraction that will be seconds but it doesn't account for crossing days so you need to explain how that works. If you have dates in another column then you can create datetime variables, subtract them and end up with seconds as well. &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 10 Nov 2021 22:31:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-calculate-time-difference-from-character-format/m-p/779663#M248363</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-11-10T22:31:49Z</dc:date>
    </item>
    <item>
      <title>Re: Need to calculate time difference from character format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-calculate-time-difference-from-character-format/m-p/779665#M248364</link>
      <description>&lt;P&gt;The answer I need is 00:12:04.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Nov 2021 22:52:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-calculate-time-difference-from-character-format/m-p/779665#M248364</guid>
      <dc:creator>EPANG1</dc:creator>
      <dc:date>2021-11-10T22:52:35Z</dc:date>
    </item>
    <item>
      <title>Re: Need to calculate time difference from character format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-calculate-time-difference-from-character-format/m-p/779666#M248365</link>
      <description>What are your inputs?</description>
      <pubDate>Wed, 10 Nov 2021 22:53:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-calculate-time-difference-from-character-format/m-p/779666#M248365</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-11-10T22:53:59Z</dc:date>
    </item>
    <item>
      <title>Re: Need to calculate time difference from character format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-calculate-time-difference-from-character-format/m-p/779667#M248366</link>
      <description>&lt;P&gt;the example for data spread into 2 days are as follow:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;start time:&amp;nbsp;20191011234432AD&lt;/P&gt;&lt;P&gt;end time:&amp;nbsp;20191012005325AD&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I use substr (starttime,9,6) = 234432&amp;nbsp; and substr(endtime,9,6) =&amp;nbsp;005325&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output I need is 53 minutes 25 seconds plus 15minutes 28 seconds which gives the final answer as 01:08:53.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this make sense.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Nov 2021 22:59:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-calculate-time-difference-from-character-format/m-p/779667#M248366</guid>
      <dc:creator>EPANG1</dc:creator>
      <dc:date>2021-11-10T22:59:21Z</dc:date>
    </item>
    <item>
      <title>Re: Need to calculate time difference from character format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-calculate-time-difference-from-character-format/m-p/779670#M248367</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	start_time='20191011234432AD';
	end_time='20191012005325AD';
run;

data want;
	set have;
	start_time_sas=input(substr(start_time, 1, 14) , b8601dj.);
	end_time_sas=input(substr(end_time, 1, 14) , b8601dj.);
	format start_time_sas end_time_sas datetime22.;
	diff=end_time_sas - start_time_sas;
	format diff time8.;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;Obs	start_time	end_time	start_time_sas	end_time_sas	diff
1	20191011234432AD	20191012005325AD	11OCT2019:23:44:32	12OCT2019:00:53:25	1:08:53&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/329652"&gt;@EPANG1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;the example for data spread into 2 days are as follow:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;start time:&amp;nbsp;20191011234432AD&lt;/P&gt;
&lt;P&gt;end time:&amp;nbsp;20191012005325AD&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I use substr (starttime,9,6) = 234432&amp;nbsp; and substr(endtime,9,6) =&amp;nbsp;005325&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The output I need is 53 minutes 25 seconds plus 15minutes 28 seconds which gives the final answer as 01:08:53.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this make sense.&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>Wed, 10 Nov 2021 23:30:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-calculate-time-difference-from-character-format/m-p/779670#M248367</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-11-10T23:30:24Z</dc:date>
    </item>
    <item>
      <title>Re: Need to calculate time difference from character format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-calculate-time-difference-from-character-format/m-p/779678#M248369</link>
      <description>&lt;P&gt;Or use the Informat width definition to avoid the SUBSTR call&lt;/P&gt;
&lt;PRE&gt;data want;
	set have;
	start_time_sas=input(start_time, b8601dj14.);
	end_time_sas=input(end_time, b8601dj14.);
	format start_time_sas end_time_sas datetime22.;
	diff=end_time_sas - start_time_sas;
	format diff time8.;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Nov 2021 23:52:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-calculate-time-difference-from-character-format/m-p/779678#M248369</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-11-10T23:52:21Z</dc:date>
    </item>
    <item>
      <title>Re: Need to calculate time difference from character format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-calculate-time-difference-from-character-format/m-p/779687#M248372</link>
      <description>Thanks, good tip there!</description>
      <pubDate>Thu, 11 Nov 2021 01:48:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-calculate-time-difference-from-character-format/m-p/779687#M248372</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-11-11T01:48:16Z</dc:date>
    </item>
    <item>
      <title>Re: Need to calculate time difference from character format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-calculate-time-difference-from-character-format/m-p/779693#M248373</link>
      <description>&lt;P&gt;the code works beautifully.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Nov 2021 02:40:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-calculate-time-difference-from-character-format/m-p/779693#M248373</guid>
      <dc:creator>EPANG1</dc:creator>
      <dc:date>2021-11-11T02:40:59Z</dc:date>
    </item>
  </channel>
</rss>

