Formatting Date and Datetime Values

SAS provides formats to convert the internal representation of date and datetime values used by SAS to ordinary notations for dates and times. Several different formats are available for displaying dates and datetime values in most of the commonly used notations.

A SAS format is an instruction that converts the internal numerical value of a SAS variable to a character string that can be printed or displayed. Date formats convert SAS date values to a readable form; datetime formats convert SAS datetime values to a readable form.

In the preceding example, the variable DATE was set to the SAS date value for the first day of the month for each observation. If the data set USCPI were printed or otherwise displayed, the values shown for DATE would be the number of days since 1 January 1960. (See the “DATE with no format” column in Figure 3.2.) To display date values appropriately, use the FORMAT statement.

The following example processes the data set USCPI to make several copies of the variable DATE and uses a FORMAT statement to give different formats to these copies. The format cases shown are the MONYY7. format (for the DATE variable), the DATE9. format (for the DATE1 variable), and no format (for the DATE0 variable). The PROC PRINT output in Figure 3.2 shows the effect of the different formats on how the date values are printed.

data fmttest;
   set uscpi;
   date0 = date;
   date1 = date;
   label date  = "DATE with MONYY7. format"
         date1 = "DATE with DATE9. format"
         date0 = "DATE with no format";
   format date monyy7. date1 date9.;
run;
proc print data=fmttest label;
run;

Figure 3.2 SAS Date Values Printed with Different Formats
Obs DATE with MONYY7.
format
US Consumer
Price Index
DATE with
no format
DATE with DATE9.
format
1 JUN1990 129.9 11109 01JUN1990
2 JUL1990 130.4 11139 01JUL1990
3 AUG1990 131.6 11170 01AUG1990
4 SEP1990 132.7 11201 01SEP1990
5 OCT1990 133.5 11231 01OCT1990
6 NOV1990 133.8 11262 01NOV1990
7 DEC1990 133.8 11292 01DEC1990
8 JAN1991 134.6 11323 01JAN1991
9 FEB1991 134.8 11354 01FEB1991
10 MAR1991 135.0 11382 01MAR1991
11 APR1991 135.2 11413 01APR1991
12 MAY1991 135.6 11443 01MAY1991
13 JUN1991 136.0 11474 01JUN1991
14 JUL1991 136.2 11504 01JUL1991

The appropriate format to use for SAS date or datetime valued ID variables depends on the sampling frequency or periodicity of the time series. Table 3.2 shows recommended formats for common data sampling frequencies and shows how the date ’17OCT1991’D or the datetime value ’17OCT1991:14:45:32’DT is displayed by these formats.

Table 3.2 Formats for Different Sampling Frequencies

ID values

Periodicity

FORMAT

Example

SAS date

annual

YEAR4.

1991

 

quarterly

YYQC6.

1991:4

 

monthly

MONYY7.

OCT1991

 

weekly

WEEKDATX23.

Thursday, 17 Oct 1991

 

daily

DATE9.

17OCT1991

SAS datetime

hourly

DATETIME10.

17OCT91:14

 

minutes

DATETIME13.

17OCT91:14:45

 

seconds

DATETIME16.

17OCT91:14:45:32

See Chapter 4, Date Intervals, Formats, and Functions, for more information about the date and datetime formats available.