May 4 is celebrated by some folks as "Star Wars Day". Of course, this makes a SAS Jedi wonder "What is the SAS date of the next Star Wars Day"? Not being one to do things by hand, I'd rather program to find the answer. For this, I'll use the SAS MDY function. MDY returns a valid SAS date value when provided with the month, day and year.

If May 4 has not arrived yet this year, the next Star Wars day will be THIS year, and I would calculate it like this:

 NextStarWarsDay=MDY(5,4,year(today()));

However, if it's already May 4 (or later), the next Star Wars Day will be next year, and I'd want to add one to the year:

 NextStarWarsDay=MDY(5,4,year(today())+1);

The difference between the two calculations lies in determining whether or not to add a 1 to the year value in the MDY function. We can use a little boolean to automate this process. A logical expression produces a numeric value: 1 when true, 0 when false. The following logical expression yields 1 if this year's occurrence of the date is already past, and a 0 if this year's occurrence of the date is still yet to happen:

 mdy(5,4,year(today()))

That calculation could replace the hard-coded 1 in the date calculation:

 NextStarWarsDay=MDY(5,4,year(today())+ (mdy(5,4,year(today()))

This should consistently produce the correct answer.  We'll test the process in a DATA _null_ step:

 data _null_; today=mdy(5,3,2013); /* today=mdy(5,6,2013);*/ NextStarWarsDay=MDY(5,4,year(today)+(mdy(5,4,year(today))

This works fine. I'd replace the today variable with the today() function, and the code is ready for general use:

 data _null_; NextStarWarsDay=MDY(5,4,year(today())+(mdy(5,4,year(today()))

Until next time, may the SAS be with you!
Mark

PS - Sometimes the code doesn't look like I planned when I post it in HTML - so just click here download a zip file with the programs used to develop this blog entry. Enjoy!

Tags
Share