Relatively new to SAS and looking for some help with using date functions.
I'm using Today()-x to set dates and making it available for looking at historic data (a rare need, but worth including) and have tried writing an IF statement around it, as I need a slight change for financial year end.
My code is as follows:
%macro RunDate_Variables;
%global rep_month repdt prevdt;
data _null_;
rundate = today()-0;
runmonth = Month(rundate);
%if runmonth eq 04
%then %do;
ReportDate = MDY(4, 4, Year(rundate);
PrevReportDate = intnx('Month', ReportDate,-2,'e');
%end;
%else %if runmonth eq 05;
%then %do;
ReportDate = intnx('Month', rundate,-1,'e');
PrevReportDate = iMDY(4, 4, Year(rundate));
%end;
%else %do;
ReportDate = intnx('Month', rundate,-1,'e');
ReportDate = intnx('Month', ReportDate,-1,'e');
%end;
call symputx('rep_month', put(ReportDate, MONYY7.));
call symputx('repdt', put(ReportDate, yymmddN8.));
call symputx('prevdt', put(PrevReportDate, yymmddn8.));
run;
%mend;
%RunDate_Variables;
Unfortunately, the runmonth doesn't seem to be doing what I'd hoped it would do and therefore isn't resolving as expected within the macro.
Ultimately, what I'm looking for, is for the code to identify when the code is being run in April and set the ReportingDate to 4th April (for Financial Year End) and PrevReportingDate to 28th (or 29th) Feb, and for when the code is run in May for the ReportingDate to be set as 30th April and PrevReportingDate to be set as 4th April.
The code won't need to be run in the space between 31st March and 4th April, so having it resolve to 4th April in that space won't be an issue.
Any suggestions appreciated. The code doesn't need to stick with the format I've attempted, the only bit that needs to remain the same are the variable names rep_month, repdt and prevdt as these were the names used throughout the code when I picked the project up.
EDIT: I've managed to make the idea work using a manually adjusted flag outside the data step and altering the IF conditions, but the issue for this is that it requires either two flags (one for April and one for May), or two separate inputs to be read from the Flag (again, one to recognise April, the other May). Whilst this works, I'd still like to look into the possibility of fully automating this, as FYE is an easy time to remember to alter the flag, but altering the flag in the month after FYE will likely be less memorable.
Thanks.
You cannot check the values of data variables with macro code, your %IF
statement is just comparing the constant string runmonth
to the constant string 04
. They will never be equal.
I don't see anything here that requires any macro logic, but you could still keep the macro definition if you want to call the same data step multiple times.
data _null_;
rundate = today()-0;
runmonth = Month(rundate);
if runmonth eq 04 then do;
ReportDate = MDY(4, 4, Year(rundate);
PrevReportDate = intnx('Month', ReportDate,-2,'e');
end;
else if runmonth eq 05 then do;
ReportDate = intnx('Month', rundate,-1,'e');
PrevReportDate = MDY(4, 4, Year(rundate));
end;
else do;
ReportDate = intnx('Month', rundate,-1,'e');
PrevReportDate = intnx('Month', ReportDate,-1,'e');
end;
call symputx('rep_month', put(ReportDate, MONYY7.),'g');
call symputx('repdt', put(ReportDate, yymmddN8.),'g');
call symputx('prevdt', put(PrevReportDate, yymmddn8.),'g');
run;