<?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: Use proc sql to convert character [MM/DD/YY:HH:MM:SS AM/PM] to datetime? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Use-proc-sql-to-convert-character-MM-DD-YY-HH-MM-SS-AM-PM-to/m-p/740583#M231374</link>
    <description>&lt;P&gt;Also, how would I code if instead the date was in the form [YYYY/MM/DD:HH:MM:SS AM/PM] ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;With attributes of type=Char ; len=24 format=$24. ; informat = $24.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The following code generates a table called impact2 with column sas_contact_date containing values of "."&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt; &lt;STRONG&gt;format&lt;/STRONG&gt;&lt;SPAN class="s1"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p2"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN class="s2"&gt;picture&lt;/SPAN&gt; mdyampms (&lt;SPAN class="s2"&gt;default&lt;/SPAN&gt;=&lt;SPAN class="s3"&gt;&lt;STRONG&gt;24&lt;/STRONG&gt;&lt;/SPAN&gt;)&lt;/P&gt;&lt;P class="p2"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;low-high=&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN class="s4"&gt;'G/%0m/%0d %0I:%0M:%0S %p'&lt;/SPAN&gt; (&lt;SPAN class="s2"&gt;datatype&lt;/SPAN&gt;=datetime);&lt;/P&gt;&lt;P class="p1"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;SPAN class="s1"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p3"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt; &lt;STRONG&gt;sql&lt;/STRONG&gt;&lt;SPAN class="s1"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p2"&gt;&lt;SPAN class="s2"&gt;create&lt;/SPAN&gt; &lt;SPAN class="s2"&gt;table&lt;/SPAN&gt;&amp;nbsp;impact2 &lt;SPAN class="s2"&gt;as&lt;/SPAN&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p2"&gt;&lt;SPAN class="s2"&gt;select&lt;/SPAN&gt; *,input(contact_date,&lt;SPAN class="s3"&gt;ANYDTDTM21.&lt;/SPAN&gt;) &lt;SPAN class="s2"&gt;as&lt;/SPAN&gt; sas_contact_date&amp;nbsp;&lt;SPAN class="s2"&gt;format&lt;/SPAN&gt;=&lt;SPAN class="s3"&gt;MDYAMPM24.&lt;/SPAN&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p2"&gt;&lt;SPAN class="s2"&gt;FROM&amp;nbsp;&lt;/SPAN&gt;impact;&lt;/P&gt;&lt;P class="p1"&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;&lt;SPAN class="s1"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 11 May 2021 19:15:40 GMT</pubDate>
    <dc:creator>PharmDoc</dc:creator>
    <dc:date>2021-05-11T19:15:40Z</dc:date>
    <item>
      <title>Use proc sql to convert character [MM/DD/YY:HH:MM:SS AM/PM] to datetime?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-proc-sql-to-convert-character-MM-DD-YY-HH-MM-SS-AM-PM-to/m-p/740332#M231248</link>
      <description>&lt;P&gt;I have a dataset called impact with a column called contact_date .&lt;/P&gt;&lt;P&gt;The contact_date column is in a character format&amp;nbsp;&amp;nbsp;[MM/DD/YY:HH:MM:SS AM/PM], that is, for example, one of the observations literally reads "[10/29/20:04:47:26 PM]".&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to use proc sql to convert the contact_date column from character to datetime format.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'v tried the following code, but I want the date to read as 10/29/20 and not 29OCT20:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sql;
select *,input(contact_date, anydtdtm.) as sas_contact_date format=datetime20.
from impact;
quit;&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;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 May 2021 00:24:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-proc-sql-to-convert-character-MM-DD-YY-HH-MM-SS-AM-PM-to/m-p/740332#M231248</guid>
      <dc:creator>PharmDoc</dc:creator>
      <dc:date>2021-05-11T00:24:51Z</dc:date>
    </item>
    <item>
      <title>Re: Use proc sql to convert character [MM/DD/YY:HH:MM:SS AM/PM] to datetime?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-proc-sql-to-convert-character-MM-DD-YY-HH-MM-SS-AM-PM-to/m-p/740353#M231257</link>
      <description>&lt;P&gt;The following code somewhat works, except it doesn't include the seconds and treats the time as 24hr time:&lt;/P&gt;&lt;P&gt;[10/29/20:04:47:26 PM]&amp;nbsp; &amp;nbsp;&lt;SPAN&gt;→&lt;/SPAN&gt;&amp;nbsp; 10/29/2020 4:47 AM&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sql;
select *,input(contact_date,ANYDTDTM.) as sas_contact_date format=MDYAMPM22. 
FROM impact;
quit;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This code includes the seconds, but the date reads as DDMMMYYYY and the time is still treated as 24hr time.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;[10/29/20:04:47:26 PM]&amp;nbsp; &amp;nbsp;→&amp;nbsp;&amp;nbsp; 29OCT2020:04:47:26 AM&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;proc sql;
select *,input(contact_date,ANYDTDTM.) as sas_contact_date format=DATEAMPM22. 
FROM impact;
quit;&lt;/PRE&gt;&lt;P&gt;The following code produces the following notes in the log and outputs missing values:&lt;/P&gt;&lt;DIV class="sasNote sapUiTreeNodeSelected"&gt;NOTE: Invalid date value&lt;/DIV&gt;&lt;DIV class="sasNote"&gt;NOTE: Invalid argument to function INPUT. Missing values may be generated.&lt;/DIV&gt;&lt;DIV class="sasNote"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasNote"&gt;&lt;DIV class="sasNote"&gt;&lt;SPAN&gt;[10/29/20:04:47:26 PM]&amp;nbsp; &amp;nbsp;→&amp;nbsp; .&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;PRE&gt;proc sql;
select *,input(contact_date,MDYAMPM.) as sas_contact_date format=DATEAMPM22. 
FROM impact;
quit;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 May 2021 03:01:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-proc-sql-to-convert-character-MM-DD-YY-HH-MM-SS-AM-PM-to/m-p/740353#M231257</guid>
      <dc:creator>PharmDoc</dc:creator>
      <dc:date>2021-05-11T03:01:40Z</dc:date>
    </item>
    <item>
      <title>Re: Use proc sql to convert character [MM/DD/YY:HH:MM:SS AM/PM] to datetime?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-proc-sql-to-convert-character-MM-DD-YY-HH-MM-SS-AM-PM-to/m-p/740379#M231275</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/357149"&gt;@PharmDoc&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The following code somewhat works, except it doesn't include the seconds and treats the time as 24hr time:&lt;/P&gt;
&lt;P&gt;[10/29/20:04:47:26 PM]&amp;nbsp; &amp;nbsp;&lt;SPAN&gt;→&lt;/SPAN&gt;&amp;nbsp; 10/29/2020 4:47 AM&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
select *,input(contact_date,ANYDTDTM.) as sas_contact_date format=MDYAMPM22. 
FROM impact;
quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The reason it prints an AM time instead of a PM time is not because of the format, but due to the informat used in the INPUT functions, which ignored the "PM" when reading the character value.&amp;nbsp; Replace ANYDTDTM. with ANYDTDTM21 in the INPUT function.&amp;nbsp; This will not cause problems if all of your character values have exactly the number of characters as in your example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, it will still not print seconds.&amp;nbsp; I don't see a SAS format that would print&lt;/P&gt;
&lt;P&gt;&amp;nbsp; 10/20/2020 04:47:26 PM&lt;/P&gt;
&lt;P&gt;so you'll have to define your own format, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
  picture mdyampms (default=22)
    low-high=  '%0m/%0d/%G %0I:%0M:%0S %p' (datatype=datetime);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then assign the format mdyampms in your proc sql statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The picture statement allows you to implement the ("datatype=datetime)" option, which in turn supports the use of the format directives&amp;nbsp; (%0m for month, %0d for day-of-month, etc.).&amp;nbsp; You can find more about the possible datatypes and corresponding directives in&amp;nbsp;&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/proc/p0n990vq8gxca6n1vnsracr6jp2c.htm#p0eubpiv9ngaocn1uatbigc5swi2" target="_self"&gt;PICTURE Statement&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 May 2021 05:57:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-proc-sql-to-convert-character-MM-DD-YY-HH-MM-SS-AM-PM-to/m-p/740379#M231275</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-05-11T05:57:22Z</dc:date>
    </item>
    <item>
      <title>Re: Use proc sql to convert character [MM/DD/YY:HH:MM:SS AM/PM] to datetime?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-proc-sql-to-convert-character-MM-DD-YY-HH-MM-SS-AM-PM-to/m-p/740402#M231283</link>
      <description>&lt;P&gt;You&amp;nbsp;&lt;STRONG&gt;NEVER, EVER&lt;/STRONG&gt; want your date to read xx/xx/xx. Always use 4-digit years. Always.&lt;/P&gt;</description>
      <pubDate>Tue, 11 May 2021 08:19:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-proc-sql-to-convert-character-MM-DD-YY-HH-MM-SS-AM-PM-to/m-p/740402#M231283</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-11T08:19:29Z</dc:date>
    </item>
    <item>
      <title>Re: Use proc sql to convert character [MM/DD/YY:HH:MM:SS AM/PM] to datetime?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-proc-sql-to-convert-character-MM-DD-YY-HH-MM-SS-AM-PM-to/m-p/740563#M231365</link>
      <description>&lt;P&gt;Thank you mkeintz, that was very helpful and it worked.&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;PRE&gt;&lt;FONT color="#000080"&gt;proc format&lt;/FONT&gt;; 
  &lt;FONT color="#0000FF"&gt;picture&lt;/FONT&gt; mdyampms (&lt;FONT color="#0000FF"&gt;default&lt;/FONT&gt;=&lt;FONT color="#008000"&gt;22&lt;/FONT&gt;) 
    low-high=  &lt;FONT color="#800080"&gt;'%0m/%0d/%G %0I:%0M:%0S %p'&lt;/FONT&gt; (&lt;FONT color="#0000FF"&gt;datatype&lt;/FONT&gt;=datetime); 
&lt;FONT color="#000080"&gt;run&lt;/FONT&gt;; 
 
 
&lt;FONT color="#000080"&gt;proc sql&lt;/FONT&gt;; 
&lt;FONT color="#0000FF"&gt;create table&lt;/FONT&gt; impact2 &lt;FONT color="#0000FF"&gt;as&lt;/FONT&gt;  
&lt;FONT color="#0000FF"&gt;select&lt;/FONT&gt; *,&lt;FONT color="#0000FF"&gt;input&lt;/FONT&gt;(contact_date,&lt;FONT color="#008000"&gt;ANYDTDTM21.&lt;/FONT&gt;) &lt;FONT color="#0000FF"&gt;as&lt;/FONT&gt; sas_contact_date format=&lt;FONT color="#008000"&gt;MDYAMPM22.&lt;/FONT&gt;  
&lt;FONT color="#0000FF"&gt;FROM&lt;/FONT&gt; impact; 
&lt;FONT color="#000080"&gt;quit&lt;/FONT&gt;; 
 
&lt;FONT color="#000080"&gt;proc contents&lt;/FONT&gt; &lt;FONT color="#0000FF"&gt;data&lt;/FONT&gt;=impact2; 
&lt;FONT color="#000080"&gt;run&lt;/FONT&gt;; 
&lt;FONT color="#000080"&gt;proc print&lt;/FONT&gt; &lt;FONT color="#0000FF"&gt;data&lt;/FONT&gt;=impact2; 
&lt;FONT color="#000080"&gt;run&lt;/FONT&gt;; &lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, when I run an analysis of the minimum and maximum date, SAS treats the year 1923 as 2023&lt;/P&gt;&lt;P&gt;[01/26/23:12:46:17 PM]&amp;nbsp;&lt;SPAN&gt;→&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;1/26/2023 12:46 PM&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;FONT color="#000080"&gt;proc sql&lt;/FONT&gt;;
	&lt;FONT color="#0000FF"&gt;select&lt;/FONT&gt; &lt;FONT color="#993366"&gt;"sas_contact_date"&lt;/FONT&gt; label=&lt;FONT color="#993366"&gt;"Date variable"&lt;/FONT&gt;, &lt;FONT color="#0000FF"&gt;min&lt;/FONT&gt;(sas_contact_date) 
		format=&lt;FONT color="#008000"&gt;MDYAMPM22.&lt;/FONT&gt; &lt;FONT color="#993366"&gt;&lt;FONT color="#000000"&gt;label=&lt;/FONT&gt;"Minimum date"&lt;/FONT&gt; , &lt;FONT color="#0000FF"&gt;max&lt;/FONT&gt;(sas_contact_date) 
		format=&lt;FONT color="#008000"&gt;MDYAMPM22.&lt;/FONT&gt; label=&lt;FONT color="#993366"&gt;"Maximum date"&lt;/FONT&gt; &lt;FONT color="#0000FF"&gt;from&lt;/FONT&gt; impact2;
&lt;FONT color="#000080"&gt;quit&lt;/FONT&gt;;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 May 2021 17:47:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-proc-sql-to-convert-character-MM-DD-YY-HH-MM-SS-AM-PM-to/m-p/740563#M231365</guid>
      <dc:creator>PharmDoc</dc:creator>
      <dc:date>2021-05-11T17:47:49Z</dc:date>
    </item>
    <item>
      <title>Re: Use proc sql to convert character [MM/DD/YY:HH:MM:SS AM/PM] to datetime?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-proc-sql-to-convert-character-MM-DD-YY-HH-MM-SS-AM-PM-to/m-p/740583#M231374</link>
      <description>&lt;P&gt;Also, how would I code if instead the date was in the form [YYYY/MM/DD:HH:MM:SS AM/PM] ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;With attributes of type=Char ; len=24 format=$24. ; informat = $24.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The following code generates a table called impact2 with column sas_contact_date containing values of "."&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt; &lt;STRONG&gt;format&lt;/STRONG&gt;&lt;SPAN class="s1"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p2"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN class="s2"&gt;picture&lt;/SPAN&gt; mdyampms (&lt;SPAN class="s2"&gt;default&lt;/SPAN&gt;=&lt;SPAN class="s3"&gt;&lt;STRONG&gt;24&lt;/STRONG&gt;&lt;/SPAN&gt;)&lt;/P&gt;&lt;P class="p2"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;low-high=&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN class="s4"&gt;'G/%0m/%0d %0I:%0M:%0S %p'&lt;/SPAN&gt; (&lt;SPAN class="s2"&gt;datatype&lt;/SPAN&gt;=datetime);&lt;/P&gt;&lt;P class="p1"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;SPAN class="s1"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p3"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt; &lt;STRONG&gt;sql&lt;/STRONG&gt;&lt;SPAN class="s1"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p2"&gt;&lt;SPAN class="s2"&gt;create&lt;/SPAN&gt; &lt;SPAN class="s2"&gt;table&lt;/SPAN&gt;&amp;nbsp;impact2 &lt;SPAN class="s2"&gt;as&lt;/SPAN&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p2"&gt;&lt;SPAN class="s2"&gt;select&lt;/SPAN&gt; *,input(contact_date,&lt;SPAN class="s3"&gt;ANYDTDTM21.&lt;/SPAN&gt;) &lt;SPAN class="s2"&gt;as&lt;/SPAN&gt; sas_contact_date&amp;nbsp;&lt;SPAN class="s2"&gt;format&lt;/SPAN&gt;=&lt;SPAN class="s3"&gt;MDYAMPM24.&lt;/SPAN&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p2"&gt;&lt;SPAN class="s2"&gt;FROM&amp;nbsp;&lt;/SPAN&gt;impact;&lt;/P&gt;&lt;P class="p1"&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;&lt;SPAN class="s1"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 May 2021 19:15:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-proc-sql-to-convert-character-MM-DD-YY-HH-MM-SS-AM-PM-to/m-p/740583#M231374</guid>
      <dc:creator>PharmDoc</dc:creator>
      <dc:date>2021-05-11T19:15:40Z</dc:date>
    </item>
    <item>
      <title>Re: Use proc sql to convert character [MM/DD/YY:HH:MM:SS AM/PM] to datetime?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-proc-sql-to-convert-character-MM-DD-YY-HH-MM-SS-AM-PM-to/m-p/740590#M231377</link>
      <description>&lt;P&gt;Why are you only reading 21 of the 24 characters in your string?&amp;nbsp; In does not really hurt to tell INPUT() to use more characters than the string you are giving it actually has, as long as the width is within the range supported by the informat.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does you string really have the square brackets?&amp;nbsp; You will want to remove those first.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  string='[2021/05/11:00:10:30 PM]' ;
  dt = input(compress(string,'[]'),anydtdtm24.);
  format dt datetime19.;
  put dt=;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;929   data test;
930     string='[2021/05/11:00:10:30 PM]' ;
931     dt = input(compress(string,'[]'),anydtdtm24.);
932     format dt datetime19.;
933     put dt=;
934   run;

dt=11MAY2021:12:10:30
NOTE: The data set WORK.TEST has 1 observations and 2 variables.
&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 May 2021 19:28:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-proc-sql-to-convert-character-MM-DD-YY-HH-MM-SS-AM-PM-to/m-p/740590#M231377</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-11T19:28:39Z</dc:date>
    </item>
    <item>
      <title>Re: Use proc sql to convert character [MM/DD/YY:HH:MM:SS AM/PM] to datetime?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-proc-sql-to-convert-character-MM-DD-YY-HH-MM-SS-AM-PM-to/m-p/740642#M231407</link>
      <description>&lt;P&gt;Okay, so again following as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;suggested earlier. I created a custom format:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;FONT color="#000080"&gt;proc format&lt;/FONT&gt;;&lt;BR /&gt;  &lt;FONT color="#0000FF"&gt;picture&lt;/FONT&gt; mdyampms (&lt;FONT color="#0000FF"&gt;default&lt;/FONT&gt;=&lt;FONT color="#003300"&gt;24&lt;/FONT&gt;)&lt;BR /&gt;    low-high=  &lt;FONT color="#800080"&gt;'%G/%0m/%0d %0I:%0M:%0S %p'&lt;/FONT&gt; (&lt;FONT color="#0000FF"&gt;datatype&lt;/FONT&gt;=datetime);&lt;BR /&gt;&lt;FONT color="#000080"&gt;run&lt;/FONT&gt;;&lt;/PRE&gt;&lt;P&gt;But there's a caveat, when I specify format=mdyampms22 in proc sql below, the datetime correctly reads as YYYY/MM/SS HH:MM:SS AM/PM in a new column, which is great, but when I try to create a frequency plot (see last snippets of code) the dates do not show up on the x-axis, whereas they do show up on the xaxis of a frequency plot when I type in the non-customized format= mdyampm22 in the proc sql statement (illustrated at the end of this reply)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Following along the lines as what&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&amp;nbsp;suggests and incorporating into proc sql:&lt;/P&gt;&lt;P&gt;(I noticed that in proc contents the Informat is blank):&lt;/P&gt;&lt;PRE&gt;&lt;FONT color="#000080"&gt;proc sql&lt;/FONT&gt;;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;create table&lt;/FONT&gt; impact2 &lt;FONT color="#0000FF"&gt;as&lt;/FONT&gt; &lt;BR /&gt;&lt;FONT color="#0000FF"&gt;select&lt;/FONT&gt; *,&lt;FONT color="#0000FF"&gt;input&lt;/FONT&gt;(&lt;FONT color="#0000FF"&gt;compress&lt;/FONT&gt;(contact_date,&lt;FONT color="#800080"&gt;'[]'&lt;/FONT&gt;),&lt;FONT color="#003300"&gt;ANYDTDTM24.&lt;/FONT&gt;) &lt;FONT color="#0000FF"&gt;as&lt;/FONT&gt; fcontact_date format=&lt;FONT color="#003300"&gt;mdyampms22.&lt;/FONT&gt; &lt;BR /&gt;&lt;FONT color="#0000FF"&gt;FROM&lt;/FONT&gt; impact;&lt;BR /&gt;&lt;FONT color="#000080"&gt;quit&lt;/FONT&gt;;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;proc contents&lt;/FONT&gt; &lt;FONT color="#0000FF"&gt;data&lt;/FONT&gt;=impact2;&lt;BR /&gt;&lt;FONT color="#000080"&gt;run&lt;/FONT&gt;;&lt;BR /&gt;&lt;FONT color="#000080"&gt;proc print&lt;/FONT&gt; &lt;FONT color="#0000FF"&gt;data&lt;/FONT&gt;=impact2;&lt;BR /&gt;&lt;FONT color="#000080"&gt;run&lt;/FONT&gt;;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or alternatively I can forgo proc sql and again follow more along what&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;suggested but use a data step:&lt;/P&gt;&lt;PRE&gt;&lt;FONT color="#000080"&gt;&lt;FONT color="#008000"&gt;/* mdyampms22. is from the custom format created earlier   */&lt;/FONT&gt;&lt;BR /&gt;data&lt;/FONT&gt; impact2;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;set&lt;/FONT&gt; impact;&lt;BR /&gt;  fcontact_date = &lt;FONT color="#0000FF"&gt;input&lt;/FONT&gt;(&lt;FONT color="#0000FF"&gt;compress&lt;/FONT&gt;(contact_date,&lt;FONT color="#800080"&gt;'[]'&lt;/FONT&gt;),&lt;FONT color="#003300"&gt;anydtdtm24.&lt;/FONT&gt;);&lt;BR /&gt;  &lt;FONT color="#0000FF"&gt;format&lt;/FONT&gt; contact_date &lt;FONT color="#003300"&gt;mdyampms22.&lt;/FONT&gt;;&lt;BR /&gt;  &lt;FONT color="#0000FF"&gt;put&lt;/FONT&gt; fcontact_date=;&lt;BR /&gt;&lt;FONT color="#000080"&gt;run&lt;/FONT&gt;;&lt;BR /&gt;&lt;FONT color="#000080"&gt;proc contents&lt;/FONT&gt; &lt;FONT color="#0000FF"&gt;data&lt;/FONT&gt;=impact2;&lt;BR /&gt;&lt;FONT color="#000080"&gt;run&lt;/FONT&gt;;&lt;BR /&gt;&lt;FONT color="#000080"&gt;proc print&lt;/FONT&gt; &lt;FONT color="#0000FF"&gt;data&lt;/FONT&gt;=impact2; &lt;BR /&gt;&lt;FONT color="#000080"&gt;run&lt;/FONT&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;P&gt;But even using the datastep method, the values do not show up on the xaxis of a frequency plot (see below)&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;/*** Analyze date variables ***/&lt;BR /&gt;title "Minimum and Maximum Dates";&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;	select "fcontact_date" label="Date variable", min(fcontact_date) &lt;BR /&gt;		format=MDYAMPMS22. label="Minimum date" , max(fcontact_date) &lt;BR /&gt;		format=MDYAMPMS22. label="Maximum date" from impact2;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;title "Date Frequencies";&lt;BR /&gt;&lt;BR /&gt;proc freq data=impact2 noprint;&lt;BR /&gt;	tables fcontact_date /  out=_tmpfreq1;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc sgplot data=_tmpfreq1;&lt;BR /&gt;	yaxis min=0 minor ;&lt;BR /&gt;	xaxis minor type=time;&lt;BR /&gt;	series x=fcontact_date y=count;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc delete data=_tmpfreq1;&lt;BR /&gt;run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Date variable Minimum date Maximum date&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;fcontact_date&lt;/TD&gt;&lt;TD&gt;2010/12/28 01:53:57 PM&lt;/TD&gt;&lt;TD&gt;2026/09/19 10:44:42 AM&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="with format=mdyampms22." style="width: 644px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/59230i26F19A335CE4B9FA/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screen Shot 2021-05-11 at 7.24.32 PM.png" alt="with format=mdyampms22." /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;with format=mdyampms22.&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Again, specifying mdyampm22. instead of the custom mdyampms22. in proc sql ultimately inputs the year labels on the xaxis of the frequency plot&lt;/P&gt;&lt;P&gt;(BUT, again, years 1923 and 1926 are being treated as 2023 and 2026):&lt;/P&gt;&lt;P&gt;This begs the question of if using the picture statement to create the custom format&amp;nbsp;&lt;FONT color="#003300"&gt;mdyampms22. &lt;FONT color="#000000"&gt;is necessary?&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;PRE&gt;proc sql;&lt;BR /&gt;create table impact2 as &lt;BR /&gt;select *,input(compress(contact_date,'[]'),ANYDTDTM24.) as fcontact_date format=mdyampm22. &lt;BR /&gt;FROM impact;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;PRE&gt;title "Minimum and Maximum Dates";&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;	select "fcontact_date" label="Date variable", min(fcontact_date) &lt;BR /&gt;		format=MDYAMPM22. label="Minimum date" , max(fcontact_date) &lt;BR /&gt;		format=MDYAMPM22. label="Maximum date" from impact2;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;title "Date Frequencies";&lt;BR /&gt;&lt;BR /&gt;proc freq data=impact2 noprint;&lt;BR /&gt;	tables fcontact_date / out=_tmpfreq1;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc sgplot data=_tmpfreq1;&lt;BR /&gt;	yaxis min=0 minor ;&lt;BR /&gt;	xaxis minor type=time ;&lt;BR /&gt;	series x=fcontact_date y=count;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc delete data=_tmpfreq1;&lt;BR /&gt;run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Date variable Minimum date Maximum date&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;fcontact_date&lt;/TD&gt;&lt;TD&gt;12/28/2010 1:53 PM&lt;/TD&gt;&lt;TD&gt;9/19/2026 10:44 AM&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="with format=mdyampm22." style="width: 644px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/59231i00669DC974299571/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screen Shot 2021-05-11 at 7.26.59 PM.png" alt="with format=mdyampm22." /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;with format=mdyampm22.&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 May 2021 23:34:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-proc-sql-to-convert-character-MM-DD-YY-HH-MM-SS-AM-PM-to/m-p/740642#M231407</guid>
      <dc:creator>PharmDoc</dc:creator>
      <dc:date>2021-05-11T23:34:01Z</dc:date>
    </item>
  </channel>
</rss>

