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.
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;
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;
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??)
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;
Nice, never used nwkdom()!
@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 😉
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).
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.