How to Use the MDY Function in SAS (With Examples)


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 here.

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

Additional Resources

The following tutorials explain how to perform other common tasks in SAS:

How to Use DAY, MONTH, and YEAR Functions in SAS
How to Get Day of Week from Date in SAS
How to Add Days to Date in SAS

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *