Date, Time, and Datetime Formats

Some of the commonly used SAS date and datetime formats are listed in Table 4.3 and Table 4.4. You can specify the width value for each format by adding w. The tables list the range of width values allowed and the default width value for each format.

The notation used by a format is abbreviated in different ways depending on the width option used. For example, the format MMDDYY8. writes the date 17 October 1991 as 10/17/91, while the format MMDDYY6. writes this date as 101791. In particular, formats that display the year show two-digit or four-digit year values depending on the width option. The examples shown in the tables use the default width.

The interval function INTFMT returns a recommended format for time ID values based on the interval that describes the frequency of the values. The following example uses INTFMT to select a format to display the quarterly time ID variable qtrDate. In this example, INTFMT returns the format YYQC6., which displays the year in four digits and the quarter in a single digit. This selected format is stored in a macro variable that is created by the CALL SYMPUT statement. The second argument to INTFMT controls the width of the year for date formats; it can take the value 'long' or 'l' to indicate 4 for the year width or the value 'short' or 's' to indicate 2 for the year width. For more information about the INTFMT function, see the SAS Language Reference: Dictionary. For more information about the CALL SYMPUT statement, see the SAS Language Reference: Dictionary.

The macro variable &FMT is then used in the FORMAT statement in the PROC PRINT step as follows:

data b(keep=qtrDate);
   interval = 'QTR';
   form = INTFMT( interval, 'long' );
   call symput('fmt',form);
   do i=1 to 4;
      qtrDate = INTNX( interval, '01jan00'd, i-1 );
      output;
   end;
run;

proc print;
   format qtrDate &fmt;
run;

It is also possible to display date and datetime values as strings by using the format that is identified by INTFMT. In the following example, INTFIT is used to identify the intervals of the sashelp.citiwk and sashelp.air data sets. Then INTFMT is used to identify formats that are based on the intervals. The formats are then used to convert the first SAS date value of each data set to a string. The START variable displays the date of the first observation of each data set. This method assumes that the interval of the data set can be identified by examining the first two observations. This is often the case for output data sets and data sets that have been properly prepared for input by using a procedure such as the TIMESERIES procedure. More than two observations might be required to identify the difference between a DAY interval and a WEEKDAY interval. This example would need to be modified if the DATE variable contained SAS datetime values.

data a(keep=DATE0 DATE INTERVAL FMT START);
   length START INTERVAL FMT $32;
   format date0 date DATE.;
   set sashelp.citiwk(obs=2) sashelp.air(obs=2);
   DATE0 = lag(date);
   if ( mod(_n_,2) eq 1 ) then delete;
   if ( mod(_n_,2) eq 0 ) then INTERVAL = intfit( DATE0, date, 'D' );
   if ( mod(_n_,2) eq 0 ) then FMT = INTFMT( interval, 'l' );
   START = putn( DATE0, FMT );
run;

proc print;
run;

See SAS Language Reference: Concepts for a complete description of these formats, including the variations of the formats produced by different width options. See Chapter 3: Working with Time Series Data, for a discussion of the use of date and datetime formats.