22.3 - SAS Date Informats and Formats

Throughout this lesson so far, we have used the mmddyy8. informat to read in SAS dates. And, we have used the date7. and date9. formats to display SAS dates. In this section, we'll just take a look at a few quick examples to illustrate some of the other informats and formats available in SAS.

Example 22.14 Section

The following SAS program reads in three dates (date1, date2, and date3) using a mmddyy informat. Then, the dates are printed using a ddmmyy format:

DATA inputdates1;
    INPUT @6 date1 mmddyy6. @13 date2 mmddyy8. @22 date3 mmddyy10.;
    FORMAT date1 ddmmyy10. date2 ddmmyyb10. date3 ddmmyyc10.;
    DATALINES;
    041008 04-10-08 04 10 2008
    ;
RUN;
 
PROC PRINT data = inputdates1;
    TITLE 'The mmddyy informat and the ddmmyy format';
RUN;

The mmddyy informat and the ddmmyy format
Obsdate1date2date3
110/04/200810 04 200810:04:2008

First, review the INPUT statement and the corresponding forms of the April 10, 2008 date in the DATALINES statement. Note, in particular, that the width of the mmddyy informat (6, 8, or 10) tells SAS what form of the date it should expect. Don't worry — SAS will let you know if you misspecify the width of the format! Also, note that the way that we format dates can be completely independent of the way that they are informatted. Here, the dates are read in using the mmddyy informat and are displayed in the rearranged ddmmyy format. Well, let's be a little more specific here about that ddmmyy format. The "b" that appears in the format for the date2 variable tells SAS to display blank spaces between the month, day, and year. The "c" that appears in the format for the date3 variable tells SAS to display colons between the month, day, and year. If nothing appears (or alternatively an "s") in a ddmmyy format, as it does here for the date1 variable, SAS will display forward slashes between the month, day, and year.

When you are satisfied you understand the use of the mmddyy informat and the ddmmyy format, launch and run  the SAS program. Review the output to convince yourself that the program does as claimed.

Example 22.15 Section

The following SAS program reads in three dates (date1, date2, and date3) using a ddmmyy informat. Then, the dates are printed using a mmddyy format:

DATA inputdates2;
     INPUT @6 date1 ddmmyy6. @13 date2 ddmmyy8. @22 date3 ddmmyy10.;
	 FORMAT date1 mmddyyd10. date2 mmddyyn8. date3 mmddyyp10.;
	 DATALINES;
     100408 10-04-08 10 04 2008
	 ;
RUN;
 
PROC PRINT data = inputdates2;
   TITLE 'The ddmmyy informat and the mmddyy format';
RUN;

The ddmmyy informat and the mmddyy format
Obsdate1date2date3
104-10-20080410200804.10.2008

First, review the INPUT statement and the corresponding forms of the April 10, 2008 date in the DATALINES statement. Again, the width of the ddmmyy informat (6, 8, or 10) tells SAS what form of the date it should expect. The "d" that appears in the format for the date1 variable tells SAS to display dashes between the month, day, and year. The "n" that appears in the format for the date2 variable tells SAS to display nothing between the month, day, and year. (Note that the width of the mmddyyn8. format is 8, and not 10. If you specify a width of 10 with the "n" extension, SAS will hiccup.) The "p" that appears in the format for the date3 variable tells SAS to display periods between the month, day, and year.

When you are satisfied you understand the use of the ddmmyy informat and the mmddyy format, launch and run  the SAS program. Review the output to convince yourself that the program does as claimed.

Example 22.16 Section

The following SAS program reads in three dates (date1, date2, and date3) using a date informat. Then, the dates are printed using weekdate, worddate, and worddatx formats, respectively:

DATA inputdates3;
         INPUT @6 date1 date7. @14 date2 date9. @24 date3 date11.;
    	 FORMAT date1 weekdate25. 
                date2 worddate19.
                date3 worddatx19.;
    	 DATALINES;
         10Apr08 10Apr2008 10-Apr-2008
    	 ;
    RUN;
    PROC PRINT data = inputdates3;
       TITLE 'The date7 informat and the weekdate and worddate formats';
    RUN;

The date7 informat and the weekdate and worddate formats
Obsdate1date2date3
1Thursday, Apr 10, 2008April 10, 200810 April 2008

First, review the INPUT statement and the corresponding forms of the April 10, 2008 date in the DATALINES statement. Note, in particular, that the width of the date informat (7, 9, or 11) tells SAS what form of the date it should expect. Again — SAS will let you know if you incorrectly specify the width of the format!

Then, launch and run  the SAS program, and review the output so you can appreciate how dates formatted using the weekdate, worddate and worddatx formats are displayed. If the widths that you specify for these formats are too small, SAS will attempt to abbreviate the date for you. You might want to change the width of the weekdate format to, say, 20 to see this for yourself.