SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Search||next

Transforming Data with SAS Functions
Manipulating SAS Date Values with Functions


MDY Function

The MDY function creates a SAS date value from numeric values that represent the month, day, and year. For example, suppose the data set Hrd.Temp contains the employee start date in three numeric variables, Month, Day, and Year.


SAS Data Set Hrd.Temp
City State Zip Phone Month Day Year PayRate Days Hours
CARY NC 27513 6224549 1 12 2000 10 11 88
CARY NC 27513 6223251 11 17 1999 8 25 200
CHAPEL HILL NC 27514 9974749 12 30 1999 40 26 208
RALEIGH NC 27612 6970450 10 10 1999 15 10 80


Having the start date in three variables makes it hard to perform calculations based on length of employment. You can convert these numeric values to useful SAS date values by applying the MDY function.


General form, MDY function:
MDY(month,day,year)

where

  • month can be a variable that represents the month or a number from 1-12
  • day can be a variable that represents the day or a number from 1-31
  • year can be a variable that represents the year or a number with 2 or 4 digits.


In the data set Hrd.Temp, the values for month, day, and year are stored in the numeric variables Month, Day, and Year. You write the following MDY function to create the SAS date values:
     mdy(month,day,year)

Then place this function in an assignment statement to create a new variable to contain the SAS date values.

     data hrd.newtemp(drop=month day year);
        set hrd.temp;
        Date=mdy(month,day,year);
     run;

Here is the new data set that contains the variable Date.


SAS Data Set Hrd.Newtemp
City State Zip Phone PayRate Days Hours Date
CARY NC 27513 6224549 10 11 88 14621
CARY NC 27513 6223251 8 25 200 14565
CHAPEL HILL NC 27514 9974749 40 26 208 14608
RALEIGH NC 27612 6970450 15 10 80 14527


Remember, to display SAS date values in a more readable form, you can associate a SAS format with the values. For example, the FORMAT statement below associates the DATE9. format with the variable Date. A portion of the output that is created by this PROC PRINT step appears below.
     proc print data=hrd.newtemp;
        format date date9.;
     run;

City State

Zip

Phone Pay
Rate
Days Hours

Date

CARY NC 27513 6224549 10 11 88 12JAN2000
CARY NC 27513 6223251 8 25 200 17NOV1999
CHAPEL HILL NC 27514 9974749 40 26 208 30DEC1999
RALEIGH NC 27612 6970450 15 10 80 10OCT1999


The MDY function can also add the same SAS date to every observation. This may be useful if you want to compare a fixed beginning date with differing end dates. Just use numbers instead of data set variables when providing values to the MDY function.
     data hrd.newtemp;
        set hrd.temp;
        DateConstant=mdy(6,17,2002);
     proc print data=hrd.newtemp;
        format dateconstant date9.;
     run;

City State

Zip

Phone Pay
Rate
Days Hours

Date
Constant

CARY NC 27513 6224549 10 11 88 17JUN2002
CARY NC 27513 6223251 8 25 200 17JUN2002
CHAPEL HILL NC 27514 9974749 40 26 208 17JUN2002
RALEIGH NC 27612 6970450 15 10 80 17JUN2002


CAUTION: Be careful when entering and formatting year values. The MDY function accepts two-digit values for the year, but SAS software interprets two-digit values according to the 100-year span set by the YEARCUTOFF= system option. The default value of YEARCUTOFF= is 1920. For details, see the lesson Reading Date and Time Values.

Whenever possible, use four-digit year values in the MDY function:

  • MDY(5,10,20)   = May 10, 1920
  • MDY(5,10,2020) = May 10, 2020

To have the years display clearly, format SAS dates with the DATE9. format. This forces the year to appear with four digits, as shown above in the Date and DateConstant variables of your Hrd.Newtemp output.


  |next


Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Terms of Use & Legal Information | Privacy Statement