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

This post was kindly contributed by The SAS Dummy - go there to comment and to read the full post.

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 or 400
  • Yeah…except when it’s also divisible by 1000.

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
...
2080
2084
2088
2092
2096
2104
2108
2112
2116

Notice how 2000 was included, but 2100 is not? That’s not a leap year, and SAS knows it. Did you?

See also

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

tags: leap year, SAS programming

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

This post was kindly contributed by The SAS Dummy - go there to comment and to read the full post.