<?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: calculate minute difference between 2 timestamps (character variables) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/calculate-minute-difference-between-2-timestamps-character/m-p/820523#M323868</link>
    <description>&lt;P&gt;Thank you Paige for your reply which of course would have been the correct way to do it starting out.&lt;/P&gt;
&lt;P&gt;Margaret&lt;/P&gt;</description>
    <pubDate>Mon, 27 Jun 2022 13:08:32 GMT</pubDate>
    <dc:creator>urban58</dc:creator>
    <dc:date>2022-06-27T13:08:32Z</dc:date>
    <item>
      <title>calculate minute difference between 2 timestamps (character variables)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-minute-difference-between-2-timestamps-character/m-p/820516#M323863</link>
      <description>&lt;P&gt;Hello SAS community,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm trying to find the minutes between 2 timestamps (both character variables) in a large SAS dataset I received,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;e.g.&lt;/P&gt;
&lt;P&gt;id&amp;nbsp; &amp;nbsp; &amp;nbsp;starttime&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;endtime&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;2021-10-27 10:11:01&amp;nbsp; &amp;nbsp; &amp;nbsp; 2021-10-27 10:19:33&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I know intck function can do it but all the examples I see have date in a date9 format&amp;nbsp;&lt;/P&gt;
&lt;P&gt;mins_between =&amp;nbsp; intck('minute',starttime,endtime);&lt;/P&gt;
&lt;P&gt;but I just get&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Character values have been converted to numeric values at the places given by: (Line):(Column).&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;171:31 171:46&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;NOTE: Invalid numeric data, starttime='2021-10-27 10:11:01' , at line 171 column 31.&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;NOTE: Invalid numeric data, endtime='2021-10-27 10:19:33' , at line 171 column 46.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can someone please help!&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Margaret&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;</description>
      <pubDate>Mon, 27 Jun 2022 12:35:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-minute-difference-between-2-timestamps-character/m-p/820516#M323863</guid>
      <dc:creator>urban58</dc:creator>
      <dc:date>2022-06-27T12:35:36Z</dc:date>
    </item>
    <item>
      <title>Re: calculate minute difference between 2 timestamps (character variables)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-minute-difference-between-2-timestamps-character/m-p/820518#M323864</link>
      <description>&lt;P&gt;You need to convert your strings into DATETIME values.&lt;/P&gt;
&lt;P&gt;You could try using ANYDTDTM informat, but that is a GUESSING procedure.&lt;/P&gt;
&lt;P&gt;The most reliable way is to convert the date part of the string and the time part separately and recombine into a datetime value.&amp;nbsp; That way you can ensure the date string is evaluated using YMD order and not some other order.&lt;/P&gt;
&lt;P&gt;Since datetime values are stored in seconds to convert the difference to minutes just divide by 60 since there are 60 seconds in a minute.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;startdt = dhms(input(startime,yymmdd10.),0,0,input(scan(starttime,2,' '),time8.));
enddt = dhms(input(endtime,yymmdd10.),0,0,input(scan(endtime,2,' '),time8.));
mintues = (enddt - starrdt) / 60 ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want an integer then decide how want to convert 5 minutes and 30 seconds into an integer number of minutes.&amp;nbsp; Is that 5 minutes? then use INT() .&amp;nbsp; Should that be 6 minutes then use ROUND().&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jun 2022 12:46:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-minute-difference-between-2-timestamps-character/m-p/820518#M323864</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-27T12:46:13Z</dc:date>
    </item>
    <item>
      <title>Re: calculate minute difference between 2 timestamps (character variables)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-minute-difference-between-2-timestamps-character/m-p/820520#M323866</link>
      <description>&lt;P&gt;If your starttime and endtime are really character strings, then you can't do any subtraction at all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are creating these variables as character strings, that's a mistake. Read them in or create them as numeric (date/time values, which are in seconds), then do the subtraction and divide by 60 seconds in a minute. The informat :ymddttm18. will read the values in as SAS date/time values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input id starttime &amp;amp; :ymddttm18. endtime &amp;amp; :ymddttm18. ;
cards;
1     2021-10-27 10:11:01      2021-10-27 10:19:33
;
data want;
    set have;
    minute_difference=(endtime-starttime)/60;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jun 2022 12:51:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-minute-difference-between-2-timestamps-character/m-p/820520#M323866</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-06-27T12:51:37Z</dc:date>
    </item>
    <item>
      <title>Re: calculate minute difference between 2 timestamps (character variables)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-minute-difference-between-2-timestamps-character/m-p/820522#M323867</link>
      <description>&lt;P&gt;Thank you Tom for you quick comprehensive reply. I like your answer as I don't have to make changes to other variables I've created from these timestamps and it works beautifully.&lt;/P&gt;
&lt;P&gt;Also thanks for thinking of the rounding function as that was my next thought when I saw the output!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Really appreciate it.&lt;/P&gt;
&lt;P&gt;Margaret&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jun 2022 13:05:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-minute-difference-between-2-timestamps-character/m-p/820522#M323867</guid>
      <dc:creator>urban58</dc:creator>
      <dc:date>2022-06-27T13:05:40Z</dc:date>
    </item>
    <item>
      <title>Re: calculate minute difference between 2 timestamps (character variables)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-minute-difference-between-2-timestamps-character/m-p/820523#M323868</link>
      <description>&lt;P&gt;Thank you Paige for your reply which of course would have been the correct way to do it starting out.&lt;/P&gt;
&lt;P&gt;Margaret&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jun 2022 13:08:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-minute-difference-between-2-timestamps-character/m-p/820523#M323868</guid>
      <dc:creator>urban58</dc:creator>
      <dc:date>2022-06-27T13:08:32Z</dc:date>
    </item>
  </channel>
</rss>

