Leap year questions come up all of the time in computing, but if there is any true season for it, it's now. The end of February is approaching and developers wonder: does my process know that it's a leap year, and will it behave properly?
People often ask how to use SAS to calculate the leap years. The complicated answer is:
- Check whether the year is divisible by 4 (MOD function)
- But add exceptions when divisible by 100
- Yeah...except when it's also divisible by 400.
The simple answer is: ask SAS. You can create a SAS date value with the MDY function. Feb 29 is a valid date for leap years; in off years, MDY returns a missing value.
data leap_years(keep=year); length date 8; do year=2000 to 2200; /* MISSING when Feb 29 not a valid date */ date=mdy(2,29,year); if not missing(date) then output; end; run;
Here's an excerpt of the result:
2000 2004 2008 2012 2016 2020 2024 /* skip a few */ 2088 2092 2096 2104 2108 2112 2116 2120
Notice how 2000 was included (divisible by 400), but 2100 is not? That's not a leap year, and SAS knows it. Did you?
In the year 9999...: history of leap year and some software bugs