BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
aman23
Obsidian | Level 7

How to get the date of 1st monday from previous month of today's date in SAS.

i.e: if today is 22ndFeb2019 then i need 7thJan2019 as output.

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, intnx() moves a date by a given period.  That will get you someways, then you just need to find the next monday.  Maybe;

data want;
  last_month=intnx('month',today(),-1,'b');
  do while(weekday(last_month) ne 2);
    last_month=last_month+1;
  end;  
  format last_month date9.;
run;

View solution in original post

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, intnx() moves a date by a given period.  That will get you someways, then you just need to find the next monday.  Maybe;

data want;
  last_month=intnx('month',today(),-1,'b');
  do while(weekday(last_month) ne 2);
    last_month=last_month+1;
  end;  
  format last_month date9.;
run;
aman23
Obsidian | Level 7

can you plz explain the code after d while, how it worked or logic that you have used

i.e :  

do while(weekday(last_month) ne 2);  ( what ne 2 resolved here??)
    last_month=last_month+1;  (why value of last_month incremented by 1??)
Kurt_Bremser
Super User
  1. The result of the weekday function is a number between 1 and 7 (1=Sunday, 7=Saturday). So it tests for Monday
  2. SAS dates are counts of days, so adding 1 to a date means "skip to the next day".
andreas_lds
Jade | Level 19

Use intnx (as suggested by @RW9 ) to get a valid date for last month, then use function nwkdom to get the first monday.

data _null_;
	lastMonth = intnx('MONTH', today(), -1, 's');
	pmm = nwkdom(1, 2, month(lastMonth), year(lastMonth));
	put pmm date9.;
run;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Nice, never used nwkdom()!

Kurt_Bremser
Super User

@andreas_lds wrote:

Use intnx (as suggested by @RW9 ) to get a valid date for last month, then use function nwkdom to get the first monday.

data _null_;
	lastMonth = intnx('MONTH', today(), -1, 's');
	pmm = nwkdom(1, 2, month(lastMonth), year(lastMonth));
	put pmm date9.;
run;


@andreas_lds wrote:

Use intnx (as suggested by @RW9 ) to get a valid date for last month, then use function nwkdom to get the first monday.

data _null_;
	lastMonth = intnx('MONTH', today(), -1, 's');
	pmm = nwkdom(1, 2, month(lastMonth), year(lastMonth));
	put pmm date9.;
run;

Maxim 9 at work again 😉

Kurt_Bremser
Super User

You can do this in a formula by doing a double mod():

data test;
format prev_month yymmddd10.;
do i = -1 to -24 by -1;
  prev_month = intnx('month',today(),i,'b');
  prev_month = prev_month + mod(mod(2-weekday(prev_month),7)+7,7);
  x1 = weekday(prev_month);
  output;
end;
run;

I used a loop to show that it always results in weekday of 2 (Monday).

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 7 replies
  • 2808 views
  • 8 likes
  • 4 in conversation