BookmarkSubscribeRSS Feed
SAS_INFO
Quartz | Level 8

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;

 

 

 

5 REPLIES 5
andreas_lds
Jade | Level 19

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?

Kurt_Bremser
Super User

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.

Astounding
PROC Star

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

Kurt_Bremser
Super User

@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.

Patrick
Opal | Level 21

@SAS_INFO

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;

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2982 views
  • 1 like
  • 5 in conversation