Our tutorials reference a dataset called "sample" in many examples. If you'd like to download the sample dataset to work through the examples, choose one of the files below:
Before reading this tutorial, you may wish to review SAS Date Formats and Informats and SAS Date, Time, and Datetime Values.
Dates, times, and date-times are commonly used variable types in research. In SAS, dates and times are considered numeric variables, but they have several special properties to be aware of.
Regardless of how the researcher records dates or times in their dataset, SAS "internally" records datetime variables as integers. This helps to simplify the computations when computing the differences between dates.
Date | SAS Internal Value |
---|---|
January 1, 1960 | 0 |
January 3, 1960 | 2 |
December 31, 1959 | -1 |
January 1, 1959 | -365 |
January 1, 1961 | 366 |
January 1, 1963 | 1096 |
By default, SAS date and time variables are printed using the SAS internal values, rather than a "human-readable" date format. However, dates can be displayed using any chosen format by setting the format in a data step or proc step. (For a full listing of date-time formats, see About SAS Date, Time, and Datetime Values.)
In this tutorial, we show how to compute new variables from dates and times using two major types of date functions: extraction-type functions and computation-type functions.
Date extraction functions are used to extract a portion of a date from a date variable.
Date creation functions construct new date or datetime variables based on their inputs.
Date computation (or "date difference") functions carry out arithmetic operations on dates; for example, computing the elapsed time between two dates.
Note that the DATDIF and YRDIF functions require a special argument basis
argument. The basis argument determines how the date arithmetic is carried out. Specifically, it determines the number of days that should be used to characterize the period of time in a month or year. In SAS 9.3, there are five possible options that can be used, which are indicated by the following character strings:
Character String | Meaning |
---|---|
'30/360' | Specifies a 30 day month and a 360 day year, regardless of the actual number of calendar days in a given month or year. |
'ACT/ACT' or 'Actual' | Uses the actual number of days or years between dates. |
'ACT/360' | Uses the actual number of days in a particular month, and 360 days as the number of days in a year (regardless of the actual number of days in a given year.) |
'ACT/365' | Uses the actual number of days in a particular month, and 365 days as the number of days in a year (regardless of the actual number of days in a given year.) |
'AGE' | Valid for YRDIF function only. Specifies that a person's age will be computed. This is the default option for YRDIF. |
Given a SAS date value, the YEAR function extracts the year as a numeric value.
YEAR(date);
Where date is a SAS date value that is specified either as a variable or as a SAS date constant.
DATA sample;
SET sample;
yr = YEAR(DOB);
RUN;
Here the YEAR function extracts the year portion from the date value variable DOB and saves it in the new numeric variable yr.
Given a SAS date value, the QTR function extracts the quarter as a numeric value.
QTR(date);
Where date is a SAS date value that is specified either as a variable or as a SAS date constant.
DATA sample;
SET sample;
quarter = QTR(DOB);
RUN;
Here the QTR function extracts the quarter value from the date value variable DOB and saves it in the new numeric variable quarter.
Given a SAS date value, the MONTH function extracts the month as a numeric value.
MONTH(date);
Where date is a SAS date value that is specified either as a variable or as a SAS date constant.
DATA sample;
SET sample;
mn = MONTH(DOB);
RUN;
Here the MONTH function extracts the month value from the date variable DOB and saves it in the new numeric variable mn.
Given a SAS date value, the DAY function extracts the day of the month as a numeric value (between 1-31).
DAY(date);
Where date is a SAS date value that is specified either as a variable or as a SAS date constant.
DATA sample;
SET sample;
days = DAY(DOB);
RUN;
Here the DAY function extracts the day value from the date variable DOB and saves it in the new numeric variable days.
Given a SAS date value, the WEEKDAY function extracts the day of the week from a SAS date value as a number from 1-7.
Value | Day of the week |
1 | Sunday |
2 | Monday |
3 | Tuesday |
4 | Wednesday |
5 | Thursday |
6 | Friday |
7 | Saturday |
WEEKDAY(date);
Where date is a SAS date value that is specified either as a variable or as a SAS date constant.
DATA sample;
SET sample;
wkday = WEEKDAY(DOB);
RUN;
Here the WEEKDAY function extracts the day of the week value from the date variable DOB and saves it in the new numeric variable wkday.
The MDY function creates a new SAS date value, given numeric values representing the month, day, and year.
MDY(month, day, year);
Where
DATA sample;
SET sample;
date = MDY(mn, days, yr);
FORMAT date MMDDYY10.;
RUN;
Here a new variable date will be created by combining the values in the variables mn, days, and yr using the MDY function. The (optional) MMDDYY10.
format tells SAS to display the date values in the form MM/DD/YYYY.
Given two SAS dates, function DATDIF returns the number of days between those two dates.
DATDIF(start_date, end_date, basis);
Where
DATA sample;
SET sample;
date = DATDIF(DOB, Admdate, 'Actual');
RUN;
Here the DATDIF function returns the difference between two date variables (DOB and Admdate) in number of days and saves it in the new numeric variable date.
Given two SAS dates, the YRDIF function returns the number of years between two dates.
YRDIF(start_date, end_date, basis);
Where
DATA sample;
SET sample;
years = YRDIF(DOB, Admdate, 'Actual');
RUN;
Here the YRDIF function gives the difference between two dates (DOB and Admdate) in number of years and saves it in the new numeric variable years.