DATA Step, Macro, Functions and more

how to find the first monday after 5th of every month

Reply
Contributor
Posts: 64

how to find the first monday after 5th of every month

[ Edited ]

Hi All,

 

I need to finds the first Monday after 5th of every month.

Please help

 

I used the following code to find the 5th of every month

 

data a;

format rundate date9.;

rundate=(intnx('Month',today(),+1))+4;

run;

 

 

 

Valued Guide
Posts: 574

Re: how to find the first monday after 5th of every month

Posted in reply to arunasaran

You are using the correct function, but the parameters are wrong.

 

data bob;
   fifth = '05Jun2018'd;
   target = intnx('WEEK', fifth, 1, 'B') + 1;

   format fifth target ddmmyyp10.;
run;

What do you expect if the 5th is a Monday?

Super User
Posts: 10,255

Re: how to find the first monday after 5th of every month

Posted in reply to arunasaran

Use a formula that is based on weekday(), and create a modulated offset:

data test;
input mydate yymmdd10.;
fifth = intnx('month',mydate,0,'b') + 4;
mon_after_fifth = fifth + mod(9 - weekday(fifth),7);
format mydate fifth mon_after_fifth yymmddd10.;
wd = weekday(mon_after_fifth); /* just to be sure we always get a monday */
cards;
2018-06-02
2018-05-10
2018-03-02
;
run;

I put the March date in because 2018-03-05 was a Monday.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 6,775

Re: how to find the first monday after 5th of every month

Posted in reply to KurtBremser

If 03-05-2018 is a Monday, shouldn't the program return 03-12-2018 instead of 03-05-2018?

Super User
Posts: 10,255

Re: how to find the first monday after 5th of every month

Posted in reply to Astounding

@Astounding wrote:

If 03-05-2018 is a Monday, shouldn't the program return 03-12-2018 instead of 03-05-2018?


That would need a little tweaking, but only the OP can give us the definite answer.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Respected Advisor
Posts: 4,736

Re: how to find the first monday after 5th of every month

Posted in reply to arunasaran

@arunasaran

Can't think of anything much prettier than below to return for any given current_date a run_date of the next Monday of the month on the 5th or after AND this run_date also always being >= current_date.

 

data sample;
  format currdate rundate _mon5orAfter weekdate17.;
  do currdate='01jan2018'd to today();
    _mon5orAfter=intnx('week.3',intnx('month',currdate,0,'b')+4,0,'e');
    rundate=ifn(currdate>_mon5orAfter,intnx('week.3',intnx('month',currdate,1,'b')+4,0,'e'),_mon5orAfter);
    output;
  end;
  stop;
run;

 

Ask a Question
Discussion stats
  • 5 replies
  • 124 views
  • 1 like
  • 5 in conversation