<?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: Date formatting in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Date-formatting/m-p/398114#M96252</link>
    <description>&lt;P&gt;This is a perfect situation to use the PICTURE statement to make a user-defined format, instead of the value statement.&amp;nbsp; That's because the picture statement honors the often neglected&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;datatype=date&lt;/STRONG&gt;&lt;/EM&gt; parameter, which in turn supports use of "format directives" (the "%" codes below):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
  picture myfmt (min=18)
    low-high = '%B %d, %Y ' (datatype=date) ;
run;

data _null_;
  do d='15jan2014'd to '01jan2015'd by 30;
    put d=date9.   d=myfmt.;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the sas help materials search for documentation on the picture statement.&amp;nbsp; This capability is not present in the value statement.&lt;/P&gt;</description>
    <pubDate>Fri, 22 Sep 2017 14:36:59 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2017-09-22T14:36:59Z</dc:date>
    <item>
      <title>Date formatting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-formatting/m-p/398052#M96241</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have about a 100&amp;nbsp;dates that I'm trying to format in a specific way i.e. Month space day, space year. So for 3july1965, I want to make it July 3, 1965. For 20121jul I want to make it July 1, 2012 and for 02112007 I want to make it July 11, 2007. I've only included these 3 as the rest are just repetition of this theme.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is much appreciated&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data dates;&lt;/P&gt;&lt;P&gt;input #1 @1 one $1-13&lt;BR /&gt;#2 @1 two $1-12&lt;BR /&gt;#3 @1 three $1-12;&lt;BR /&gt;datalines;&lt;BR /&gt;3july1965&lt;BR /&gt;20121Jul&lt;BR /&gt;07112007&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;proc print data=dates;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Sep 2017 11:31:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-formatting/m-p/398052#M96241</guid>
      <dc:creator>harcluna</dc:creator>
      <dc:date>2017-09-22T11:31:11Z</dc:date>
    </item>
    <item>
      <title>Re: Date formatting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-formatting/m-p/398061#M96244</link>
      <description>&lt;P&gt;You have two issues here. &amp;nbsp;First is how to obtain a valid SAS date value from your incoming text. &amp;nbsp;Second is how to display that value in the format that you desire. &amp;nbsp;Untested, but should handle both issues:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data dates;&lt;/P&gt;
&lt;P&gt;input dateval&amp;nbsp;anydtdte9.;&lt;/P&gt;
&lt;P&gt;format dateval&amp;nbsp;mmddyyb10.;&lt;BR /&gt;datalines;&lt;BR /&gt;3july1965&lt;BR /&gt;20121Jul&lt;BR /&gt;07112007&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;proc print;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The ANYDTDTE informat reads most date and datetime&amp;nbsp;text strings, and converts them to the equivalent SAS date value. &amp;nbsp;The MMDDYYB10. format displays dates in 10 characters, month then day then year, with B=Blanks as the separator.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There may be some text strings that ANYDTDTE can't figure out. &amp;nbsp;And it may have to make a decision if it sees a string like 07112007 (which is the month and which is the day). &amp;nbsp;You'll have to test it and see what you get.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Sep 2017 11:46:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-formatting/m-p/398061#M96244</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-09-22T11:46:00Z</dc:date>
    </item>
    <item>
      <title>Re: Date formatting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-formatting/m-p/398114#M96252</link>
      <description>&lt;P&gt;This is a perfect situation to use the PICTURE statement to make a user-defined format, instead of the value statement.&amp;nbsp; That's because the picture statement honors the often neglected&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;datatype=date&lt;/STRONG&gt;&lt;/EM&gt; parameter, which in turn supports use of "format directives" (the "%" codes below):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
  picture myfmt (min=18)
    low-high = '%B %d, %Y ' (datatype=date) ;
run;

data _null_;
  do d='15jan2014'd to '01jan2015'd by 30;
    put d=date9.   d=myfmt.;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the sas help materials search for documentation on the picture statement.&amp;nbsp; This capability is not present in the value statement.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Sep 2017 14:36:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-formatting/m-p/398114#M96252</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-09-22T14:36:59Z</dc:date>
    </item>
    <item>
      <title>Re: Date formatting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-formatting/m-p/398179#M96271</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
  picture myfmt (min=18)
    low-high = '%B %d, %Y ' (datatype=date) ;
run;

data _null_;
  do d='15jan2014'd to '01jan2015'd by 30;
    put d=date9.   d=myfmt.;
  end;
run;&lt;/CODE&gt;&lt;/PRE&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;SAS supplied format WORDDATE.&lt;/P&gt;
&lt;PRE&gt;data _null_;
  do d='15jan2014'd to '01jan2015'd by 30;
    put d=date9.   d=worddate.;
  end;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Sep 2017 16:58:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-formatting/m-p/398179#M96271</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-09-22T16:58:39Z</dc:date>
    </item>
    <item>
      <title>Re: Date formatting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-formatting/m-p/398260#M96315</link>
      <description>&lt;P&gt;Thank you for your help, but I still can't get my data to look like I want. The code below shows how I'd like it to end up, but I'm just having trouble with the intermediate steps.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data testdate;&lt;BR /&gt;length chardate $8;&lt;BR /&gt;infile datalines;&lt;BR /&gt;input chardate $;&lt;BR /&gt;date = input(chardate,yymmdd8.);&lt;BR /&gt;return;&lt;BR /&gt;datalines;&lt;BR /&gt;20170701&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;ods listing;&lt;BR /&gt;proc print data=testdate;&lt;BR /&gt;title '1) Internally stored values';&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc print data=testdate;&lt;BR /&gt;title '2) Using SAS format with dates';&lt;BR /&gt;format date worddate.;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Sat, 23 Sep 2017 04:22:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-formatting/m-p/398260#M96315</guid>
      <dc:creator>harcluna</dc:creator>
      <dc:date>2017-09-23T04:22:30Z</dc:date>
    </item>
    <item>
      <title>Re: Date formatting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-formatting/m-p/398262#M96316</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/166352"&gt;@harcluna&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Running the code you've posted looks good to me.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.JPG" style="width: 304px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/15375i78F49A75EAA12A00/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.JPG" alt="Capture.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is not working as you want? Please explain in detail.&lt;/P&gt;</description>
      <pubDate>Sat, 23 Sep 2017 04:46:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-formatting/m-p/398262#M96316</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-09-23T04:46:07Z</dc:date>
    </item>
    <item>
      <title>Re: Date formatting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-formatting/m-p/398278#M96322</link>
      <description>&lt;P&gt;Hi Patrick,&lt;/P&gt;&lt;P&gt;Thank you for your interest. The problem is I have a lot of dates in a format different from that in the code e.g.&lt;BR /&gt;3july1965&lt;BR /&gt;20121Jul&lt;BR /&gt;07112007&lt;/P&gt;&lt;P&gt;What I would like to do is convert them to this format:&lt;BR /&gt;July, 3 1965&lt;BR /&gt;July, 21 2012&lt;BR /&gt;July, 11 2007&lt;/P&gt;&lt;P&gt;The date format I used in the code&lt;BR /&gt;20170701 converted to July, 1 2017, but I can't convert the other formats so the code will run properly (I have a lot of dates so I don't want to change them all by hand).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 23 Sep 2017 09:43:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-formatting/m-p/398278#M96322</guid>
      <dc:creator>harcluna</dc:creator>
      <dc:date>2017-09-23T09:43:17Z</dc:date>
    </item>
    <item>
      <title>Re: Date formatting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-formatting/m-p/398329#M96339</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/166352"&gt;@harcluna&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;You normally get answers quicker and better suited to your problem if you provide representative sample data via a working SAS data step covering all your cases. We can't know what you're actually dealing with.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dates in SAS&lt;/P&gt;
&lt;P&gt;SAS stores dates as the count of days since 1/1/1960 as a simple number in a numeric variable. You then can apply a SAS Date FORMAT to make this number human readable. Such a format is only for display and it doesn't change the internally stored value (the count of days). In your case the format is &lt;EM&gt;worddate.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;You use SAS date &lt;STRONG&gt;IN&lt;/STRONG&gt;FORMATS to instruct SAS how to interprete a string representing a date so SAS can convert this string into a SAS Date value (the count of days).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The problem you're dealing with is that your date strings are so varying using multiple patterns for representing a date. There isn't a one fits all SAS INFORMAT to deal with all these patterns. You need to write code which can try multiple Informats and then stops once one of these Informats has been successful (=text string converted resulting in a non-missing numeric value).&lt;/P&gt;
&lt;P&gt;You can find the list of available SAS Date Informats here:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/leforinforref/64790/HTML/default/viewer.htm#n0verk17pchh4vn1akrrv0b5w3r0.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/leforinforref/64790/HTML/default/viewer.htm#n0verk17pchh4vn1akrrv0b5w3r0.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What complicates things: There is no Informat for a date string pattern of yyyydmon like &lt;EM&gt;20121Jul&lt;/EM&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For such cases you need either to implement your own informat or you need to add some additional logic in your code which transforms the string to something for which you've got an informat (i.e. recombine the string using substring() functions and then concatenate the substrings the way you need them).&lt;/P&gt;
&lt;P&gt;The code below uses a combination of above. It creates a custom informat using a Regular Expression which recombines the input string and then applies the DATE9. informat to this recombined string. That's only one way of doing things and if you're not familiar with Regular Expressions then you probably want to implement such logic as an additional block in your data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here you go:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
   /* convert date string to a pattern for which there is a SAS Date Informat */
   invalue yyyyddmon (default=9)
    's/^\s*(\d{4})(\d{1,2}[a-z]{3})\s*$/\2\1/i' (regexpe) = [date9.] 
    other=_same_; 
run;

options datestyle=mdy;
data sample;
  input inDateString :$20.;
  format outSASDateValue worddate.;
  if not missing(inDateString) then 
    do;;
      /* try: read the string and convert to SAS Date value using different INformats */
      do tryInFMT='anydtdte.','mmddyy10.','yyyyddmon.';
        outSASDateValue=inputn(inDateString,tryInFMT);
        /* check: stop trying if conversion successful */
        if not missing(outSASDateValue) then leave;
      end;
    end;
  datalines;
3july1965
20121Jul
201201Jul
07112007
07/11/2007
;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&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>Sun, 24 Sep 2017 07:02:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-formatting/m-p/398329#M96339</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-09-24T07:02:01Z</dc:date>
    </item>
  </channel>
</rss>

