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;

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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