Discover More Tips and Techniques on This Blog

MDY Function

The MDY function converts MONTH, DAY, and YEAR values to a SAS date value. For example, MDY(10,19,1999) returns the SAS date value '19OCT99'D.

Syntax:    MDY(month,day,year)

Arguments

month : specifies a numeric expression that represents an integer from 1 through 12.

day    :specifies a numeric expression that represents an integer from 1 through 31.

year   :specifies a two-digit or four-digit integer that represents the year. The YEARCUTOFF= system    option defines the year value for two-digit dates.

If you know month, day, and year values, it’s very easy to derive date variable. You just need to use MDY function (of course, month, day, and year should be numeric). However, if the data is character then the conversion to numeric should occur first and then the conversion to the date value.

Example:

*When month, day, and year has numeric values;
data test;
year=1999;
month=12;
day=19;
newdate=mdy(month,day,year);
format newdate yymmdd10.;
run;


*When month, day, and year has character values;
data test;
year='1999';
month='12';
day='19';
newdate=mdy(input(month,2.),input(day,2.),input(year,4.);
format newdate yymmdd10.;
run;

You can use CATX and Input functions to get the same result.

newdate=input(catx('-',year,month,day),yymmdd10.);

Ref: SAS Help and Documentation.

Disclosure:

In the spirit of transparency and innovation, I want to share that some of the content on this blog is generated with the assistance of ChatGPT, an AI language model developed by OpenAI. While I use this tool to help brainstorm ideas and draft content, every post is carefully reviewed, edited, and personalized by me to ensure it aligns with my voice, values, and the needs of my readers. My goal is to provide you with accurate, valuable, and engaging content, and I believe that using AI as a creative aid helps achieve that. If you have any questions or feedback about this approach, feel free to reach out. Your trust and satisfaction are my top priorities.