<?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 Datetime Character to Date Format with Varying Character Lengths in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Convert-Datetime-Character-to-Date-Format-with-Varying-Character/m-p/780762#M248797</link>
    <description>&lt;P&gt;What value do you want for the time of day when it is not specified?&amp;nbsp; Is it ok to set it to zero (midnight)?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input id $ seq char_date $20.;
cards;
1	1	2018-04-25T11:26
1	2	2018-04-25
1	3	2018-04-26
2	1	2017-09-30T06:23
2	2	2017-10-05
3	1	2018-03-17
3	2	2018-03-17
3	3	2018-04-02T22:00
4	1	2017-12-04T13:41
4	2	2017-12-08T09:20
5	1	2018-01-15
;

data want;
  set have;
  dt = input(char_date,b8601dt20.);
  date=datepart(dt);
  time=timepart(dt);
  format dt datetime19. date date9. time tod8.;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs    id    seq    char_date                            dt         date        time

  1    1      1     2018-04-25T11:26     25APR2018:11:26:00    25APR2018    11:26:00
  2    1      2     2018-04-25           25APR2018:00:00:00    25APR2018    00:00:00
  3    1      3     2018-04-26           26APR2018:00:00:00    26APR2018    00:00:00
  4    2      1     2017-09-30T06:23     30SEP2017:06:23:00    30SEP2017    06:23:00
  5    2      2     2017-10-05           05OCT2017:00:00:00    05OCT2017    00:00:00
  6    3      1     2018-03-17           17MAR2018:00:00:00    17MAR2018    00:00:00
  7    3      2     2018-03-17           17MAR2018:00:00:00    17MAR2018    00:00:00
  8    3      3     2018-04-02T22:00     02APR2018:22:00:00    02APR2018    22:00:00
  9    4      1     2017-12-04T13:41     04DEC2017:13:41:00    04DEC2017    13:41:00
 10    4      2     2017-12-08T09:20     08DEC2017:09:20:00    08DEC2017    09:20:00
 11    5      1     2018-01-15           15JAN2018:00:00:00    15JAN2018    00:00:00
&lt;/PRE&gt;</description>
    <pubDate>Wed, 17 Nov 2021 15:34:30 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-11-17T15:34:30Z</dc:date>
    <item>
      <title>Convert Datetime Character to Date Format with Varying Character Lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-Datetime-Character-to-Date-Format-with-Varying-Character/m-p/780755#M248791</link>
      <description>&lt;P&gt;I want to convert dates stored as character to actual date format. However, one of the date columns is inconsistent. I tried the macro shown below; however, it isn't converting anything that contains a time. Is there another informat I should be using instead?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Code:&lt;/U&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 input id $ seq char_date : $20.;
 cards;
 	1	1	2018-04-25T11:26
	1	2	2018-04-25
	1	3	2018-04-26
	2	1	2017-09-30T06:23
	2	2	2017-10-05
	3	1	2018-03-17
	3	2	2018-03-17
	3	3	2018-04-02T22:00
	4	1	2017-12-04T13:41
	4	2	2017-12-08T09:20
	5	1	2018-01-15
	;
run;

%macro dtfmt(dsn, var);
	data &amp;amp;dsn.;
	 set &amp;amp;dsn.;

	 &amp;amp;var.c = input(&amp;amp;var., anydtdte22.);
	run;
%mend dtfmt;

%dtfmt(dsn= have, var= char_date);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;U&gt;Result:&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="mariko5797_0-1637161385495.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/65838i1212A1F4465DB90F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="mariko5797_0-1637161385495.png" alt="mariko5797_0-1637161385495.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Desired Result:&lt;/U&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 input id $ seq date : yymmdd10. time : time6.;
 format date date9. time time6.;
 cards;
 	1	1	2018-04-25	11:26
	1	2	2018-04-25	.
	1	3	2018-04-26	.
	2	1	2017-09-30	06:23
	2	2	2017-10-05	.
	3	1	2018-03-17	.
	3	2	2018-03-17	.
	3	3	2018-04-02	22:00
	4	1	2017-12-04	13:41
	4	2	2017-12-08	09:20
	5	1	2018-01-15	.
	;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 Nov 2021 15:09:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-Datetime-Character-to-Date-Format-with-Varying-Character/m-p/780755#M248791</guid>
      <dc:creator>mariko5797</dc:creator>
      <dc:date>2021-11-17T15:09:50Z</dc:date>
    </item>
    <item>
      <title>Re: Convert Datetime Character to Date Format with Varying Character Lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-Datetime-Character-to-Date-Format-with-Varying-Character/m-p/780756#M248792</link>
      <description>&lt;P&gt;IF you want a date from that structure use the informat YYMMDD10.&lt;/P&gt;
&lt;P&gt;That will only read the 10 characters you have an ignore the rest.&lt;/P&gt;
&lt;P&gt;read the remainder as time with&lt;/P&gt;
&lt;P&gt;Time = input (scan(char_date,2,'T'),time.);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Suggestion: Apply a format so you can tell the value instead of seeing numbers like 21299.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Warning: habitual use of the&lt;/P&gt;
&lt;PRE&gt;data somename;
   set somename;
  &amp;lt;code&amp;gt;
run;&lt;/PRE&gt;
&lt;P&gt;will at some time cause you a headache because of minor logic errors. That coding completely replaces the original data set and you may have a lot of fun tracing back just how you managed to destroy the values you need.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/311553"&gt;@mariko5797&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I want to convert dates stored as character to actual date format. However, one of the date columns is inconsistent. I tried the macro shown below; however, it isn't converting anything that contains a time. Is there another informat I should be using instead?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Code:&lt;/U&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 input id $ seq char_date : $20.;
 cards;
 	1	1	2018-04-25T11:26
	1	2	2018-04-25
	1	3	2018-04-26
	2	1	2017-09-30T06:23
	2	2	2017-10-05
	3	1	2018-03-17
	3	2	2018-03-17
	3	3	2018-04-02T22:00
	4	1	2017-12-04T13:41
	4	2	2017-12-08T09:20
	5	1	2018-01-15
	;
run;

%macro dtfmt(dsn, var);
	data &amp;amp;dsn.;
	 set &amp;amp;dsn.;

	 &amp;amp;var.c = input(&amp;amp;var., anydtdte22.);
	run;
%mend dtfmt;

%dtfmt(dsn= have, var= char_date);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;U&gt;Result:&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="mariko5797_0-1637161385495.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/65838i1212A1F4465DB90F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="mariko5797_0-1637161385495.png" alt="mariko5797_0-1637161385495.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Desired Result:&lt;/U&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 input id $ seq date : yymmdd10. time : time6.;
 format date date9. time time6.;
 cards;
 	1	1	2018-04-25	11:26
	1	2	2018-04-25	.
	1	3	2018-04-26	.
	2	1	2017-09-30	06:23
	2	2	2017-10-05	.
	3	1	2018-03-17	.
	3	2	2018-03-17	.
	3	3	2018-04-02	22:00
	4	1	2017-12-04	13:41
	4	2	2017-12-08	09:20
	5	1	2018-01-15	.
	;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Nov 2021 15:18:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-Datetime-Character-to-Date-Format-with-Varying-Character/m-p/780756#M248792</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-11-17T15:18:48Z</dc:date>
    </item>
    <item>
      <title>Re: Convert Datetime Character to Date Format with Varying Character Lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-Datetime-Character-to-Date-Format-with-Varying-Character/m-p/780757#M248793</link>
      <description>&lt;P&gt;Is there a way to extract only the time from CHAR_DATE? I want both the date and time parts.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Nov 2021 15:14:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-Datetime-Character-to-Date-Format-with-Varying-Character/m-p/780757#M248793</guid>
      <dc:creator>mariko5797</dc:creator>
      <dc:date>2021-11-17T15:14:18Z</dc:date>
    </item>
    <item>
      <title>Re: Convert Datetime Character to Date Format with Varying Character Lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-Datetime-Character-to-Date-Format-with-Varying-Character/m-p/780762#M248797</link>
      <description>&lt;P&gt;What value do you want for the time of day when it is not specified?&amp;nbsp; Is it ok to set it to zero (midnight)?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input id $ seq char_date $20.;
cards;
1	1	2018-04-25T11:26
1	2	2018-04-25
1	3	2018-04-26
2	1	2017-09-30T06:23
2	2	2017-10-05
3	1	2018-03-17
3	2	2018-03-17
3	3	2018-04-02T22:00
4	1	2017-12-04T13:41
4	2	2017-12-08T09:20
5	1	2018-01-15
;

data want;
  set have;
  dt = input(char_date,b8601dt20.);
  date=datepart(dt);
  time=timepart(dt);
  format dt datetime19. date date9. time tod8.;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs    id    seq    char_date                            dt         date        time

  1    1      1     2018-04-25T11:26     25APR2018:11:26:00    25APR2018    11:26:00
  2    1      2     2018-04-25           25APR2018:00:00:00    25APR2018    00:00:00
  3    1      3     2018-04-26           26APR2018:00:00:00    26APR2018    00:00:00
  4    2      1     2017-09-30T06:23     30SEP2017:06:23:00    30SEP2017    06:23:00
  5    2      2     2017-10-05           05OCT2017:00:00:00    05OCT2017    00:00:00
  6    3      1     2018-03-17           17MAR2018:00:00:00    17MAR2018    00:00:00
  7    3      2     2018-03-17           17MAR2018:00:00:00    17MAR2018    00:00:00
  8    3      3     2018-04-02T22:00     02APR2018:22:00:00    02APR2018    22:00:00
  9    4      1     2017-12-04T13:41     04DEC2017:13:41:00    04DEC2017    13:41:00
 10    4      2     2017-12-08T09:20     08DEC2017:09:20:00    08DEC2017    09:20:00
 11    5      1     2018-01-15           15JAN2018:00:00:00    15JAN2018    00:00:00
&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 Nov 2021 15:34:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-Datetime-Character-to-Date-Format-with-Varying-Character/m-p/780762#M248797</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-11-17T15:34:30Z</dc:date>
    </item>
  </channel>
</rss>

