Working with Time Series Data

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 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 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 2: SAS Date Values Printed with Different Formats

ObsDATE with MONYY7.
format
US Consumer
Price Index
DATE with
no format
DATE with DATE9.
format
1JUN1990129.91110901JUN1990
2JUL1990130.41113901JUL1990
3AUG1990131.61117001AUG1990
4SEP1990132.71120101SEP1990
5OCT1990133.51123101OCT1990
6NOV1990133.81126201NOV1990
7DEC1990133.81129201DEC1990
8JAN1991134.61132301JAN1991
9FEB1991134.81135401FEB1991
10MAR1991135.01138201MAR1991
11APR1991135.21141301APR1991
12MAY1991135.61144301MAY1991
13JUN1991136.01147401JUN1991
14JUL1991136.21150401JUL1991


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 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: 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


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

Last updated: June 19, 2025