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, |
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:
where
|
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 |
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 |
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 |
CARY | NC | 27513 | 6224549 | 10 | 11 | 88 | 17JUN2002 |
CARY | NC | 27513 | 6223251 | 8 | 25 | 200 | 17JUN2002 |
NC | 27514 | 9974749 | 40 | 26 | 208 | 17JUN2002 | |
RALEIGH | NC | 27612 | 6970450 | 15 | 10 | 80 | 17JUN2002 |
|
![]() ![]() ![]() ![]() ![]() ![]() |
|
![]() |
![]() |
Copyright © 2002 SAS Institute Inc.,
Cary, NC, USA. All rights reserved.