Skip to Main Content

SAS Tutorials: Date-Time Functions and Variables in SAS

About Date-Time Variables

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.

  • SAS time values are stored internally as the number of seconds between midnight of the current day and another time value.
  • SAS datetime values stored internally as the number of seconds between midnight, January 1, 1960, and the specified date and time.
  • SAS date values are the stored internally as the number of days between January 1, 1960, and a specified date. Dates after January 1, 1960, are stored as positive numbers; dates before January 1, 1960, are stored as negative numbers.
Table 1: SAS internal values for selected 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.

  • YEAR - Given a number or a variable representing a date or datetime, returns the year.
  • QTR - Given a number or a variable representing a date or datetime, returns the quarter.
  • MONTH - Given a number or a variable representing a date or datetime, returns the month.
  • DAY - Given a number or a variable representing a date or datetime, returns the day (as a number from 1-31).
  • WEEKDAY - Given a number or a variable representing a date or datetime, returns the day of the week (as a coded number from 1-7).

Date creation functions construct new date or datetime variables based on their inputs.

  • MDY - Given numbers or variables representing the month, day, and year, creates a new date variable.

Date computation (or "date difference") functions carry out arithmetic operations on dates; for example, computing the elapsed time between two dates.

  • DATDIF - Given two SAS dates or datetimes, computes the difference between the dates in days.
  • YRDIF - Given two SAS dates or datetimes, computes the difference between the dates in years.

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:

Table 2: Valid character strings for basis argument in DATDIF and YRDIF functions.
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.

YEAR Function

Given a SAS date value, the YEAR function extracts the year as a numeric value.

Syntax

YEAR(date);

Where date is a SAS date value that is specified either as a variable or as a SAS date constant.

Example

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.

QTR Function

Given a SAS date value, the QTR function extracts the quarter as a numeric value.

Syntax

QTR(date);

Where date is a SAS date value that is specified either as a variable or as a SAS date constant.

Example

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.

MONTH Function

Given a SAS date value, the MONTH function extracts the month as a numeric value.

Syntax

MONTH(date);

Where date is a SAS date value that is specified either as a variable or as a SAS date constant.

Example

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.

DAY Function

Given a SAS date value, the DAY function extracts the day of the month as a numeric value (between 1-31).

Syntax

DAY(date);

Where date is a SAS date value that is specified either as a variable or as a SAS date constant.

Example

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.

WEEKDAY Function

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.

Definitions of numeric codes returned from the WEEKDAY function.
Value Day of the week
1 Sunday
2 Monday
3 Tuesday
4 Wednesday
5 Thursday
6 Friday
7 Saturday

Syntax

WEEKDAY(date);

Where date is a SAS date value that is specified either as a variable or as a SAS date constant.

Example

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.

MDY Function

The MDY function creates a new SAS date value, given numeric values representing the month, day, and year.

Syntax

MDY(month, day, year);

Where

  • month is a number from 1-12 (or a numeric variable containing that number);
  • day is a number from 1-31 (or a numeric variable containing that number)
  • year is a two- or four-digit year (or a numeric variable containing that number)

Example

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.

DATDIF Function

Given two SAS dates, function DATDIF returns the number of days between those two dates.

Syntax

DATDIF(start_date, end_date, basis);

Where

  • start_date is a SAS date value that specifies the starting date;
  • end_date is a SAS date value that specifies the ending date;
  • basis specifies a character constant that represents how SAS should count the number of days in a month and year (see Table 2).

Example

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.

YRDIF Function

Given two SAS dates, the YRDIF function returns the number of years between two dates.

Syntax

YRDIF(start_date, end_date, basis);

Where

  • start_date is a SAS date value that specifies the starting date;
  • end_date is a SAS date value that specifies the ending date;
  • basis specifies a character constant that represents how SAS should count the number of days in a month and year (see Table 2).

Example

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.