Feed Back And Queries

Navigation

SAS Date Function: MDY ( )

<< Go Back To SAS Date Functions
 
SAS MDY( ) function is mainly used create a SAS date value out of a three separate arguments Month, Day and Year.
As we know, SAS internally stores date as numeric value. Hence its important to create that specific number representing the given date (e.g. 1 JAN 1960 is Stored as 0 hence 2 JAN 1960 will be 1 and so on)
 
MDY( ) helps to create a SAS date representing the given date interms of Month, Day and Year.
This has been demonstrated below;
 
1. SAS MDY( ) Function 
 
 
SYNTAX: MDY (month, day, year ); 

MDY() function takes 3 arguments

  • Month – numeric value between 1 to 12
  • Day – numeric value between 1 to 31
  • Year – 2 digit or 4 digit numeric value representing a year. In case of 2 digit value, the exact consideration of date depends on YEARCUTOFF option specified.

 

 

Example: SAS MDY( ) Function

SAS Code:
  

data temp;

dt_with_4digit = MDY(3, 2, 2010);

dt_with_2digit = MDY(3, 2, 98);

dt_with_wrong_month = MDY (13,3,2010);

run;

 
 
proc print data = temp;

run;

 
PROC PRINT OUTPUT without FORMAT statement :
 

Obs

dt_with_4digit

dt_with_2digit

dt_with_wrong_month

1

18323

13940

.

 

PROC PRINT OUTPUT with FORMAT statement :

proc print data = temp;

format dt_with_4digit dt_with_2digit dt_with_wrong_month date9.;

run;
 
 
 

Obs

dt_with_4digit

dt_with_2digit

dt_with_wrong_month

1

02MAR2010

02MAR1998

.

 
 
 
* Please note this code has compiled on SAS 9.2 Platform.
 

If we see the code more closely;

In the first statement year value is given with 4 digits, hence refers to 2nd March 2010.

 

In case of second statement year value specified is 2 digits and as YEARCUTOFF option is set as 1920; ‘98’ refers to 1998 and hence the actual date is 2nd March 1998.

 

In the third statement, we can observe that we get ‘.’ Missing value, as we provide wrong value ‘13’ as month to the MDY function.

Further in the log one can observe following NOTES

 

NOTE: Invalid argument to function MDY at line 18 column 23.

dt_with_4digit=18323 dt_with_2digit=13940 dt_with_wrong_month=. _ERROR_=1 _N_=1

NOTE: Mathematical operations could not be performed at the following places. The results of the operations have been set to

      missing values.

      Each place is given by: (Number of times) at (Line):(Column).

      1 at 18:23  

 

It is interesting to note the difference between first and second print procedures;

First print procedure don’t have format statement; hence date values are represented as actual values as those stored internally.

In the second print procedure; as format statement is used dates are represented in DATE9 format while printing.

 

 
Comments