DATA Step, Macro, Functions and more

How to create date series from start and end dates?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 91
Accepted Solution

How to create date series from start and end dates?

SAS folks-

I’m looking for an easy way to generate a series of SAS dates given the start date and end date in macro variables (daily or monthly is typical).  I run into this occasionally, and have not found a solution that always works.

Given macro variables START_DATE of 01apr1998 and END_DATE of 11feb2014, then ????

We are running SAS 9.3, and we have ETS, STAT, and GRAPH.

Thanks so much for your help!

Wendy T


Accepted Solutions
Solution
‎02-11-2014 01:42 PM
Super User
Posts: 17,963

Re: How to create date series from start and end dates?

Using the intnx function, you can change the month to days for different time periods, ie week, month, quarter etc. Check the documentation for the options Smiley Happy

%let start_date=01Apr1998;

%let end_date=11feb2014;

data want_month;

date="&start_date"d;

do while (date<="&end_date"d);

    output;

    date=intnx('month', date, 1, 's');

end;

format date date9.;

run;

data want_day;

date="&start_date"d;

do while (date<="&end_date"d);

    output;

    date=intnx('day', date, 1, 's');

end;

format date date9.;

run;

View solution in original post


All Replies
Super User
Posts: 17,963

Re: How to create date series from start and end dates?

%let start_date=01Apr1998;

%let end_date=11feb2014;

data want;

do date="&start_date"d to "&end_date"d;

output;

end;

format date date9.;

run;

Solution
‎02-11-2014 01:42 PM
Super User
Posts: 17,963

Re: How to create date series from start and end dates?

Using the intnx function, you can change the month to days for different time periods, ie week, month, quarter etc. Check the documentation for the options Smiley Happy

%let start_date=01Apr1998;

%let end_date=11feb2014;

data want_month;

date="&start_date"d;

do while (date<="&end_date"d);

    output;

    date=intnx('month', date, 1, 's');

end;

format date date9.;

run;

data want_day;

date="&start_date"d;

do while (date<="&end_date"d);

    output;

    date=intnx('day', date, 1, 's');

end;

format date date9.;

run;

Frequent Contributor
Posts: 91

Re: How to create date series from start and end dates?

Reeza-

Thanks so much for your elegant answers - they were just what I needed! 

Sorry for the delay in getting them marked - for some reason I have been unable to access the buttons.

Wendy T

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 5173 views
  • 4 likes
  • 2 in conversation