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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 2071 views
  • 1 like
  • 5 in conversation