Hi, I have the below code written. Macro bus_str_m1 and current_ui_m1 resolves to '31-Jun-2014' when dd = 31July2014. However '31-Jun-2014' is an invalid date, I would like it to be '30-Jun-2014'. How would I re-write the code if I want the variables work the same on all other days except for month end or invalid dates? Is there a way SAS can automatically check for invalid dates?
data _null_;
CALL SYMPUT('yyyymmdd', put(today(), yymmddn8.) );
CALL SYMPUT('yyyymm', put(today(), yymmn6.) );
run;
data _null_;
dd = day(today());
MONTH_STR_M1 = intnx('month',input(put(&yyyymm, 6.), yymmn6.), 0);
MONTH_END_M1 = intnx('month',input(put(&yyyymm, 6.), yymmn6.), 1);
MONTH_END_P1 = intnx('month',input(put(&yyyymm, 6.), yymmn6.), -1);
MONTH_CURR_UI = intnx('month',input(put(&yyyymm, 6.), yymmn6.), -13);
CALL SYMPUT('bur_str_m1', "'"||put(dd,z2.)||"-"||PUT(MONTH_END_P1, monname3.)||"-"||put(MONTH_STR_M1,year4.)||"'");
CALL SYMPUT('bur_end_m1', "'"||put(dd,z2.)||"-"||PUT(MONTH_STR_M1, MONNAME3.)||"-"||PUT(MONTH_END_M1,YEAR4.)||"'");
CALL SYMPUT('current_ui_m1', "'"||put(dd,z2.)||"-"||PUT(MONTH_CURR_UI, MONNAME3.)||"-"||PUT(MONTH_CURR_UI,YEAR4.)||"'");
CALL SYMPUT('pre_yyyymm', PUT(MONTH_END_P1,yymmn6.));
run;
%put &bur_str_m1 &bur_end_m1 ¤t_ui_m1 &pre_yyyymm;
'31-Jun-2014' '31-Jul-2014' '31-Jun-2013' 201406
Thanks,
Santosh
1/ Do not mix up the different dates doing a calculation on your own.
The intnx is meant to get a correct date calculated to specific interval. see alignment: SAS(R) 9.4 Functions and CALL Routines: Reference, Second Edition
2/ getting back to a formatted date, just use sas the correct formats on a date (date/time) value, no need to compose something yourself.
Use the intnx function to return the same day of the previous month:
intnx('month',datevar,-1,'s').
SAS designed this very intelligently for month's with 31 days. In the event that the previous month does not have as many days, it will default to the end of the month. So running this with a current date of 3/29/2014 would return 2/28/2014 as the same day from the previous month.
Then, rather than build your own string, just use the date11 format:
data _null_;
dd = day(today());
pd = intnx('month',dd,-1,'s');
CALL SYMPUT('bur_str_m1', "'"||put(pd,date11.)||"'");
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.