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).
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.