DEV Community

Dutra
Dutra

Posted on

1 1

SAS Function isBrHoliday

A useful function to test if a date is a Brazilian national holiday. With the necessary adaptations, it can be used for other countries, applying their respective holidays. The function uses proc fcmp to work with executions in a different way than the construction of macros, for this purpose the created functions are stored in a specific library in the outlib parameter.

proc fcmp outlib=work.sas_functions.dateFunctions;

    /*
    =================== 
     * FUNCTION:isBrHoliday
     * AUTHOR: Dutra - dutra@relevants.org
     * DESCRIPTION: Tests a date to validate if it is a 
Brazilian national holiday, returning 0 or 1.
    ===================
    */
    function isBrHoliday(date);
        year                = year(date);
        anonovo             = mdy(01,01,year);
        tiradentes          = mdy(04,21,year);
        diaTrabalho         = mdy(05,01,year);
        independencia       = mdy(09,07,year);
        nsraAparecida       = mdy(10,12,year);
        finados             = mdy(11,02,year);
        republica           = mdy(11,15,year);
        natal               = mdy(12,25,year);
        pascoa              = holiday('easter', year);
        segCarnaval         = pascoa - 48;
        terCarnaval         = pascoa - 47;
        sextaPaixao         = pascoa - 2;
        corpusChristi       = pascoa + 60;

        if date in (anonovo, segCarnaval, terCarnaval,
 sextaPaixao, pascoa, corpusChristi, tiradentes, diaTrabalho,
 independencia, nsraAparecida, finados, republica, natal
               ) then
            return(1);
        else 
            return(0);
    endsub;

quit;

options cmplib=work.sas_functions; 
Enter fullscreen mode Exit fullscreen mode

To use the function just call passing the date parameter. As in the following example:

 /*
Function test, defining the date and passing it as a parameter of
the isHoliday function
*/
%let date=%sysfunc(mdy(12,25,2023));

%let isHoliday=%sysfunc(isBrHoliday(&date.));

%put Date=&date. - isHoliday=&isHoliday.;
Enter fullscreen mode Exit fullscreen mode

I hope it can help with your projects. I will also make new posts available about other functions that I use to handle dates.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more