Published on :
ETL CREATION_INTERNE

Leap Year Detection

This code is also available in: Deutsch Español Français
Awaiting validation
The script uses a DATA step to iterate through years from 2000 to 2200. For each year, it attempts to construct a date for February 29th. If the resulting date is not missing (meaning February 29th is a valid date for that year), then the year is considered a leap year and is added to the 'leap_years' output dataset. Only the 'year' variable is kept in the final dataset.
Data Analysis

Type : CREATION_INTERNE


The data is entirely generated internally by the DATA step, without dependency on existing datasets (SASHELP or external).

1 Code Block
DATA STEP Data
Explanation :
This block is a DATA step that creates a new dataset named 'leap_years'. It initializes a 'date' variable to store date values. A 'do' loop iterates through each year from 2000 to 2200. Inside the loop, the MDY() function is used to attempt to create a date for February 29th of the current year. If February 29th is not a valid date (i.e., the year is not a leap year), the MDY() function returns a missing value. The 'if not missing(date)' condition filters these cases, only writing leap years to the output dataset via the 'output' statement. The 'keep=year' option ensures that only the 'year' variable is retained in the final dataset.
Copied!
1DATA leap_years(keep=year);
2 LENGTH date 8;
3 DO year=2000 to 2200;
4 /* MISSING when Feb 29 not a valid date */
5 date=mdy(2,29,year);
6 IF not missing(date) THEN
7 OUTPUT;
8 END;
9RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.