<?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 Extracting date from a character variable that might have partial date or datetime representation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extracting-date-from-a-character-variable-that-might-have/m-p/962267#M375066</link>
    <description>&lt;P&gt;In SQL I would like to&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;INPUT(datestr,?? ...some informat...) as DATE format DATE11.&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a singular informat that can deal with a variety of date representations, some of which may be incomplete ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Consider sample data and output&lt;/P&gt;
&lt;PRE&gt;options nocenter nodate nonumber;title;

data x;
  attrib 
    datestr length=$30 
    date length=8 format=date11.     label='ANYDTDTE19.'
    dtim length=8 format=datetime19. label='ANYDTDTM19.'
    dtme length=8 format=datetime19. label='E8601DT19.'
  ;
  input 
    @1 datestr $CHAR30. 
    @1 date anydtdte19. 
    @1 dtim anydtdtm19.
    @1 dtme E8601DT19.
  ;
  datalines ;
12/01/2017
12/3/17
1/5/18T12:34:56
1/5/18 12:34:56
2021-12-07
2021-12-07T10
2021-12-07T11:08
2021-12-07T12:37:25
2021-12-08 12
2021-12-08 13:08
2021-12-08 14:37:25
2022
2023-02
20211207
20211207T10
20211207T11:08
20211207T12:37:25
20211208 12
20211208 13:08
20211208 14:37:25
;

proc print label ;
  title "option DATESTYLE=%sysfunc(getoption(DATESTYLE))";
  title2 "date values output with date11. datetime with datetime19." ;
  title3 "column label is the informat used against [datestr]" ;
run;&lt;/PRE&gt;
&lt;PRE&gt;option DATESTYLE=MDY
date values output with date11. datetime with datetime19.
column label is the informat used against [datestr]

Obs    datestr                ANYDTDTE19.            ANYDTDTM19.             E8601DT19.

  1    12/01/2017             01-DEC-2017     01DEC2017:00:00:00                      .
  2    12/3/17                03-DEC-2017     03DEC2017:00:00:00                      .
  3    1/5/18T12:34:56                  .                      .                      .
  4    1/5/18 12:34:56        05-JAN-2018     05JAN2018:12:34:56                      .
  5    2021-12-07             07-DEC-2021     07DEC2021:00:00:00                      .
  6    2021-12-07T10                    .                      .                      .
  7    2021-12-07T11:08                 .                      .     07DEC2021:11:08:00
  8    2021-12-07T12:37:25    07-DEC-2021     07DEC2021:12:37:25     07DEC2021:12:37:25
  9    2021-12-08 12                    .                      .                      .
 10    2021-12-08 13:08       08-DEC-2021     08DEC2021:13:08:00     08DEC2021:13:08:00
 11    2021-12-08 14:37:25    08-DEC-2021     08DEC2021:14:37:25     08DEC2021:14:37:25
 12    2022                             .                      .                      .
 13    2023-02                01-FEB-2023     01FEB2023:00:00:00                      .
 14    20211207               07-DEC-2021     07DEC2021:00:00:00                      .
 15    20211207T10                      .                      .                      .
 16    20211207T11:08                   .                      .                      .
 17    20211207T12:37:25                .                      .                      .
 18    20211208 12                      .                      .                      .
 19    20211208 13:08         08-DEC-2021     08DEC2021:13:08:00                      .
 20    20211208 14:37:25      08-DEC-2021     08DEC2021:14:37:25                      .&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there some informat better than ANYDTDTM or ANYDTDTE?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In SQL to use a variety of inputting attempts would COALESCE (input-way-1, input-way-2, ... input-way-n) be plausible (while trying to get cpu use down)&lt;/P&gt;</description>
    <pubDate>Wed, 19 Mar 2025 18:22:09 GMT</pubDate>
    <dc:creator>RichardAD</dc:creator>
    <dc:date>2025-03-19T18:22:09Z</dc:date>
    <item>
      <title>Extracting date from a character variable that might have partial date or datetime representation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-date-from-a-character-variable-that-might-have/m-p/962267#M375066</link>
      <description>&lt;P&gt;In SQL I would like to&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;INPUT(datestr,?? ...some informat...) as DATE format DATE11.&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a singular informat that can deal with a variety of date representations, some of which may be incomplete ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Consider sample data and output&lt;/P&gt;
&lt;PRE&gt;options nocenter nodate nonumber;title;

data x;
  attrib 
    datestr length=$30 
    date length=8 format=date11.     label='ANYDTDTE19.'
    dtim length=8 format=datetime19. label='ANYDTDTM19.'
    dtme length=8 format=datetime19. label='E8601DT19.'
  ;
  input 
    @1 datestr $CHAR30. 
    @1 date anydtdte19. 
    @1 dtim anydtdtm19.
    @1 dtme E8601DT19.
  ;
  datalines ;
12/01/2017
12/3/17
1/5/18T12:34:56
1/5/18 12:34:56
2021-12-07
2021-12-07T10
2021-12-07T11:08
2021-12-07T12:37:25
2021-12-08 12
2021-12-08 13:08
2021-12-08 14:37:25
2022
2023-02
20211207
20211207T10
20211207T11:08
20211207T12:37:25
20211208 12
20211208 13:08
20211208 14:37:25
;

proc print label ;
  title "option DATESTYLE=%sysfunc(getoption(DATESTYLE))";
  title2 "date values output with date11. datetime with datetime19." ;
  title3 "column label is the informat used against [datestr]" ;
run;&lt;/PRE&gt;
&lt;PRE&gt;option DATESTYLE=MDY
date values output with date11. datetime with datetime19.
column label is the informat used against [datestr]

Obs    datestr                ANYDTDTE19.            ANYDTDTM19.             E8601DT19.

  1    12/01/2017             01-DEC-2017     01DEC2017:00:00:00                      .
  2    12/3/17                03-DEC-2017     03DEC2017:00:00:00                      .
  3    1/5/18T12:34:56                  .                      .                      .
  4    1/5/18 12:34:56        05-JAN-2018     05JAN2018:12:34:56                      .
  5    2021-12-07             07-DEC-2021     07DEC2021:00:00:00                      .
  6    2021-12-07T10                    .                      .                      .
  7    2021-12-07T11:08                 .                      .     07DEC2021:11:08:00
  8    2021-12-07T12:37:25    07-DEC-2021     07DEC2021:12:37:25     07DEC2021:12:37:25
  9    2021-12-08 12                    .                      .                      .
 10    2021-12-08 13:08       08-DEC-2021     08DEC2021:13:08:00     08DEC2021:13:08:00
 11    2021-12-08 14:37:25    08-DEC-2021     08DEC2021:14:37:25     08DEC2021:14:37:25
 12    2022                             .                      .                      .
 13    2023-02                01-FEB-2023     01FEB2023:00:00:00                      .
 14    20211207               07-DEC-2021     07DEC2021:00:00:00                      .
 15    20211207T10                      .                      .                      .
 16    20211207T11:08                   .                      .                      .
 17    20211207T12:37:25                .                      .                      .
 18    20211208 12                      .                      .                      .
 19    20211208 13:08         08-DEC-2021     08DEC2021:13:08:00                      .
 20    20211208 14:37:25      08-DEC-2021     08DEC2021:14:37:25                      .&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there some informat better than ANYDTDTM or ANYDTDTE?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In SQL to use a variety of inputting attempts would COALESCE (input-way-1, input-way-2, ... input-way-n) be plausible (while trying to get cpu use down)&lt;/P&gt;</description>
      <pubDate>Wed, 19 Mar 2025 18:22:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-date-from-a-character-variable-that-might-have/m-p/962267#M375066</guid>
      <dc:creator>RichardAD</dc:creator>
      <dc:date>2025-03-19T18:22:09Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting date from a character variable that might have partial date or datetime representatio</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-date-from-a-character-variable-that-might-have/m-p/962269#M375067</link>
      <description>&lt;P&gt;The problem caused by the letter T is easy to fix, just replace the letter T with a space (use the TRANSLATE function).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The other problems caused by just having a year, or just having an hour, require you to write your own code to deal with those.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if length(datestr)=4 and notdigit(datestr)=0 then dateval=mdy(1,1,input(datestr,4.));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Mar 2025 20:04:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-date-from-a-character-variable-that-might-have/m-p/962269#M375067</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-03-19T20:04:52Z</dc:date>
    </item>
  </channel>
</rss>

