Dear SAS-experts,
I want to make my progamming code more efficient by generating a list of date values from a %macro program. Based on two date value macro's (startdate and enddate) I would like to end up getting a macro-value (&date2) that holds a list of five years on the 1st of January that I can then use in other programs as list values.
So far I came up with:
%let startdate = 01-01-2017;
%let enddate = 01-01-2021;
%macro date_test;
%let start=%sysfunc(inputn(&startdate,ddmmyy10.));
%let end=%sysfunc(inputn(&enddate,ddmmyy10.));
%let diff=%sysfunc(intck(year,&start,&end));
%do i=0 %to &diff;
%global date2;
%let date=%sysfunc(intnx(year,&start,&i,b),ddmmyy10.);
%let date2=&date;
%end;
%put &date2;
%mend date_test;
%date_test
In the %do statement it performs well, but I just don't know how to write and retain these values. Can anyone please help me or guide me in the right direction?
Thanks in advance!
Regards,
Ferdinand
If you want to make things more efficient, don't use macro-code in cases that are easily solved by using simple data-steps.
Why don't you create the series of dates wherever you need them for program control? It's only a DO or %DO loop.
One solution is to create a macro that generates the list on the fly:
%macro date_list(startdate,enddate,informat=ddmmyy10.,format=ddmmyy10.,increment=year,delim=%str(,));
%local start end diff i;
%let start=%sysfunc(inputn(&startdate,&informat));
%let end=%sysfunc(inputn(&enddate,&informat));
%let diff=%sysfunc(intck(&increment,&start,&end));
%do;%sysfunc(putn(&start,&format))%end;
%do i=1 %to &diff;&delim%sysfunc(intnx(year,&start,&i,b),&format)%end;
%mend;
%put %date_list(01-01-2017,01-01-2021);
I made the macro a bit more flexible by adding some parameters (with defaults):
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.