Chapter Contents
Chapter Contents
Working with Time Series Data

Formatting Date and Datetime Values

The SAS System 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 aan 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 2.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 2.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.;
   proc print data=fmttest label;

Obs DATE with MONYY.
cpi DATE with
no 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
Figure 2.2: SAS Date Values Printed with Different Formats

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 2.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 2.2: Formats for Different Sampling Frequencies
ID values Periodicity FORMAT Example
SAS DateAnnualYEAR4.1991
 WeeklyWEEKDATX23.Thursday, 17 Oct 1991
SAS DatetimeHourlyDATETIME10.17OCT91:14

See Chapter 3 for more information on the date and datetime formats available.

Chapter Contents
Chapter Contents

Copyright © 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.