<?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: Converting date to numeric and separating values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-to-numeric-and-separating-values/m-p/574314#M162257</link>
    <description>&lt;P&gt;Out of experience:&lt;/P&gt;
&lt;P&gt;&lt;FONT size="6"&gt;&lt;STRONG&gt;DON'T!&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;There's functions for extracting the date parts (year(), month() and day()), and if you ever need a character representation, use one in ymd order, with or without hyphens (done with the e8601da and b8601da formats).&lt;/P&gt;</description>
    <pubDate>Wed, 17 Jul 2019 18:17:32 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-07-17T18:17:32Z</dc:date>
    <item>
      <title>Converting date to numeric and separating values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-to-numeric-and-separating-values/m-p/574303#M162251</link>
      <description>&lt;P&gt;I have a dataset where I use an informat of anydtdte21. and format MMDDYYs10. when I read in the file. The date field is displayed as something like 10/30/2008. I want to convert that to a character field that would look like 10302008 for a char_date field.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;After doing that, I want to pull out the month, day, and year, so I have separate fields (char_mo, char_day, char_yr) that would be like 10, 30, and 2008 for this example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've tried a few things with put, input, and the datepart function, but I can't seem to get anything to work. Any help is appreciated. Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jul 2019 17:36:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-to-numeric-and-separating-values/m-p/574303#M162251</guid>
      <dc:creator>wernie</dc:creator>
      <dc:date>2019-07-17T17:36:16Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date to numeric and separating values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-to-numeric-and-separating-values/m-p/574305#M162253</link>
      <description>&lt;P&gt;The whole idea of converting dates to characters really freaks me out, as I can not see any reasonable purpose to have this information as character strings. And so you might really want to think about why is this necessary, and perhaps come to the conclusion that it is not necessary.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The put command will convert numerics to characters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;chardate=put(numericdate,mmddyy8.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;From there it should be easy enough to get the month and year, if you really need that.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jul 2019 17:40:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-to-numeric-and-separating-values/m-p/574305#M162253</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-07-17T17:40:07Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date to numeric and separating values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-to-numeric-and-separating-values/m-p/574306#M162254</link>
      <description>You rarely need to do this in SAS, especially as characters. You're of course aware that months and days will sort as 1, 11, 12, 13...19, 2, 20, 21 etc. because that's the character order?</description>
      <pubDate>Wed, 17 Jul 2019 17:45:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-to-numeric-and-separating-values/m-p/574306#M162254</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-07-17T17:45:08Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date to numeric and separating values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-to-numeric-and-separating-values/m-p/574307#M162255</link>
      <description>&lt;P&gt;First point I want to make is that it is a mistake to store you character version of your dates using style of MMDDYYYY. They won't sort properly.&amp;nbsp; They are easily confused with values in DDMMYYYY order instead.&amp;nbsp; If you are storing dates as characters use YYYYMMDD order so that they sort and you don't confuse half your audience about what date you mean.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use the PUT() function to convert a value to a string.&amp;nbsp; Just use the format you want to display it with.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;date_char=put(date_val,yymmddn8.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To get the day, month, year part of a date use the DAY(), MONTH() and YEAR() function.&amp;nbsp; But there are also DAY. , MONTH. and YEAR. formats.&amp;nbsp; The problem with the formats is they do not add the leading zero.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  date_val=today();
  format date_val date9.;
  date_char=put(date_val,yymmddn8.);
  day_val=day(date_val);
  month_val=month(date_val);
  year_val=year(date_val);
  day_char=put(day_val,z2.);
  month_char=put(month_val,z2.);
  year_char=put(year_val,z4.);
  output;
  day_char=put(date_val,day2.);
  month_char=put(date_val,month2.);
  year_char=put(date_val,year4.);
  output;
run;
proc print; run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                     date_                 month_                            month_    year_
Obs     date_val      char      day_val      val     year_val    day_char     char     char

 1     17JUL2019    20190717       17         7        2019         17         07      2019
 2     17JUL2019    20190717       17         7        2019         17          7      2019
&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 Jul 2019 17:47:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-to-numeric-and-separating-values/m-p/574307#M162255</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-17T17:47:59Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date to numeric and separating values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-to-numeric-and-separating-values/m-p/574314#M162257</link>
      <description>&lt;P&gt;Out of experience:&lt;/P&gt;
&lt;P&gt;&lt;FONT size="6"&gt;&lt;STRONG&gt;DON'T!&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;There's functions for extracting the date parts (year(), month() and day()), and if you ever need a character representation, use one in ymd order, with or without hyphens (done with the e8601da and b8601da formats).&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jul 2019 18:17:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-to-numeric-and-separating-values/m-p/574314#M162257</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-17T18:17:32Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date to numeric and separating values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-to-numeric-and-separating-values/m-p/574332#M162265</link>
      <description>&lt;P&gt;Thanks everyone! I was replicating a process that someone else used and they did that, so I was trying to do the same thing. I'm not entirely sure why they did that though as it looks like they ended up putting it back into a regular date and just used the format that I used way back when I imported the data. I'll keep looking through to see if it's really necessary, as mentioned.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jul 2019 19:16:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-to-numeric-and-separating-values/m-p/574332#M162265</guid>
      <dc:creator>wernie</dc:creator>
      <dc:date>2019-07-17T19:16:25Z</dc:date>
    </item>
  </channel>
</rss>

