SAS - Date & Times


Advertisements

IN SAS dates are a special case of numeric values. Each day is assigned a specific numeric value starting from 1st January 1960. This date is assigned the date value 0 and the next date has a date value of 1 and so on. The previous days to this date are represented by -1 , -2 and so on. With this approach SAS can represent any date in future and any date in past.

When SAS reads the data from a source it converts the data read into a specific date format as specified the date format. The variable to store the date value is declared with the proper informat required. The output date is shown by using the output data formats.

SAS Date Informat

The source data can be read properly by using specific date informats as shown below. The digit at the end of the informat indicates the minimum width of the date string to be read completely using the informat. A smaller width will give incorrect result. with SAS V9, there is a generic date format anydtdte15. which can process any date input.

Input Date Date width Informat
03/11/2014 10 mmddyy10.
03/11/14 8 mmddyy8.
December 11, 2012 20 worddate20.
14mar2011 9 date9.
14-mar-2011 11 date11.
14-mar-2011 15 anydtdte15.

Example

The below code shows the reading of different date formats. Please note the all the output values are just numbers as we have not applied any format statement to the output values.

DATA TEMP;
INPUT @1 Date1 date11. @12 Date2 anydtdte15. @23 Date3 mmddyy10.   ;
DATALINES;
02-mar-2012 3/02/2012 3/02/2012
;
PROC PRINT DATA = TEMP;
RUN;

When the above code is executed, we get the following output.

date_time_1

SAS Date output format

The dates after being read , can be converted to another format as required by the display. This is achieved using the format statement for the date types. They take the same formats as informats.

Example

In the below exampel the date is read in one format but displayed in another format.

DATA TEMP;
INPUT  @1 DOJ1 mmddyy10. @12 DOJ2 mmddyy10.;
format  DOJ1 date11.  DOJ2 worddate20. ;
DATALINES;
01/12/2012 02/11/1998 
;
PROC PRINT DATA = TEMP;
RUN;

When the above code is executed, we get the following output.

date_time_2
Advertisements