<?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 Help with datapart and timepart function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574589#M162392</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was wondering if anyoen could help me with the datepart function. I have tried a few things I've seen in the message boards, with no success.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The variable 'birth_date' is set up like this &amp;nbsp; 09APR62:23:59:59 (it is numeric and the format is DATETIME18.)- this is one example but there are about 10,000 records all with different birth dates&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In this example I want only the year (62) and to be changed to 1962. I am not sure how to isolate just this number and also add the 19 in front of it for every row.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Thank you!&lt;/P&gt;</description>
    <pubDate>Thu, 18 Jul 2019 14:41:01 GMT</pubDate>
    <dc:creator>sashelp123</dc:creator>
    <dc:date>2019-07-18T14:41:01Z</dc:date>
    <item>
      <title>Help with datapart and timepart function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574589#M162392</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was wondering if anyoen could help me with the datepart function. I have tried a few things I've seen in the message boards, with no success.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The variable 'birth_date' is set up like this &amp;nbsp; 09APR62:23:59:59 (it is numeric and the format is DATETIME18.)- this is one example but there are about 10,000 records all with different birth dates&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In this example I want only the year (62) and to be changed to 1962. I am not sure how to isolate just this number and also add the 19 in front of it for every row.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jul 2019 14:41:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574589#M162392</guid>
      <dc:creator>sashelp123</dc:creator>
      <dc:date>2019-07-18T14:41:01Z</dc:date>
    </item>
    <item>
      <title>Re: Help with datapart and timepart function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574596#M162394</link>
      <description>&lt;P&gt;data foo;&lt;BR /&gt;format my_dt datetime.;&lt;BR /&gt;my_dt='09APR62:23:59:59'dt;&lt;BR /&gt;format my_date date9.;&lt;BR /&gt;my_date=datepart(my_dt);&lt;BR /&gt;year=put(my_date,year4.);&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc print data=foo; run;&lt;/P&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="table.png" style="width: 307px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/31118i572106955C3B16FB/image-size/large?v=v2&amp;amp;px=999" role="button" title="table.png" alt="table.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jul 2019 14:36:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574596#M162394</guid>
      <dc:creator>GraphGuy</dc:creator>
      <dc:date>2019-07-18T14:36:15Z</dc:date>
    </item>
    <item>
      <title>Re: Help with datapart and timepart function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574597#M162395</link>
      <description>&lt;P&gt;First thing to note is that there is a bug in (feature of?) the DATETIME format.&amp;nbsp; A width of 18 should be enough to display date with 9 digits and time with 8 and one for the colon.&amp;nbsp; But for some reason it instead uses only 7 characters for the date.&amp;nbsp; Change to using DATETIME19 or longer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want the year from a datetime value then you can nest call to DATEPART() function inside the call to YEAR() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;yob = year(datetime(birth_date));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Another thing to consider is to not use _DATE suffix on the names of variables that contain DATETIME values instead of DATE values.&amp;nbsp; It will just lead to confusion.&amp;nbsp; Use DATETIME or TIMESTAMP instead to make it clearer what type of data the variable contains.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jul 2019 14:37:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574597#M162395</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-18T14:37:19Z</dc:date>
    </item>
    <item>
      <title>Re: Help with datapart and timepart function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574598#M162396</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13585"&gt;@GraphGuy&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;data foo;&lt;BR /&gt;format my_dt datetime.;&lt;BR /&gt;my_dt='09APR62:23:59:59'dt;&lt;BR /&gt;format my_date date9.;&lt;BR /&gt;my_date=datepart(my_dt);&lt;BR /&gt;year=put(my_date,year4.);&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc print data=foo; run;&lt;/P&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="table.png" style="width: 307px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/31118i572106955C3B16FB/image-size/large?v=v2&amp;amp;px=999" role="button" title="table.png" alt="table.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Note that there is a DTYEAR format that can by applied directly to the datetime value to display just the year value.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jul 2019 14:40:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574598#M162396</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-18T14:40:00Z</dc:date>
    </item>
    <item>
      <title>Re: Help with datapart and timepart function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574599#M162397</link>
      <description>&lt;P&gt;thank you that fixed up the first row, but sorry a detail that I failed to mention is that there are thousands of records all with different dates, so the first row was properly changed to 1962, but I need it to part out the date for every row if that's possible&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jul 2019 14:40:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574599#M162397</guid>
      <dc:creator>sashelp123</dc:creator>
      <dc:date>2019-07-18T14:40:10Z</dc:date>
    </item>
    <item>
      <title>Re: Help with datapart and timepart function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574600#M162398</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/169209"&gt;@sashelp123&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;thank you that fixed up the first row, but sorry a detail that I failed to mention is that there are thousands of records all with different dates, so the first row was properly changed to 1962, but I need it to part out the date for every row if that's possible&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Show what code you actually ran.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jul 2019 14:41:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574600#M162398</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-18T14:41:10Z</dc:date>
    </item>
    <item>
      <title>Re: Help with datapart and timepart function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574603#M162400</link>
      <description>&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; foo;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; xxx;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;format&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; my_dt &lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;datetime.&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;my_dt=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;'09APR62:23:59:59'dt&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;format&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; my_date &lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;date9.&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;my_date=datepart(my_dt);&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;year=put(my_date,&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;year4.&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&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, 18 Jul 2019 14:50:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574603#M162400</guid>
      <dc:creator>sashelp123</dc:creator>
      <dc:date>2019-07-18T14:50:10Z</dc:date>
    </item>
    <item>
      <title>Re: Help with datapart and timepart function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574604#M162401</link>
      <description>&lt;P&gt;Hi thank you for your reply! I am still learning and I'm not sure how to include your code in a statement, like what the full code would like like to do that? thank you&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jul 2019 14:51:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574604#M162401</guid>
      <dc:creator>sashelp123</dc:creator>
      <dc:date>2019-07-18T14:51:35Z</dc:date>
    </item>
    <item>
      <title>Re: Help with datapart and timepart function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574614#M162404</link>
      <description>&lt;P&gt;So assuming you have a dataset named HAVE and a variable named BIRTH_DATE the first thing to check is whether the variables already have the right century or not.&amp;nbsp; You could try seeing what the distribution of values is by using PROC FREQ.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=HAVE;
  tables BIRTH_DATE;
  format BIRTH_DATE dtyear4.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to make a copy of your data and in the process fix the format attached you can use a simple data step like this.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT;
  set HAVE;
  format BIRTH_DATE datetime19.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to fix the format attached to your existing dataset you can just use PROC DATASETS to update the dataset without having to make a copy of the data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc datasets lib=WORK nolist;
  modify HAVE ;
    format BIRTH_DATE datetime19.;
  run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jul 2019 15:00:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574614#M162404</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-18T15:00:37Z</dc:date>
    </item>
    <item>
      <title>Re: Help with datapart and timepart function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574620#M162405</link>
      <description>&lt;P&gt;ok thank you! so I did that and changed the format to 19, so now how do I create a variable (year) that is just 19 plus the 2 values for the end year? you mentioned yob=year(datetime(birth_date)), does that go in a data step, or how do I make that change? thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;Data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; date_clean;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; want;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;yob = year(datetime(birth_date));&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jul 2019 15:12:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574620#M162405</guid>
      <dc:creator>sashelp123</dc:creator>
      <dc:date>2019-07-18T15:12:06Z</dc:date>
    </item>
    <item>
      <title>Re: Help with datapart and timepart function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574621#M162406</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/169209"&gt;@sashelp123&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;ok thank you! so I did that and changed the format to 19, so now how do I create a variable (year) that is just 19 plus the 2 values for the end year? you mentioned yob=year(datetime(birth_date)), does that go in a data step, or how do I make that change? thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="2" color="#000080"&gt;&lt;STRONG&gt;Data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; date_clean;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="2" color="#0000ff"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; want;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;yob = year(datetime(birth_date));&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="2" color="#000080"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That code will create a new variable named YOB that has the year value from the datetime variable BIRTH_DATE.&lt;/P&gt;
&lt;P&gt;Is that what you want?&amp;nbsp; I don't understand what you mean by "end year".&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jul 2019 15:14:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574621#M162406</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-18T15:14:36Z</dc:date>
    </item>
    <item>
      <title>Re: Help with datapart and timepart function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574627#M162411</link>
      <description>&lt;P&gt;oh I'm not sure why I said 'end year' I just meant year. So currently in the date it is just showing for example '62' for the year, but I want it to change to 1962. Also when I put that code into sas I seem to be getting an error message.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;126 Data date_clean;&lt;/P&gt;&lt;P&gt;127 set want;&lt;/P&gt;&lt;P&gt;128 yob = year(datetime(birth_date));&lt;/P&gt;&lt;P&gt;--------&lt;/P&gt;&lt;P&gt;72&lt;/P&gt;&lt;P&gt;ERROR 72-185: The DATETIME function call has too many arguments.&lt;/P&gt;&lt;P&gt;129 run;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jul 2019 15:19:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574627#M162411</guid>
      <dc:creator>sashelp123</dc:creator>
      <dc:date>2019-07-18T15:19:08Z</dc:date>
    </item>
    <item>
      <title>Re: Help with datapart and timepart function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574631#M162414</link>
      <description>The function is DATEPART() not DATETIME().&lt;BR /&gt;The DATETIME() function returns the current datetime value from the computer's clock.&lt;BR /&gt;</description>
      <pubDate>Thu, 18 Jul 2019 15:25:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574631#M162414</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-18T15:25:37Z</dc:date>
    </item>
    <item>
      <title>Re: Help with datapart and timepart function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574634#M162417</link>
      <description>&lt;P&gt;Success, thank you very much! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jul 2019 15:32:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-datapart-and-timepart-function/m-p/574634#M162417</guid>
      <dc:creator>sashelp123</dc:creator>
      <dc:date>2019-07-18T15:32:11Z</dc:date>
    </item>
  </channel>
</rss>

