How to use the MDY function in SAS?

The MDY function in SAS is used to convert dates from numeric values to a SAS date value. This function takes three arguments: the month, day, and year of the date. The function then returns a SAS date value with the same month, day, and year from the arguments given. This function is useful for creating a SAS date value from a date that is stored as three separate numeric values.


You can use the MDY function in SAS to return a date value from month, day and year values.

This function uses the following syntax:

MDY(month, day, year)

where:

  • month: Integer value for month from 1 to 12
  • day: Integer value for day of the month from 1 to 31
  • year: A two-digit or four-digit integer that represents the year

The following example shows how to use the MDY function in practice.

Example: How to Use the MDY Function in SAS

Suppose we have the following dataset in SAS that contains information about sales made on various dates at some retail store:

/*create dataset*/
data my_data;
    input month day year sales;
    datalines;
4 15 2022 94
6 17 2022 88
7 25 2022 90
8 14 2022 105
10 13 2022 119
12 15 2022 100
1 4 2023 87
3 15 2023 90
5 29 2023 130
;
run;

/*view dataset*/
proc print data=my_data;

The following code shows how to use the MDY function to create dates using the numeric values in the month, day and year columns:

/*create new dataset*/
data new_data;
    set my_data;
    date_numeric = mdy(month, day, year);
    date_worddate = put(mdy(month, day, year), worddate.);
    date_date9 = put(mdy(month, day, year), date9.);
    date_mmddyy10 = put(mdy(month, day, year), mmddyy10.);
run;

/*view dataset*/
proc print data=new_data;

MDY function in SAS example

Notice that we used the MDY function to create four new columns that all contain dates in various formats.

Note #1: You can find a complete list of potential date formats in SAS .

Note #2: You can find the complete documentation for the SAS MDY function .

x