<?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: extract date from datetime with datepart in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533641#M146333</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/219549"&gt;@zimcom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15043"&gt;@ScottBass&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much, Scott&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your program works only if the variable is TEXT, right? But in my original dataset, &lt;SPAN&gt;the format of variable is&amp;nbsp;DATETIME. and informat is&amp;nbsp;ANYDTDTM40.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I appreciated your help! &lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;ANYDTDTM informat uses a lot of assumptions. In this case the assumption in a datetime appearing value is that the first value is the day of the month not the year. And again, did no one learn from Y2K that two-digit years are a bad idea?&lt;/P&gt;
&lt;P&gt;So read the original value as text, not the likely assigned by proc import ANYDATEDTM informat and then parse as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15043"&gt;@ScottBass&lt;/a&gt; suggests. Or other parsing technique.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This example shows that ANYDTDTM does not want to see a year in the first position at all:&lt;/P&gt;
&lt;PRE&gt;data junk;
   x='17APR04:15:54:05';
   y='2017APR04:15:54:05';

   datex= input(x,anydtdtm40.);
   datey= input(y,anydtdtm40.);
   format datex datey datetime20.;
run;&lt;/PRE&gt;</description>
    <pubDate>Thu, 07 Feb 2019 16:12:28 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2019-02-07T16:12:28Z</dc:date>
    <item>
      <title>extract date from datetime with datepart</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533463#M146258</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a column od datetime as below:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;DFCREATE&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;17APR04:15:54:05&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;17APR04:15:56:09&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;17APR05:15:36:48&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;17APR05:15:43:17&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;17APR27:16:30:15&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;17AUG25:09:51:58&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;17APR10:14:04:58&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;17APR10:14:07:37&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;17APR10:14:11:16&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;17APR21:12:27:26&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;17JUN17:12:52:39&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;17APR19:11:55:46&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;17SEP28:17:28:47&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;17JUN01:16:23:07&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;17JUN01:16:24:30&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;17AUG08:15:59:43&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;17AUG08:16:00:54&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and I would like ot extract the date part, but when I use&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;creat_date = datepart(DFCREATE);&lt;BR /&gt;format creat_date is8601da.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;new_date&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2004-04-17&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2004-04-17&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2005-04-17&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2005-04-17&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1927-04-17&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2025-08-17&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2010-04-17&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2010-04-17&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2010-04-17&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2021-04-17&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2017-06-17&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2019-04-17&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the converted date is just wrong, the first observation should be 2017-04-04, not 2004-04-17.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas?&amp;nbsp; Thank you so much&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>Thu, 07 Feb 2019 02:18:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533463#M146258</guid>
      <dc:creator>zimcom</dc:creator>
      <dc:date>2019-02-07T02:18:21Z</dc:date>
    </item>
    <item>
      <title>Re: extract date from datetime with datepart</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533469#M146262</link>
      <description>&lt;P&gt;So you have your dates as&amp;nbsp;&lt;/P&gt;
&lt;P&gt;yyMONdd?&lt;/P&gt;
&lt;P&gt;That's a very unusual format.&lt;/P&gt;
&lt;P&gt;Are you sure? The standard is ddMONyy.&lt;/P&gt;
&lt;P&gt;There is no informat in SAS for yyMONdd, so you'll have to parse the string manually if that's the case.&lt;/P&gt;
&lt;P&gt;Of course 4-digit years would resolve any doubt, but Y2k is so far away...&lt;/P&gt;</description>
      <pubDate>Thu, 07 Feb 2019 02:45:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533469#M146262</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-02-07T02:45:01Z</dc:date>
    </item>
    <item>
      <title>Re: extract date from datetime with datepart</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533508#M146279</link>
      <description>&lt;P&gt;As Chris said, that's an unusual format.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regardless, I thought it might be a fun exercise to dust off my Perl regular expressions...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   length DFCREATE $30;
   input dfcreate;
   datalines;
17APR04:15:54:05
17APR04:15:56:09
17APR05:15:36:48
17APR05:15:43:17
17APR27:16:30:15
17AUG25:09:51:58
17APR10:14:04:58
17APR10:14:07:37
17APR10:14:11:16
17APR21:12:27:26
17JUN17:12:52:39
17APR19:11:55:46
17SEP28:17:28:47
17JUN01:16:23:07
17JUN01:16:24:30
17AUG08:15:59:43
17AUG08:16:00:54
;
run;

data want;
   set have;
   length date_buffer time_buffer $20;
   rx1=prxparse("s/^(\d{2})(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(\d{2}):(.*)/\3\2\1/io");
   rx2=prxparse("s/^(\d{2})(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(\d{2}):(.*)/\4/io");
   date_buffer=prxchange(rx1,1,strip(dfcreate));
   time_buffer=prxchange(rx2,1,strip(dfcreate));
   date=input(date_buffer,date7.);
   format date date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Feb 2019 04:49:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533508#M146279</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-02-07T04:49:13Z</dc:date>
    </item>
    <item>
      <title>Re: extract date from datetime with datepart</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533609#M146324</link>
      <description>&lt;P&gt;the date and time was system generated in the format of "17APR04:15:54:05".&lt;/P&gt;&lt;P&gt;I checked the clumn attribute, the format of variable is&amp;nbsp;DATETIME. and informat is&amp;nbsp;ANYDTDTM40.&lt;/P&gt;&lt;P&gt;I need to extarct and then convert the date portion to compare with another date (e.g., 2017-03-01).&lt;/P&gt;</description>
      <pubDate>Thu, 07 Feb 2019 15:12:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533609#M146324</guid>
      <dc:creator>zimcom</dc:creator>
      <dc:date>2019-02-07T15:12:31Z</dc:date>
    </item>
    <item>
      <title>Re: extract date from datetime with datepart</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533610#M146325</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15043"&gt;@ScottBass&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much, Scott&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your program works only if the variable is TEXT, right? But in my original dataset, &lt;SPAN&gt;the format of variable is&amp;nbsp;DATETIME. and informat is&amp;nbsp;ANYDTDTM40.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I appreciated your help! &lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Feb 2019 15:17:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533610#M146325</guid>
      <dc:creator>zimcom</dc:creator>
      <dc:date>2019-02-07T15:17:20Z</dc:date>
    </item>
    <item>
      <title>Re: extract date from datetime with datepart</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533641#M146333</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/219549"&gt;@zimcom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15043"&gt;@ScottBass&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much, Scott&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your program works only if the variable is TEXT, right? But in my original dataset, &lt;SPAN&gt;the format of variable is&amp;nbsp;DATETIME. and informat is&amp;nbsp;ANYDTDTM40.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I appreciated your help! &lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;ANYDTDTM informat uses a lot of assumptions. In this case the assumption in a datetime appearing value is that the first value is the day of the month not the year. And again, did no one learn from Y2K that two-digit years are a bad idea?&lt;/P&gt;
&lt;P&gt;So read the original value as text, not the likely assigned by proc import ANYDATEDTM informat and then parse as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15043"&gt;@ScottBass&lt;/a&gt; suggests. Or other parsing technique.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This example shows that ANYDTDTM does not want to see a year in the first position at all:&lt;/P&gt;
&lt;PRE&gt;data junk;
   x='17APR04:15:54:05';
   y='2017APR04:15:54:05';

   datex= input(x,anydtdtm40.);
   datey= input(y,anydtdtm40.);
   format datex datey datetime20.;
run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Feb 2019 16:12:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533641#M146333</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-02-07T16:12:28Z</dc:date>
    </item>
    <item>
      <title>Re: extract date from datetime with datepart</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533719#M146358</link>
      <description>&lt;P&gt;I have attached a sample file and I want to have another column with the new date as yyyy-mm-dd in date fomat so that I can calculate the days between 2 dates.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Feb 2019 20:14:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533719#M146358</guid>
      <dc:creator>zimcom</dc:creator>
      <dc:date>2019-02-07T20:14:45Z</dc:date>
    </item>
    <item>
      <title>Re: extract date from datetime with datepart</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533747#M146374</link>
      <description>&lt;P&gt;Your example file does not contain dates in Excel either, they are text. Import the field as TEXT, do not use the ANYDTDTM informat but a $20. or similar long enough. And then parse the text variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or go back to whoever created that file and ask why they are using a date format that practically no one else uses and see if they can export the data in a more typical format such as having a 4 digit year and a month number instead of three letter abbreviation.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Feb 2019 23:37:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533747#M146374</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-02-07T23:37:35Z</dc:date>
    </item>
    <item>
      <title>Re: extract date from datetime with datepart</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533779#M146391</link>
      <description>&lt;P&gt;Can you change the format to DATETIME21. or DATETIME21.2 and post the results?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
   dt1=datetime();
   dt2=dt1;
   dt3=dt1;
   format dt1 datetime. dt2 datetime18. dt3 datetime21.2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Feb 2019 23:26:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533779#M146391</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-02-07T23:26:54Z</dc:date>
    </item>
    <item>
      <title>Re: extract date from datetime with datepart</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533789#M146396</link>
      <description>&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;dt1&lt;/TD&gt;&lt;TD&gt;dt2&lt;/TD&gt;&lt;TD&gt;dt3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;07FEB19:19:09:41&lt;/TD&gt;&lt;TD&gt;07FEB19:19:09:41&lt;/TD&gt;&lt;TD&gt;07FEB2019:19:09:40.56&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Fri, 08 Feb 2019 00:11:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533789#M146396</guid>
      <dc:creator>zimcom</dc:creator>
      <dc:date>2019-02-08T00:11:15Z</dc:date>
    </item>
    <item>
      <title>Re: extract date from datetime with datepart</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533790#M146397</link>
      <description>&lt;P&gt;I used proc import to read in the csv file, after read in the date variable is date format.&lt;/P&gt;&lt;P&gt;The file was downloaded from the database and it seems like it is system date and time and it was created like this.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Feb 2019 00:15:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533790#M146397</guid>
      <dc:creator>zimcom</dc:creator>
      <dc:date>2019-02-08T00:15:15Z</dc:date>
    </item>
    <item>
      <title>Re: extract date from datetime with datepart</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533793#M146398</link>
      <description>&lt;P&gt;I meant from &lt;U&gt;&lt;STRONG&gt;your&lt;/STRONG&gt;&lt;/U&gt; data, the sample code was just to illustrate the different results, esp. with DATETIME21.&amp;nbsp; Apologies for the confusion.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Feb 2019 00:33:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533793#M146398</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-02-08T00:33:30Z</dc:date>
    </item>
    <item>
      <title>Re: extract date from datetime with datepart</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533795#M146399</link>
      <description>&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;usubjid&lt;/TD&gt;&lt;TD&gt;aestdtc&lt;/TD&gt;&lt;TD&gt;old_date&lt;/TD&gt;&lt;TD&gt;new_date&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1001&lt;/TD&gt;&lt;TD&gt;2017-03-02&lt;/TD&gt;&lt;TD&gt;17APR04:15:54:05&lt;/TD&gt;&lt;TD&gt;17APR2004:15:54:05.00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1001&lt;/TD&gt;&lt;TD&gt;2017-03-03&lt;/TD&gt;&lt;TD&gt;17APR04:15:56:09&lt;/TD&gt;&lt;TD&gt;17APR2004:15:56:09.00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1002&lt;/TD&gt;&lt;TD&gt;2017-03-01&lt;/TD&gt;&lt;TD&gt;17APR05:15:36:48&lt;/TD&gt;&lt;TD&gt;17APR2005:15:36:48.00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1002&lt;/TD&gt;&lt;TD&gt;2017-03-03&lt;/TD&gt;&lt;TD&gt;17APR05:15:43:17&lt;/TD&gt;&lt;TD&gt;17APR2005:15:43:17.00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1002&lt;/TD&gt;&lt;TD&gt;2017-03-04&lt;/TD&gt;&lt;TD&gt;17APR27:16:30:15&lt;/TD&gt;&lt;TD&gt;17APR1927:16:30:15.00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1002&lt;/TD&gt;&lt;TD&gt;2017-03-01&lt;/TD&gt;&lt;TD&gt;17AUG25:09:51:58&lt;/TD&gt;&lt;TD&gt;17AUG2025:09:51:58.00&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Fri, 08 Feb 2019 00:56:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533795#M146399</guid>
      <dc:creator>zimcom</dc:creator>
      <dc:date>2019-02-08T00:56:23Z</dc:date>
    </item>
    <item>
      <title>Re: extract date from datetime with datepart</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533799#M146400</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/219549"&gt;@zimcom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;usubjid&lt;/TD&gt;&lt;TD&gt;aestdtc&lt;/TD&gt;&lt;TD&gt;old_date&lt;/TD&gt;&lt;TD&gt;new_date&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1001&lt;/TD&gt;&lt;TD&gt;2017-03-02&lt;/TD&gt;&lt;TD&gt;17APR04:15:54:05&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="5" color="#FF0000"&gt;17APR2004&lt;/FONT&gt;:15:54:05.00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1001&lt;/TD&gt;&lt;TD&gt;2017-03-03&lt;/TD&gt;&lt;TD&gt;17APR04:15:56:09&lt;/TD&gt;&lt;TD&gt;17APR2004:15:56:09.00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1002&lt;/TD&gt;&lt;TD&gt;2017-03-01&lt;/TD&gt;&lt;TD&gt;17APR05:15:36:48&lt;/TD&gt;&lt;TD&gt;17APR2005:15:36:48.00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1002&lt;/TD&gt;&lt;TD&gt;2017-03-03&lt;/TD&gt;&lt;TD&gt;17APR05:15:43:17&lt;/TD&gt;&lt;TD&gt;17APR2005:15:43:17.00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1002&lt;/TD&gt;&lt;TD&gt;2017-03-04&lt;/TD&gt;&lt;TD&gt;17APR27:16:30:15&lt;/TD&gt;&lt;TD&gt;17APR1927:16:30:15.00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1002&lt;/TD&gt;&lt;TD&gt;2017-03-01&lt;/TD&gt;&lt;TD&gt;17AUG25:09:51:58&lt;/TD&gt;&lt;TD&gt;17AUG2025:09:51:58.00&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And your original post says:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;the converted date is just wrong, the first observation should be 2017-04-04, not &lt;FONT size="5" color="#FF0000"&gt;2004-04-17&lt;/FONT&gt;.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Feb 2019 01:30:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533799#M146400</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-02-08T01:30:37Z</dc:date>
    </item>
    <item>
      <title>Re: extract date from datetime with datepart</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533800#M146401</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/219549"&gt;@zimcom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;I have attached a sample file and I want to have another column &lt;STRONG&gt;with the new date as yyyy-mm-dd in date fomat&lt;/STRONG&gt; so that I can calculate the days between 2 dates.&amp;nbsp;&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As long as the date is stored as a date and not text, the format used to display it is irrelevant with respect to calculating the days between 2 dates.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Feb 2019 01:33:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533800#M146401</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-02-08T01:33:47Z</dc:date>
    </item>
    <item>
      <title>Re: extract date from datetime with datepart</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533801#M146402</link>
      <description>&lt;P&gt;Yes, you are right, it was converted wrong, the year should be 2017, that is where I was stopped&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;usubjid&lt;/TD&gt;&lt;TD&gt;aestdtc&lt;/TD&gt;&lt;TD&gt;old_date&lt;/TD&gt;&lt;TD&gt;new_date&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1001&lt;/TD&gt;&lt;TD&gt;2017-03-02&lt;/TD&gt;&lt;TD&gt;17APR04:15:54:05&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="5" color="#FF0000"&gt;17APR2004&lt;/FONT&gt;:15:54:05.00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1001&lt;/TD&gt;&lt;TD&gt;2017-03-03&lt;/TD&gt;&lt;TD&gt;17APR04:15:56:09&lt;/TD&gt;&lt;TD&gt;17APR2004:15:56:09.00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1002&lt;/TD&gt;&lt;TD&gt;2017-03-01&lt;/TD&gt;&lt;TD&gt;17APR05:15:36:48&lt;/TD&gt;&lt;TD&gt;17APR2005:15:36:48.00&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Fri, 08 Feb 2019 01:34:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533801#M146402</guid>
      <dc:creator>zimcom</dc:creator>
      <dc:date>2019-02-08T01:34:34Z</dc:date>
    </item>
    <item>
      <title>Re: extract date from datetime with datepart</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533802#M146403</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/219549"&gt;@zimcom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;Yes, you are right, it was converted wrong, the year should be 2017, that is where I was stopped&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Well that would be a different problem then.&amp;nbsp; Good luck, unsubscribing from this one...&lt;/P&gt;</description>
      <pubDate>Fri, 08 Feb 2019 01:36:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533802#M146403</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-02-08T01:36:47Z</dc:date>
    </item>
    <item>
      <title>Re: extract date from datetime with datepart</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533803#M146404</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15043"&gt;@ScottBass&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you, Scott for your help!&lt;/P&gt;</description>
      <pubDate>Fri, 08 Feb 2019 01:39:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533803#M146404</guid>
      <dc:creator>zimcom</dc:creator>
      <dc:date>2019-02-08T01:39:54Z</dc:date>
    </item>
    <item>
      <title>Re: extract date from datetime with datepart</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533809#M146406</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/219549"&gt;@zimcom&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your source data is a string in the form&amp;nbsp;YYMONDD:HH:MM:SS&lt;/P&gt;
&lt;P&gt;You used informat&amp;nbsp;&lt;SPAN&gt;ANYDTDTM40. to read this string and convert it to a SAS DateTime value (=numerical value; count of seconds since 1/1/1960).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;So now by using ANYDTDTM40. SAS will try to read the text string as&amp;nbsp;&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;DD&lt;/FONT&gt;&lt;/STRONG&gt;MON&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;YY&lt;/FONT&gt;&lt;/STRONG&gt;:HH:MM:SS - and because the text string (your data) also represents a valid datetime in this form SAS will use it and convert it to the number of seconds since 1/1/1960. Now you have a SAS DataTime value and whatever operations and formats you use on this value it's always against the "wrong" count of seconds.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;-&amp;gt; your current code gets it wrong when converting the source string into a SAS DateTime value.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Others wrote this already but here again:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;What you need to do is first to read the string into a SAS character variable and then "reshuffle" it into a form which a SAS datetime informat can interprete the way you need it. Below a code sample illustrating how this could work (for the date portion of the string).&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* read the excel sheet into SAS */
PROC IMPORT 
  OUT= source 
  DATAFILE= "c:\temp\Convert dates.xlsx" 
  DBMS=xlsx 
  REPLACE;
  SHEET="#LN00046";
  GETNAMES=YES;
RUN;

/*18DEC13:11:45:31*/
/* 
  extract the date string portion from column date_old
  and convert it to a SAS date value (number of seconds since 1/1/1960
  DT string is in the form: YYMONDD:HH:MM:SS
*/
data want;
  set source;
  length dt_string $7;
  format dt_SASDateValue is8601da.;
  dt_string       =cats(substrn(date_old,6,2),substrn(date_old,3,3),substrn(date_old,1,2));
  dt_SASDateValue =input(dt_string,?? date7.);
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Feb 2019 02:46:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/533809#M146406</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-02-08T02:46:38Z</dc:date>
    </item>
  </channel>
</rss>

