SAS knows it's a leap year. Do you?

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?

"Leap year considerations" are built into all SAS functions that calculate differences and intervals between date values (such as DATDIF, INTCK, INTNX, etc.).

More leap year topics

Read about SAS date values in the doc. One fun excerpt:

Using century dates greater than 4000 might result in incorrect dates. SAS does not consider century years that are divisible by 4000 to be leap years. Computations on dates that use a century date greater than or equal to 4000 might be off by days, depending on the computation. SAS does not consider the years 4000, 8000, 12000, 16000, and 20000 to be leap years.

For a fun leap year explainer, watch this episode of StarTalk with Neil deGrasse Tyson.

In the year 9999...: history of leap year and some software bugs

(This article was originally published in 2016, updated in 2024 with new links and additional information.)

The post SAS knows it's a leap year. Do you? appeared first on The SAS Dummy.