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.

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay