BookmarkSubscribeRSS Feed
Felix_Gartner
Calcite | Level 5

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

3 REPLIES 3
andreas_lds
Jade | Level 19

If you want to make things more efficient, don't use macro-code in cases that are easily solved by using simple data-steps.

s_lassen
Meteorite | Level 14

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):

  • INFORMAT is the informat used to read STARTDATE and ENDDATE.
  • FORMAT is the format used on the output, if you use e.g. 5.0, you will get a list of raw SAS dates, which can be used in an IN clause.
  • INCREMENT is the increment used in INTNX, so that the macro can also be used to generate e.g. a list of monthly dates.
  • DELIM is the output delimiter.

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 560 views
  • 0 likes
  • 4 in conversation