<?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/879993#M347654</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/145127"&gt;@lih&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have same problem. My date variable is&amp;nbsp;01JAN1960:01:47:52 (datetime20.). I tried datepart(date) and read day,month,year using substr, both not work, not sure why. Anybody has solution?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;For the DATEPART() function to work the variable needs to NUMERIC with times since 1960 in seconds.&lt;/P&gt;
&lt;P&gt;For the SUBSTR() function to work the value needs to be CHARACTER.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You cannot use them BOTH on the same variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have a variable named DATETIME that has DATETIME values then you can make DATE, YEAR, MONTH and DAY variables like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;date=datepart(datetime);
year=year(date);
month=month(date);
day=day(month);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 10 Jun 2023 04:28:12 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-06-10T04:28:12Z</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/490762#M128500</link>
      <description>&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set original;&lt;/P&gt;&lt;P&gt;date1 = darepart(date);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my date column looks like 01AUG18:18:47:42&lt;/P&gt;&lt;P&gt;and date1 column is changed into 21397 with numeric type.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;how colud I extract date into&amp;nbsp;&lt;SPAN&gt;01AUG18 with date type?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Aug 2018 09:08:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/490762#M128500</guid>
      <dc:creator>Geo-</dc:creator>
      <dc:date>2018-08-29T09:08:57Z</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/490764#M128502</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set original;
date1 = datepart(date);
format date1 date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Add &lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000195834.htm" target="_self"&gt;date9. format&lt;/A&gt; to your date1 variable. Remember, dates are just formatted numbers in SAS. A Date in SAS represents the number of days since 01Jan1960.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Aug 2018 09:22:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/490764#M128502</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-08-29T09:22: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/618266#M181349</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can any one help me with Extracting only date part from datetime with full program.&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jan 2020 16:19:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/618266#M181349</guid>
      <dc:creator>Jeetesh</dc:creator>
      <dc:date>2020-01-18T16:19:05Z</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/618272#M181352</link>
      <description>&lt;P&gt;Does not the above solution to the problem meet your needs? If not explain in detail.&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jan 2020 17:23:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/618272#M181352</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-01-18T17:23: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/879991#M347652</link>
      <description>&lt;P&gt;I have same problem. My date variable is&amp;nbsp;01JAN1960:01:47:52 (datetime20.). I tried datepart(date) and read day,month,year using substr, both not work, not sure why. Anybody has solution?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Sat, 10 Jun 2023 03:09:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/879991#M347652</guid>
      <dc:creator>lih</dc:creator>
      <dc:date>2023-06-10T03:09:55Z</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/879992#M347653</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/145127"&gt;@lih&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have same problem. My date variable is&amp;nbsp;01JAN1960:01:47:52 (datetime20.). I tried datepart(date) and read day,month,year using substr, both not work, not sure why. Anybody has solution?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Run Proc Contents on your data set. Show us the result. Make sure do indicate which variable is the one in question.&lt;/P&gt;
&lt;P&gt;If you don't know how&amp;nbsp; to run proc contents it is very simple, place the name of your data set after the data= in the example code below:&lt;/P&gt;
&lt;PRE&gt;proc contents data=yourdatasetname;
run;&lt;/PRE&gt;</description>
      <pubDate>Sat, 10 Jun 2023 04:22:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/879992#M347653</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-06-10T04:22:08Z</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/879993#M347654</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/145127"&gt;@lih&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have same problem. My date variable is&amp;nbsp;01JAN1960:01:47:52 (datetime20.). I tried datepart(date) and read day,month,year using substr, both not work, not sure why. Anybody has solution?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;For the DATEPART() function to work the variable needs to NUMERIC with times since 1960 in seconds.&lt;/P&gt;
&lt;P&gt;For the SUBSTR() function to work the value needs to be CHARACTER.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You cannot use them BOTH on the same variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have a variable named DATETIME that has DATETIME values then you can make DATE, YEAR, MONTH and DAY variables like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;date=datepart(datetime);
year=year(date);
month=month(date);
day=day(month);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 10 Jun 2023 04:28:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/879993#M347654</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-10T04:28:12Z</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/918206#M361692</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/307736"&gt;@Jeetesh&lt;/a&gt;&amp;nbsp;PeterClemmensen has given the full program which is extracting only date part.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am copy pasting his programme below and I will share what each step means.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;/*Here you are creating the dataset which you want to create*/&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;set original;&lt;/P&gt;
&lt;P&gt;/*Here you are reading the data which has this date variable*/&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;date1 = datepart(date);&lt;/P&gt;
&lt;P&gt;/*Here you are extracting date part of the date in a variable named date1*/&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;format date1 date9.;&lt;/P&gt;
&lt;P&gt;/*However, it will be sas date value, so to bring it in human readable form this date9 format it used*/&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Feel free to let us know if anything of this is not understood.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;- Dr. Abhijeet Safai&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Feb 2024 13:01:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/918206#M361692</guid>
      <dc:creator>DrAbhijeetSafai</dc:creator>
      <dc:date>2024-02-28T13:01: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/918691#M361890</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/145127"&gt;@lih&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have same problem. My date variable is&amp;nbsp;01JAN1960:01:47:52 (datetime20.). I tried datepart(date) and read day,month,year using substr, both not work, not sure why. Anybody has solution?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Important point: if something does not work, we don't know what you did. You need to show us your code, if there are errors in the log, show us the entire log for this DATA step; if it works but gives an answer you think is wrong, show us your code and show the wrong answer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Wild guess:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You did not assign a format to the variable created by &lt;FONT face="courier new,courier"&gt;datepart(date)&lt;/FONT&gt;, and it returns an answer of zero, which is the correct answer. You need to assign a DATE9. format to this variable to show the dates in "human readable" form which is 01JAN1960.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that's not it, provide the information I asked for.&lt;/P&gt;</description>
      <pubDate>Sat, 02 Mar 2024 17:12:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-date-from-datetime-with-datepart/m-p/918691#M361890</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-03-02T17:12:01Z</dc:date>
    </item>
  </channel>
</rss>

