Title | Leap year indicators | |
Author | Nicholas J. Cox, Durham University, UK |
How do you determine if a year is a leap year using Stata syntax? Even if you are not especially interested in leap years, the answer provides good examples of some key Stata functions, so do read on.
The rules in the Gregorian calendar for a year to be a leap year are
YES if 1. year divisible by 4 (but NO if 2. year divisible by 100 (but YES if 3. year divisible by 400)) and NO otherwise.
Note the nesting of rules. If a leap year is a first-order correction, the third rule is an example of a third-order correction. Scientists and engineers often use such ideas, but they seem in shorter supply in the everyday world.
You will be familiar with rule 1, but rules 2 and 3 are occasionally forgotten. For example, Excel has 1900 as a leap year; it is documented that this was to provide compatibility with Lotus 1-2-3.
In Stata, suppose that year is a variable. An indicator containing 1 for leap year and 0 otherwise is then given by
(mod(year,4) == 0 & mod(year,100) != 0) | mod(year,400) == 0
as mod(,) provides the remainder left over from division. Alternatively, we could use cond(,,):
cond(mod(year,400) == 0, 1, cond(mod(year,100) == 0, 0, cond(mod(year,4) == 0, 1, 0)))
The layout of such code may be important to you, if not to Stata. In such expressions, to make sure you have balanced parentheses, exploit the pertinent function in a decent text editor. In the Stata Do-file Editor, it is "Balance", Ctrl-B; in Vim, it is the % key; etc.
Given daily dates, a feature of a leap year is clearly that there is a February 29, so
mdy(2,29,2004) < .
or
mdy(2,29,2004) != .
is true. That is, mdy(2,29,whenever) is missing if whenever is not a leap year but is nonmissing otherwise; in the latter case, it is less than missing. You could also do that as
!mi(mdy(2,29,2004))
Similarly, there are 366 days in a leap year, so
doy(mdy(12,31,2004)) == 366
is true. We could vectorize both of those calculations to say
mdy(2,29,year) < .
or
doy(mdy(12,31,year)) == 366
Dershowitz and Reingold (2008) provide definitive explanations of calendrical calculations.
Learn
Free webinars
NetCourses
Classroom and web training
Organizational training
Video tutorials
Third-party courses
Web resources
Teaching with Stata
© Copyright 1996–2024 StataCorp LLC. All rights reserved.
×
We use cookies to ensure that we give you the best experience on our website—to enhance site navigation, to analyze usage, and to assist in our marketing efforts. By continuing to use our site, you consent to the storing of cookies on your device and agree to delivery of content, including web fonts and JavaScript, from third party web services.
Cookie Settings
Last updated: 16 November 2022
StataCorp LLC (StataCorp) strives to provide our users with exceptional products and services. To do so, we must collect personal information from you. This information is necessary to conduct business with our existing and potential customers. We collect and use this information only where we may legally do so. This policy explains what personal information we collect, how we use it, and what rights you have to that information.
These cookies are essential for our website to function and do not store any personally identifiable information. These cookies cannot be disabled.
This website uses cookies to provide you with a better user experience. A cookie is a small piece of data our website stores on a site visitor's hard drive and accesses each time you visit so we can improve your access to our site, better understand how you use our site, and serve you content that may be of interest to you. For instance, we store a cookie when you log in to our shopping cart so that we can maintain your shopping cart should you not complete checkout. These cookies do not directly store your personal information, but they do support the ability to uniquely identify your internet browser and device.
Please note: Clearing your browser cookies at any time will undo preferences saved here. The option selected here will apply only to the device you are currently using.