<?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: SAS Times not converting properly from string to time in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-Times-not-converting-properly-from-string-to-time/m-p/367721#M275400</link>
    <description>&lt;P&gt;Ugh I knew it would be something simple. Thank you! Can you explain why scan works&amp;nbsp;here but substring doesn't?&lt;/P&gt;</description>
    <pubDate>Fri, 16 Jun 2017 13:45:04 GMT</pubDate>
    <dc:creator>ColeG</dc:creator>
    <dc:date>2017-06-16T13:45:04Z</dc:date>
    <item>
      <title>SAS Times not converting properly from string to time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Times-not-converting-properly-from-string-to-time/m-p/367713#M275398</link>
      <description>&lt;P&gt;I have a series of date-time variables in the format&lt;/P&gt;&lt;P&gt;mm/dd/yyyy hhmm&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to split these into date variables and time variables. The dates work okay, and sometimes the times work, but sometimes the time is slightly wrong.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Datetime examples:&lt;/P&gt;&lt;P&gt;10/26/2016 1400&lt;/P&gt;&lt;P&gt;10/26/2016 1826&lt;/P&gt;&lt;P&gt;1/29/2017 1004&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;If I use the code&lt;/FONT&gt;:&lt;/P&gt;&lt;PRE&gt;&lt;FONT color="#000000" face="arial,helvetica,sans-serif" size="2"&gt;date1 = scan(datetime, 1, ' ');
date = input(date1, mmddyy10.);

time1 = substr(datetime, max(1, length(datetime) - 4));
time = input(substr(time1, 1, length(time1) - 2) || ':' || substr(time1, length(time1) - 1), time5.);&lt;/FONT&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then I get the output&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000" face="arial,helvetica,sans-serif" size="2"&gt;Date (#):&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Time (#):&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000" face="arial,helvetica,sans-serif" size="2"&gt;10/26/2016&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 14:00&amp;nbsp; -&amp;gt; right&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000" face="arial,helvetica,sans-serif" size="2"&gt;10/26/2016&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;18:02&amp;nbsp; -&amp;gt; wrong&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000" face="arial,helvetica,sans-serif" size="2"&gt;1/29/2017&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;10:00&amp;nbsp; -&amp;gt; wrong&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, if I don't try to make time a number and use the code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;FONT color="#000000" face="arial,helvetica,sans-serif" size="2"&gt;date1 = scan(datetime, 1, ' ');
date = input(date1, mmddyy10.);

time1 = substr(datetime, max(1, length(datetime) - 4));
time = substr(time1, 1, length(time1) - 2) || ':' || substr(time1, length(time1) - 1);&lt;/FONT&gt;&lt;/PRE&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000" face="arial,helvetica,sans-serif" size="2"&gt;Then I get the output&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000" face="arial,helvetica,sans-serif" size="2"&gt;Date (#):&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Time (char):&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;10/26/2016&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 14:00&amp;nbsp; -&amp;gt; right&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000" face="arial,helvetica,sans-serif" size="2"&gt;10/26/2016&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;18:26&amp;nbsp; -&amp;gt; right&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000" face="arial,helvetica,sans-serif" size="2"&gt;1/29/2017&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;10:04&amp;nbsp; -&amp;gt; right&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000" face="arial,helvetica,sans-serif" size="2"&gt;I need time to be numeric so that I can merge with other datasets that have time as a number. Any help would be appreciated.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jun 2017 13:33:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Times-not-converting-properly-from-string-to-time/m-p/367713#M275398</guid>
      <dc:creator>ColeG</dc:creator>
      <dc:date>2017-06-16T13:33:16Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Times not converting properly from string to time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Times-not-converting-properly-from-string-to-time/m-p/367718#M275399</link>
      <description>&lt;P&gt;Use scan() for time1 also:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test (keep=datetime date time);
input datetime $26.;
format date mmddyy10. time time5.;
date1 = scan(datetime, 1, ' ');
date = input(date1, mmddyy10.);
time1 = scan(datetime,2,' ');
time = input(substr(time1,1,2)!!':'!!substr(time1,3),time5.);
cards;
10/26/2016 1400
10/26/2016 1826
1/29/2017 1004
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 16 Jun 2017 13:40:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Times-not-converting-properly-from-string-to-time/m-p/367718#M275399</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-06-16T13:40:42Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Times not converting properly from string to time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Times-not-converting-properly-from-string-to-time/m-p/367721#M275400</link>
      <description>&lt;P&gt;Ugh I knew it would be something simple. Thank you! Can you explain why scan works&amp;nbsp;here but substring doesn't?&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jun 2017 13:45:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Times-not-converting-properly-from-string-to-time/m-p/367721#M275400</guid>
      <dc:creator>ColeG</dc:creator>
      <dc:date>2017-06-16T13:45:04Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Times not converting properly from string to time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Times-not-converting-properly-from-string-to-time/m-p/367731#M275401</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/134244"&gt;@ColeG&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Ugh I knew it would be something simple. Thank you! Can you explain why scan works&amp;nbsp;here but substring doesn't?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Your error was here:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;time1 = substr(datetime, max(1, length(datetime) - 4));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;time1 = substr(datetime, max(1, length(datetime) - 3));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;instead.&lt;/P&gt;
&lt;P&gt;Your time1 always started with a blank, and had a length of 5.&lt;/P&gt;
&lt;P&gt;That leading blank (which propagates through the rest of processing) seems to confuse the time5. informat, so that it starts reading the minutes from the 4th position (of a 6-byte string!), where the ':' is.&lt;/P&gt;
&lt;P&gt;Confirm this by adding an additional line of input data:&lt;/P&gt;
&lt;PRE&gt;10/26/2016 1430&lt;/PRE&gt;
&lt;P&gt;You'll get a time of 14:03 with your original code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That's why I prefer to use scan() instead of substr()s. Has less potential for such errors.&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jun 2017 14:11:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Times-not-converting-properly-from-string-to-time/m-p/367731#M275401</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-06-16T14:11:10Z</dc:date>
    </item>
  </channel>
</rss>

