Jedi SAS Tricks: May the 4th be with you

2

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()))<today()

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

NextStarWarsDay=MDY(5,4,year(today())+
   (mdy(5,4,year(today()))<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))<today));
   put today=mmddyy. NextStarWarsDay=mmddyy10.;
run;

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()))<today()));
   put NextStarWarsDay=mmddyy10.;
run;

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

About Author

SAS Jedi

Principal Technical Training Consultant

Mark Jordan (a.k.a. SAS Jedi) grew up in northeast Brazil as the son of Baptist missionaries. After 20 years as a US Navy submariner pursuing his passion for programming as a hobby, in 1994 he retired, turned his hobby into a dream job, and has been a SAS programmer ever since. Mark writes and teaches a broad spectrum of SAS programming classes, and his book, "Mastering the SAS® DS2 Procedure: Advanced Data Wrangling Techniques" is in its second edition. When he isn’t writing, teaching, or posting “Jedi SAS Tricks”, Mark enjoys playing with his grand and great-grandchildren, hanging out at the beach, and reading science fiction novels. His secret obsession is flying toys – kites, rockets, drones – and though he usually tries to convince Lori that they are for the grandkids, she isn't buying it. Mark lives in historic Williamsburg, VA with his wife, Lori, and Stella, their cat. To connect with Mark, check out his SAS Press Author page, follow him on Twitter @SASJedi or connect on Facebook or LinkedIn.

2 Comments

  1. Hi, I think something has gone wrong with the formatting of this post. The logical expression should compare to the today variable, or the today() function as appropriate - and does, in the version I received via my email subscription to SAS Blogs, but the comparison isn't actually shown on this page. It looks to me like the <= operator (less than or equal to, in case it's not displayed in this comment!) may have caused your content management system to hide the remainder of the code in each block?

    • SAS Jedi

      You are 100% correct, Tom! When I took a peek earlier today, I was chagrined to find that the embedded code sections in the blog didn't look like they did in my SAS editor! I THINK I figured out what I did wrong, and have re-edited the blog post. This time, I added a link to a ZIP file containing the original SAS programs I used to develop the blog posting - just in case. Thanks for taking the time to point this out and to suggest the possible causes. Maybe I need to blog more often so I don't get so rusty with WordPress :-)
      Mark

Leave A Reply

Back to Top