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 MONYY. format (for the DATE variable), the DATE. 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 MONYY. format"
            date1 = "DATE with DATE. format"
            date0 = "DATE with no format";
      format date monyy. date1 date.;
   proc print data=fmttest label;

Obs DATE with MONYY. format cpi DATE with no format DATE with DATE. format
1 JUN90 129.9 11109 01JUN90
2 JUL90 130.4 11139 01JUL90
3 AUG90 131.6 11170 01AUG90
4 SEP90 132.7 11201 01SEP90
5 OCT90 133.5 11231 01OCT90
6 NOV90 133.8 11262 01NOV90
7 DEC90 133.8 11292 01DEC90
8 JAN91 134.6 11323 01JAN91
9 FEB91 134.8 11354 01FEB91
10 MAR91 135.0 11382 01MAR91

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 '17OCT91'D or the datetime value '17OCT91: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.