Hi,
I need to automatically calculate a date in macro variable that corresponds to the 2nd of the following month (i.e. if the code runs in November, the desired date will be 02dec2022).
I use this code to get the 2nd of the current month:
%let wanted_day=2;
%put &wanted_day;
%let wanted_date=%sysfunc(MDY(%sysfunc(month(%sysfunc(today()))),%sysevalf(&wanted_day.),%sysfunc(year(%sysfunc(today())))),date9.);
%put &wanted_date;Can I change it to shift the month or do I need another approach?
Thanks for your help.
First, get this to work in a DATA step, much simpler with fewer %SYSFUNC calls (actually 0 %SYSFUNC calls) and fewer parentheses to possibly mismatch. Then you can use CALL SYMPUTX to create the macro variable.
%let wanted_day=2;
data _null_;
date=today();
day_of_next_month=intnx('month',date,1,'b')+(&wanted_day-1);
call symputx('wanted_date',day_of_next_month);
run;
%put &=wanted_date;
%put %sysfunc(putn(&wanted_date,date9.));
First, get this to work in a DATA step, much simpler with fewer %SYSFUNC calls (actually 0 %SYSFUNC calls) and fewer parentheses to possibly mismatch. Then you can use CALL SYMPUTX to create the macro variable.
%let wanted_day=2;
data _null_;
date=today();
day_of_next_month=intnx('month',date,1,'b')+(&wanted_day-1);
call symputx('wanted_date',day_of_next_month);
run;
%put &=wanted_date;
%put %sysfunc(putn(&wanted_date,date9.));
thanks ! super helpful now I see the logic.
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 save with the early bird rate—just $795!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.