• Ekta Aggarwal

Dates in SAS

SAS has got various dates formats and informats. In this tutorial we shall understand all of them in detail.


Key topics covered:


Firstly let us initialize our library:

libname mylib '/home/u52453927/My_datasets';

Most common SAS date formats and informats


All the SAS date formats and informats can be found on the official SAS website!

Most widely used SAS date formats are:

Dates in SAS













SAS has 2 types : numeric and character. While defining dates, SAS dates belong to numeric data type:


Defining dates


In the following code we are creating a new dataset MYDATA with one column Date1 which has a date9. informat.

DATA mylib.mydata;
INPUT Date1 date9.;
CARDS;
20Jan2017
20Feb2021
15Aug1988
;
RUN;







In the above output our Date1 column looks weird. Isn't it? This is because in the above code we have defined only INFORMATS in the INPUT statement. To view the dates in a desired format we need to provide a FORMAT statement.

To learn more about FORMATS and INFORMATS click here!


In the following code we have defined our FORMAT as date9.

DATA mylib.mydata;
INPUT Date1 date9.;
FORMAT Date1 date9.;
CARDS;
20Jan2017
20Feb2021
15Aug1988
;
RUN;









What is Yearcutoff?

Let us define a dataset with dates 20Jan2017 , 20 Feb 2027 and 15 Aug 1988.

DATA mylib.mydata;
INPUT Date1 date7.;
FORMAT Date1 date9.;
CARDS;
20Jan17
20Feb27
15Aug88
;
RUN;

Notice the second date!










Instead of 2027 SAS took 1927. Why?

This is because of the year cutoff which SAS has by default. So let us try to understand what is this yearcutoff?


By default SAS currently has a yearcutoff of 1926 which means if you specify two-digits in a year as 26 - 99 then SAS will understand it as 1926 - 1999 and for 00 - 25 it will understand it as 2000 - 2025.


Is there any way to change this cutoff?

Yes. SAS provides us an option to set this year cutoff. For eg. to change the yearcutoff to 1950, I shall write:

options yearcutoff=1950;

Now we will rerun the above code chunk with redefining our yearcutoff!

options yearcutoff=1950;

DATA mylib.mydata;
INPUT Date1 date7.;
FORMAT Date1 date9.;
CARDS;
20Jan17
20Feb27
15Aug88
;
RUN;









Formats and informats can be different


In SAS we can different informats (while reading the dataset) and formats (to view the dataset):


In the following SQL query we are defining dates in a mmddyy10. informat while we can view it in a date9. format.

DATA mylib.mydata;
INPUT Date1 mmddyy10.;
FORMAT Date1 date9.;
CARDS;
07/10/2017
02/12/1989
12/28/2001
;
RUN;








COLON informat in dates


In an MMDDYY10. INFORMAT SAS by default considers slashes '/' as a date separator. To read any other separator we define a colon ':' informat in the INPUT statement.


In the following code we are reading a SAS date as MMDDYY10. INFORMAT (with ' - ' as a date separator and are viewing it in a DATE9. informat)

DATA mylib.mydata;
INPUT Date1 : mmddyy10.;
FORMAT Date1 date9.;
CARDS;
07-10-2017
02-12-1989
12-28-2001
;
RUN;








Other special FORMATS:


WEEKDATE format :


In a weekdate format we have Day of the week, Month day, Year

DATA mylib.mydata;
INPUT Date1 : mmddyy10.;
FORMAT Date1 WEEKDATE.;
CARDS;
07-10-2017
02-12-1989
12-28-2001
;
RUN;







WORDDATE format:


In a WORDDATE format we have Month day, Year

DATA mylib.mydata;
INPUT Date1 : mmddyy10.;
FORMAT Date1 WORDDATE.;
CARDS;
07-10-2017
02-12-1989
12-28-2001
;
RUN;






Define a date outside date step


Sometimes we may manually need to define a date - outside the data step (eg. for filtering the rows) In such a case we can define a date in quotes followed by 'd'

https://www.analyticsisnormal.com/post/intck-comparing-two-dates-in-sas

Here we have defines the date 4th Sept 2018 manually which can be used in a WHERE statement.

WHERE date1 > '04092018'd;

You can also refer to the following tutorials to learn more about date functions:

Comparing dates in SAS using INTCK

Incrementing dates in SAS using INTNX

Tags: