2

Please can someone help me?

I need a code that can identify every 6th of the Month on a rolling period excluding weekends. i would need the code to send an email every time it is past the 6th of each month.

Please don't worry about the email part, I only need a code that can identify the 6th of every month in SAS.

for instance: "If the date of the Month is greater than 6th" then %do

Please help.

Thank you.

Tonas Max
  • 31
  • 4

2 Answers2

0

The key function here is intnx, which lets you skip to the next [whatever day].

Here's an example that hopefully explains the concept.

data dates;
  do date = '01JAN2022'd to '31DEC2022'd;
    weekday = date;
    is_sixth = (date = intnx('WEEKDAY',intnx('MONTH',date,0)+5-1,0,'e')+1);
    output;
  end;
  
  format weekday DOWNAME.;
  format date date9.;
run;

Here we first shift to the start of the month and then add 5 (sort of). That gets us 'sixth of the current month'. Then because SAS's weekday intervals are aligned where Fri,Sat,Sun are one "period", we have to subtract one, move to the end of the period, and add one. That gets us to the 6th or later. If we didn't do the subtract/add dance, we'd get to the 6th or earlier.

Joe
  • 62,789
  • 6
  • 49
  • 67
0

You can check your date directly by seeing if the day is greater than or equal the 6th. If it's a weekend, then do nothing.

data _null_;         
    if(day(today()) GE 6 AND 2 LE weekday(today()) LE 6) put 'It's the after the 6th and not a weekend';
        else put 'It's not after the 6th or it's a weekend.';
run;

If you need to only run your email once a month, create a small database that stores each day an email was sent. If an email was sent that month then do not send an email.

%macro send_email;
    %let today = %sysfunc(today() );

    proc sql noprint;
        select intnx('month', max(email_date), 0, 'B')
        into :check_date
        from lib.email_history
        ;
    quit;

    %if(    %sysfunc(intnx(month, &today., 0, B) ) NE &check_date.
        AND %sysfunc(day(&today.) ) GE 6 
        AND 2 LE %sysfunc(weekday(&today.) ) LE 6
       )
    %then %do;

        /************************/
        /* Email code goes here */
        /************************/

        /* Save a history that an email was sent */
        data email_history;
            email_date = today();
        run;

        proc append base=lib.email_history
                    data=email_history
                    ;
        run; 
    %end;

%mend;
%send_email;
Stu Sztukowski
  • 10,597
  • 1
  • 12
  • 21