<?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 Date9. numeric variable to mmddyyyy date variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884360#M349376</link>
    <description>&lt;P&gt;Hi, thank you so much. How would I put the date9. format into string style?&lt;/P&gt;</description>
    <pubDate>Tue, 11 Jul 2023 16:14:40 GMT</pubDate>
    <dc:creator>RACHEL2425</dc:creator>
    <dc:date>2023-07-11T16:14:40Z</dc:date>
    <item>
      <title>Converting Date9. numeric variable to mmddyyyy date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884335#M349358</link>
      <description>&lt;P&gt;Hi everyone,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am currently trying to sort a dataset based on date, and it wont sort correctly as the current date format is a numeric date9. format.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do i convert this variable to a date variable? Please note that I do not want to just change the format, I want to either create a new date variable based on the date9. variable or convert the variable altogether.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Rachel&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jul 2023 14:12:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884335#M349358</guid>
      <dc:creator>RACHEL2425</dc:creator>
      <dc:date>2023-07-11T14:12:30Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Date9. numeric variable to mmddyyyy date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884345#M349365</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;If your date variable type is numeric then it should sort correctly, regardless the format used.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if your variable type is character then you need to convert it into numeric, using INPUT() function and assigning the desired format (eg. DATE9. );&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set&amp;nbsp;have;&lt;BR /&gt;date1=&amp;nbsp;input(date,&amp;nbsp;mmddyy10.);&lt;BR /&gt;format&amp;nbsp;date1&amp;nbsp;mmddyy10.;&lt;BR /&gt;run;&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jul 2023 14:46:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884345#M349365</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2023-07-11T14:46:10Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Date9. numeric variable to mmddyyyy date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884349#M349368</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;current date format is a numeric date9. format.&amp;nbsp;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Assuming the above is correct, then &lt;STRONG&gt;the method of changing it to MMDDYYYY is to change the format&lt;/STRONG&gt;.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;format varname mmddyy10.;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you change it to a character variable it will not sort/order correctly and cannot extract the day/time components as necessary.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a great, but longer and in depth, reference for dates and times in SAS&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/ta-p/424354" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/ta-p/424354&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jul 2023 14:59:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884349#M349368</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-07-11T14:59:19Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Date9. numeric variable to mmddyyyy date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884357#M349375</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/426682"&gt;@RACHEL2425&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi everyone,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am currently trying to sort a dataset based on date, and it wont sort correctly as the current date format is a numeric date9. format.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do i convert this variable to a date variable? Please note that I do not want to just change the format, I want to either create a new date variable based on the date9. variable or convert the variable altogether.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Rachel&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Something does not make sense about your description of the problem.&lt;/P&gt;
&lt;P&gt;If you sort by a NUMERIC variable that has the DATE9. format attached to it then the values will sort by actual values (number of days) and not the displayed text (ddMONyyyy).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How are you sorting?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also if you change the display format to something like the MMDDYYYY mention in the subject line (did you mean use the MMDDYYN8. format?) then it still will not sort properly.&amp;nbsp; Only if you put the date value into strings in YYYYMMDD style will the resulting string sort properly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jul 2023 15:40:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884357#M349375</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-07-11T15:40:10Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Date9. numeric variable to mmddyyyy date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884360#M349376</link>
      <description>&lt;P&gt;Hi, thank you so much. How would I put the date9. format into string style?&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jul 2023 16:14:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884360#M349376</guid>
      <dc:creator>RACHEL2425</dc:creator>
      <dc:date>2023-07-11T16:14:40Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Date9. numeric variable to mmddyyyy date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884361#M349377</link>
      <description>date_character = put(date_var, mmddyy8.);&lt;BR /&gt;&lt;BR /&gt;However, this will definitely not sort if you have multiple years.</description>
      <pubDate>Tue, 11 Jul 2023 16:29:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884361#M349377</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-07-11T16:29:38Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Date9. numeric variable to mmddyyyy date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884363#M349379</link>
      <description>&lt;P&gt;To create a string using a format use the PUT() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  date_string = put(date_number,date9.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To create a string using whatever format is attached to the existing variable use the VVALUE() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  date_string = vvalue(date_number);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Jul 2023 16:31:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884363#M349379</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-07-11T16:31:14Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Date9. numeric variable to mmddyyyy date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884368#M349381</link>
      <description>&lt;P&gt;I unfortunately have multiple years.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jul 2023 16:49:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884368#M349381</guid>
      <dc:creator>RACHEL2425</dc:creator>
      <dc:date>2023-07-11T16:49:04Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Date9. numeric variable to mmddyyyy date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884369#M349382</link>
      <description>&lt;P&gt;Hi, again! thank you. it created the character string date. However, i have multiple years and the sort function does not appear to work.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jul 2023 16:57:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884369#M349382</guid>
      <dc:creator>RACHEL2425</dc:creator>
      <dc:date>2023-07-11T16:57:44Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Date9. numeric variable to mmddyyyy date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884370#M349383</link>
      <description>Then just applying the format as in my initial answer should allow your data to sort correctly. If it's not, please show your code, log and a screenshot of the sorted data that is incorrect so we can help you debug the issue.</description>
      <pubDate>Tue, 11 Jul 2023 16:59:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884370#M349383</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-07-11T16:59:07Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Date9. numeric variable to mmddyyyy date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884371#M349384</link>
      <description>&lt;P&gt;This issue with this is that it only changes the format, not the informat.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jul 2023 17:07:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884371#M349384</guid>
      <dc:creator>RACHEL2425</dc:creator>
      <dc:date>2023-07-11T17:07:12Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Date9. numeric variable to mmddyyyy date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884374#M349386</link>
      <description>&lt;P&gt;Informat is not relevant here. To sort, you want numeric variables (not character variables), the format is also irrelevant, numeric values that have DATE9. format will still sort properly. Example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data fake;
    date='01APR2019'd;
    output;
    date='01JAN2019'd;
    output;
    format date date9.;
run;
proc sort data=fake out=fake1;
    by date;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jul 2023 18:18:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884374#M349386</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-07-11T18:18:51Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Date9. numeric variable to mmddyyyy date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884376#M349388</link>
      <description>&lt;P&gt;For sorting you should keep the values as actual dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's make an example with a few date values and variables with different TYPES and different attached display formats.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input date1 :date. ;
  date2 = date1 ;
  format date1 date9. date2 yymmdd10. ;
  string1 = put(date1,date9.);
  string2 = put(date1,mmddyy10.);
  string3 = put(date1,yymmdd10.);
cards;
01JAN2023
03OCT2022
10MAR2021
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If we sort by either of the two actual date variables we get:&lt;/P&gt;
&lt;PRE&gt;OBS        date1         date2     string1      string2       string3

 1     10MAR2021    2021-03-10    10MAR2021    03/10/2021    2021-03-10
 2     03OCT2022    2022-10-03    03OCT2022    10/03/2022    2022-10-03
 3     01JAN2023    2023-01-01    01JAN2023    01/01/2023    2023-01-01
&amp;#12;
&lt;/PRE&gt;
&lt;P&gt;If we sort by STRING2 (value in MDY order) then we get.&amp;nbsp; (Plus it is difficult to know which of those last two values represents MARCH and which represent OCTOBER since half of world used DMY order instead of MDY order)&lt;/P&gt;
&lt;PRE&gt;OBS        date1         date2     string1      string2       string3

 1     01JAN2023    2023-01-01    01JAN2023    01/01/2023    2023-01-01
 2     10MAR2021    2021-03-10    10MAR2021    03/10/2021    2021-03-10
 3     03OCT2022    2022-10-03    03OCT2022    10/03/2022    2022-10-03
&lt;/PRE&gt;
&lt;P&gt;If we sort by STRING1 (value in DATE style) then we get&lt;/P&gt;
&lt;PRE&gt;OBS        date1         date2     string1      string2       string3

 1     01JAN2023    2023-01-01    01JAN2023    01/01/2023    2023-01-01
 2     03OCT2022    2022-10-03    03OCT2022    10/03/2022    2022-10-03
 3     10MAR2021    2021-03-10    10MAR2021    03/10/2021    2021-03-10
&lt;/PRE&gt;
&lt;P&gt;But if we sort by&amp;nbsp; STRING3 (value in YMD order) then we get the same thing as sorting by the actual date value.&lt;/P&gt;
&lt;PRE&gt;OBS        date1         date2     string1      string2       string3

 1     10MAR2021    2021-03-10    10MAR2021    03/10/2021    2021-03-10
 2     03OCT2022    2022-10-03    03OCT2022    10/03/2022    2022-10-03
 3     01JAN2023    2023-01-01    01JAN2023    01/01/2023    2023-01-01
&amp;#12;
&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Jul 2023 18:09:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Date9-numeric-variable-to-mmddyyyy-date-variable/m-p/884376#M349388</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-07-11T18:09:02Z</dc:date>
    </item>
  </channel>
</rss>

