<?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: Convert Date to Character in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Convert-Date-to-Character/m-p/469378#M120063</link>
    <description>&lt;P&gt;Here's an example creating a date valued variable and a character version.&lt;/P&gt;
&lt;PRE&gt;data example;
   x=201711;
   date = intnx('month',input(put(x,6. -L),yymmn6.),0,'E');
   format date date9.;
   chardate = put(date,date9.);
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For almost any purpose I would suggest creating a date valued version as you can sort the values correctly, get parts of variable as needed, change grouping in analysis by changing the display format and others.&lt;/P&gt;</description>
    <pubDate>Mon, 11 Jun 2018 19:40:47 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2018-06-11T19:40:47Z</dc:date>
    <item>
      <title>Convert Date to Character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-Date-to-Character/m-p/469376#M120062</link>
      <description>&lt;P&gt;I have numeric date as 201711 and I want to convert it in 30Nov2017 in character format. Anyone help please.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jun 2018 19:32:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-Date-to-Character/m-p/469376#M120062</guid>
      <dc:creator>TanviG</dc:creator>
      <dc:date>2018-06-11T19:32:34Z</dc:date>
    </item>
    <item>
      <title>Re: Convert Date to Character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-Date-to-Character/m-p/469378#M120063</link>
      <description>&lt;P&gt;Here's an example creating a date valued variable and a character version.&lt;/P&gt;
&lt;PRE&gt;data example;
   x=201711;
   date = intnx('month',input(put(x,6. -L),yymmn6.),0,'E');
   format date date9.;
   chardate = put(date,date9.);
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For almost any purpose I would suggest creating a date valued version as you can sort the values correctly, get parts of variable as needed, change grouping in analysis by changing the display format and others.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jun 2018 19:40:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-Date-to-Character/m-p/469378#M120063</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-06-11T19:40:47Z</dc:date>
    </item>
    <item>
      <title>Re: Convert Date to Character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-Date-to-Character/m-p/469385#M120064</link>
      <description>&lt;P&gt;can you please explain a bit how you converted numeric value in a sas date ? Many Thanks.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jun 2018 19:50:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-Date-to-Character/m-p/469385#M120064</guid>
      <dc:creator>TanviG</dc:creator>
      <dc:date>2018-06-11T19:50:31Z</dc:date>
    </item>
    <item>
      <title>Re: Convert Date to Character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-Date-to-Character/m-p/469409#M120069</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/113738"&gt;@TanviG&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have numeric date as 201711 and I want to convert it in 30Nov2017 in character format. Anyone help please.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You currently do NOT have a date. If you have a numeric variable with the value 201,711 it is NOT a date. Dates are the number of days since 1/1/1960.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to interpret it as date then use a PUT() statement to convert it the a character string and use INPUT() statement to convert it to a date.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to store the date value you have created into a character variable then use a PUT() statement. For that style use the DATE9. format.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  number = 201711;
  date1=input(put(number,z6.)||'01',yymmdd8.) ;
  date2 = intnx('month',date1,0,'e');
  char_date = put(date2,date9.);
  put (_numeric_) (= best12. /);
  put /(_numeric_) (= comma12. /);
  put /(date:) (= yymmdd10./);
  put /char_date = ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;number=201711
date1=21124
date2=21153

number=201,711
date1=21,124
date2=21,153

date1=2017-11-01
date2=2017-11-30

char_date=30NOV2017&lt;/PRE&gt;
&lt;P&gt;You can also use some arithmetic to pull the YEAR and MONTH from your number and pass them to the MDY() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  date1 = mdy(mod(number,100),1,int(number/100));
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jun 2018 21:20:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-Date-to-Character/m-p/469409#M120069</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-06-11T21:20:08Z</dc:date>
    </item>
    <item>
      <title>Re: Convert Date to Character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-Date-to-Character/m-p/469434#M120082</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/113738"&gt;@TanviG&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;can you please explain a bit how you converted numeric value in a sas date ? Many Thanks.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;SAS provides a number of informats for reading character data into SAS date values. The informat yymmn6. reads a character value with the first four digits as a year and the next two digits as months. The&amp;nbsp; "n" says there isn't a separator as might occur in some values like 2018/05. Since informats read character values we turn the numeric value into a character using PUT with an appropriate format. The -L in the Put is to left align the value in case you have any single digit month values such as 20177 as the informat expects the first 4 characters to be a year and with out the -L you could get a leading space.&amp;nbsp;The values read with YYMMN6. format will assume the day of the month is 1. The intnx function advances the date to the end of the month (interval of 'MONTH', interval count of 0 is same month and the alignment of 'E' is end).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The date could also be built using the MDY function after determining the Year and Month parts but for simplicity of code you would still want to assume the day of month 1 and use the intnx function to shift.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jun 2018 16:53:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-Date-to-Character/m-p/469434#M120082</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-06-12T16:53:45Z</dc:date>
    </item>
  </channel>
</rss>

