BookmarkSubscribeRSS Feed
mvk_sas
Calcite | Level 5

how to calculate leap year thru SAS.

In case if i have value say year 2000 its divisible by 4 so its called as leap year.

Likewise i will i have set of years say 2000 to 2012 then how can i do the calculation.

7 REPLIES 7
Haikuo
Onyx | Level 15

Please look up mod() function, it may suit your need.

Regards,

Haikuo

Linlin
Lapis Lazuli | Level 10

How about:

data want ;

do x = 2000 to  2012;

  length leap $3;

  leap='no';

  If mod(x,4)=0    then leap='YES'; /* divisible by 4 */

  If mod(x,100)=0 and mod(x,400) ne 0  then leap='NO';

output;

end;

proc print;Run;

                                        Obs      x     leap

                                         1    2000    YES

                                         2    2001    no

                                         3    2002    no

                                         4    2003    no

                                         5    2004    YES

                                         6    2005    no

                                         7    2006    no

                                         8    2007    no

                                         9    2008    YES

                                        10    2009    no

                                        11    2010    no

                                        12    2011    no

                                        13    2012    YES

Linlin

art297
Opal | Level 21

or, just let SAS do the work:

data leaptest;

  do year=1996 to 2015;

    leapyear=ifc(input(catt('29','feb',year), ? date9.),'yes','no');

    output;

  end;

run;

Howles
Quartz | Level 8

Of course the MOD technique will fail in the year 2100.

Rick_SAS
SAS Super FREQ

To Authur's reply, I'll add that the expression

date = mdy(02,29,year);

returns a valid value is YEAR is a leap year and a missing value otherwise.

light
Obsidian | Level 7

This work great. Thank you.  What if I wanted to modify the program to create a new data set that will have leap years as only observations between two given dates?

 

Thank you for your help.

ChrisHemedinger
Community Manager

You could begin by creating a data set with just the leap years.  Using the trick from @Rick_SAS:

 

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;

Then you could join/merge or exclude dates from your reference data set based on the years that appear in the results of this program.  See more explanation in my blog post on this: SAS knows it's a leap year; do you?

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 7838 views
  • 7 likes
  • 8 in conversation