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;

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.