DATA Step, Macro, Functions and more

expand a dataset of dates to include all days within each range

Reply
Learner
Posts: 1

expand a dataset of dates to include all days within each range

so I have a list of dates, and for merging purposes I would like to get all the days inbetween each one. example:

current dateset:

date

12/16/2016
12/23/2016
12/30/2016
01/06/2017

Desired Dataset:

date days

12/16/201612/16/2016
12/16/201612/17/2016
12/16/201612/18/2016
12/16/201612/19/2016
12/16/201612/20/2016
12/16/201612/21/2016
12/16/201612/22/2016
12/23/201612/23/2016
12/23/201612/24/2016
12/23/201612/25/2016
12/23/201612/26/2016
12/23/201612/27/2016
12/23/201612/28/2016
12/23/201612/29/2016
12/30/201612/30/2016
12/30/201612/31/2016
12/30/20161/1/2017
12/30/20161/2/2017
12/30/20161/3/2017
12/30/20161/4/2017
12/30/20161/5/2017

Thanks!

Super User
Posts: 5,516

Re: expand a dataset of dates to include all days within each range

This may be simplest:

 

data want;

set have;

set have (firstobs=2 rename=(date = next_date));

do days = date to next_date - 1;

   output;

end;

format days mmddyys10.;

drop next_date;

run;

 

There are some tricky aspects, but at least the program is short.

PROC Star
Posts: 1,760

Re: expand a dataset of dates to include all days within each range

Posted in reply to Astounding

Like this?

data HAVE;
input D mmddyy10.;
cards;
12/16/2016 
12/23/2016 
12/30/2016 
01/06/2017 
run;
data WANT; 
  set HAVE;
  CURRENT=D;
  D=lag(D);
  if _N_>1 then do D1=D to CURRENT-1;
    output;
  end;
  keep D D1;
run;
proc print;
  format D D1 date9.;
run;

 

Obs D D1
1 16DEC2016 16DEC2016
2 16DEC2016 17DEC2016
3 16DEC2016 18DEC2016
4 16DEC2016 19DEC2016
5 16DEC2016 20DEC2016
6 16DEC2016 21DEC2016
7 16DEC2016 22DEC2016
8 23DEC2016 23DEC2016
9 23DEC2016 24DEC2016
10 23DEC2016 25DEC2016
11 23DEC2016 26DEC2016
12 23DEC2016 27DEC2016
13 23DEC2016 28DEC2016
14 23DEC2016 29DEC2016
15 30DEC2016 30DEC2016
16 30DEC2016 31DEC2016
17 30DEC2016 01JAN2017
18 30DEC2016 02JAN2017
19 30DEC2016 03JAN2017
20 30DEC2016 04JAN2017
21 30DEC2016 05JAN2017
Ask a Question
Discussion stats
  • 2 replies
  • 203 views
  • 2 likes
  • 3 in conversation