<?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: Convert Data String to Date TIMEAMPM? in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Convert-Data-String-to-Date-TIMEAMPM/m-p/246955#M17517</link>
    <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input tc &amp;amp; :$11.;              /* Read time as character value tc. */
t=input(tc, ?? anydttme11.);   /* Try to convert it using ANYDTTME11. informat. */
if cmiss(t, tc)=1 then do;     /* If conversion failed, i.e., t is missing, but not tc, */
  _i=indexc(upcase(tc), 'AP'); /* check if an 'A' or 'P' occurs in tc. */
  if _i &amp;amp; ~index(tc, ':') then /* If this is the case, but no colon can be found in tc, */
  tc=cats(substr(tc,1,_i-1),':00',substr(tc,_i)); /* insert ":00" before the 'A' or 'P'. */
  t=input(tc, ?? anydttme11.); /* Try to convert the extended string. */
end;
drop _i tc;
format t time8.; /* optional formatting of SAS time value */
cards;
1AM
2AM
3AM
12AM
13AM
8:00AM
8PM
9 PM
19PM
29PM
10:00PM
11:34:56 PM
;

proc print data=test;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As you can see, the above code is applicable not only to the format "HHAM/PM", but can be used to read other time formats as well (including unlikely values such as 13AM or 29PM).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, please note that the TIMEAMPM. &lt;EM&gt;format&lt;/EM&gt; cannot be used to &lt;EM&gt;read&lt;/EM&gt; data and currently (SAS 9.4) there seems to be no &lt;EM&gt;in&lt;/EM&gt;format of this name.&lt;/P&gt;</description>
    <pubDate>Fri, 29 Jan 2016 19:11:18 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2016-01-29T19:11:18Z</dc:date>
    <item>
      <title>Convert Data String to Date TIMEAMPM?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Convert-Data-String-to-Date-TIMEAMPM/m-p/246937#M17516</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have data source as below. It is read in as String. And I would like to convert it to time format. I tried the INPUT function using the TIMEAMPM5. format. It does not work. Any help? Much Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1AM&lt;/P&gt;&lt;P&gt;2AM&lt;/P&gt;&lt;P&gt;3AM&lt;/P&gt;&lt;P&gt;1PM&lt;/P&gt;&lt;P&gt;2PM&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jan 2016 17:21:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Convert-Data-String-to-Date-TIMEAMPM/m-p/246937#M17516</guid>
      <dc:creator>LLG</dc:creator>
      <dc:date>2016-01-29T17:21:20Z</dc:date>
    </item>
    <item>
      <title>Re: Convert Data String to Date TIMEAMPM?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Convert-Data-String-to-Date-TIMEAMPM/m-p/246955#M17517</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input tc &amp;amp; :$11.;              /* Read time as character value tc. */
t=input(tc, ?? anydttme11.);   /* Try to convert it using ANYDTTME11. informat. */
if cmiss(t, tc)=1 then do;     /* If conversion failed, i.e., t is missing, but not tc, */
  _i=indexc(upcase(tc), 'AP'); /* check if an 'A' or 'P' occurs in tc. */
  if _i &amp;amp; ~index(tc, ':') then /* If this is the case, but no colon can be found in tc, */
  tc=cats(substr(tc,1,_i-1),':00',substr(tc,_i)); /* insert ":00" before the 'A' or 'P'. */
  t=input(tc, ?? anydttme11.); /* Try to convert the extended string. */
end;
drop _i tc;
format t time8.; /* optional formatting of SAS time value */
cards;
1AM
2AM
3AM
12AM
13AM
8:00AM
8PM
9 PM
19PM
29PM
10:00PM
11:34:56 PM
;

proc print data=test;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As you can see, the above code is applicable not only to the format "HHAM/PM", but can be used to read other time formats as well (including unlikely values such as 13AM or 29PM).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, please note that the TIMEAMPM. &lt;EM&gt;format&lt;/EM&gt; cannot be used to &lt;EM&gt;read&lt;/EM&gt; data and currently (SAS 9.4) there seems to be no &lt;EM&gt;in&lt;/EM&gt;format of this name.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jan 2016 19:11:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Convert-Data-String-to-Date-TIMEAMPM/m-p/246955#M17517</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-01-29T19:11:18Z</dc:date>
    </item>
    <item>
      <title>Re: Convert Data String to Date TIMEAMPM?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Convert-Data-String-to-Date-TIMEAMPM/m-p/246956#M17518</link>
      <description>&lt;P&gt;The time formats expect a minimum of 5 characters and your data is too short so you need to convert not read. Maybe one of these days SAS will allow an equivalent of a PICTURE for inputing values that allows date and time directives.&lt;/P&gt;
&lt;P&gt;You will need to add a variable as a character can't be converted. Something like this might get you started.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   input texttime $;&lt;BR /&gt;   i=index(upcase(texttime),'P')&amp;gt;0) *12;
   mytime = hms(input(compress(texttime,'APM'),best2.) +1,0,0);&lt;BR /&gt;   drop i;
   format mytime  timeampm5.;
datalines;
1AM
2AM
3AM
10AM
1PM
2PM
11PM
;
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>Fri, 29 Jan 2016 23:51:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Convert-Data-String-to-Date-TIMEAMPM/m-p/246956#M17518</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-01-29T23:51:12Z</dc:date>
    </item>
  </channel>
</rss>

