Hi,
I have the following code:
%let Report_Date = '31Dec2017'd;
%let yesterday= '30Dec2017'd;
%let last_week = '24Dec2017'd;
DATA example&Report_Date.;
set example;
/* do stuff */
RUN;
DATA example_compare_yesterday;
set example&Report_Date. example&yesterday.;
RUN;
DATA example_compare_last_week;
set example&report_Date. example&last_week.;
RUN;
I would like to make this code iterative, but don't have much of a grounding in macros (which I assume would be appropriate).
I am looking to do the following:
On any given date, "Report_Date" will have the current date
I would like to automatically populate "yesterday" and "last week" with the relevant date such that I can generate the comparative datasets.
Currently I have to populate all three dates to make this work.
Any help would be appreciated.
Thanks!
Look at INTNX to increment yoru dates automatically.
Remember that you can use CALL SYMPUTX within a DATA _NULL_ step to create your macro variables, if that's easier.
I think this has relevant examples for you - including how to loop through dates if desired.
You may need to tweak your code a bit to handle the unquoted/d on the date variable. YOu can either leave them as numbers and trust SAS is doing it correctly, or you can use DATE9 Format to create them as DATE9 date values and quote/add D in the report code instead. The second approach is a bit more user friendly;
data _null_;
report_date = '31Dec2017'd;
yesterday = report_Date - 1;
last_week = report_date - 7;
call symputx('Report_Date', report_date);
call symputx('myDate', put(Report_date, date9.));
.....
....
run;
%put &report_date;
Look at INTNX to increment yoru dates automatically.
Remember that you can use CALL SYMPUTX within a DATA _NULL_ step to create your macro variables, if that's easier.
I think this has relevant examples for you - including how to loop through dates if desired.
You may need to tweak your code a bit to handle the unquoted/d on the date variable. YOu can either leave them as numbers and trust SAS is doing it correctly, or you can use DATE9 Format to create them as DATE9 date values and quote/add D in the report code instead. The second approach is a bit more user friendly;
data _null_;
report_date = '31Dec2017'd;
yesterday = report_Date - 1;
last_week = report_date - 7;
call symputx('Report_Date', report_date);
call symputx('myDate', put(Report_date, date9.));
.....
....
run;
%put &report_date;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.