DATA Step, Macro, Functions and more

Distribute Days between 2 dates

Accepted Solution Solved
Reply
Contributor
Posts: 64
Accepted Solution

Distribute Days between 2 dates

Hi there

 

Don't know if this is feasible in SAS but I know its possible in other tools.

 

If I have a data set and there is 2 dates e.g.

1/Jan/2017 and 22/Apr/2017 so there is 111 days betwen these 2 dates.

 

What I want to achieve is distributing these days to a particular month, so the first 30 days go to Jan 17, the second 28 go to Feb 17, 31 to Mar and 22 to Apr

 

Any ideas if this can be done?

 

Kind Regards

Aidan 


Accepted Solutions
Solution
‎08-22-2017 07:37 AM
Respected Advisor
Posts: 4,791

Re: Distribute Days between 2 dates

[ Edited ]

@Aidan

You haven't shown us how the desired result should look like so not sure if below will give you what you're after. It should point you into the right direction though especially when it comes to working with SAS Date values.

data have;
  infile datalines truncover dlm=' ';
  input Item Date:anydtdte. Days;
  format date date9.;
  datalines;
1 22-Apr-14 4
2 11-Jun-14 3
3 27-Feb-14 114
;
run;

data want;
  set have;
  format target_date date9.;
  do target_date=date to date+days;
    output;
  end;
run;

 

View solution in original post


All Replies
Respected Advisor
Posts: 4,791

Re: Distribute Days between 2 dates

@Aidan

There is certainly a solution but you need to better explain what you have and what you need; i.e: where are these 2 dates? On a single row in 2 variables and you want to generate multiple rows with consecutive dates?

 

Best provide a SAS data step which creates a HAVE data set and then show us how the desired result should look like.

Contributor
Posts: 64

Re: Distribute Days between 2 dates

So the below is something that I would have for instance.

 

So item 1 keeps the days to April 2014

Item 2 has 3 for Jun 2014

Item 3 then is spread across more than 1 month

Hope this helps.

 

ItemDate 1Days 1 
122-Apr-144 
211-Jun-143 
327-Feb-14114 

 

 

Solution
‎08-22-2017 07:37 AM
Respected Advisor
Posts: 4,791

Re: Distribute Days between 2 dates

[ Edited ]

@Aidan

You haven't shown us how the desired result should look like so not sure if below will give you what you're after. It should point you into the right direction though especially when it comes to working with SAS Date values.

data have;
  infile datalines truncover dlm=' ';
  input Item Date:anydtdte. Days;
  format date date9.;
  datalines;
1 22-Apr-14 4
2 11-Jun-14 3
3 27-Feb-14 114
;
run;

data want;
  set have;
  format target_date date9.;
  do target_date=date to date+days;
    output;
  end;
run;

 

Contributor
Posts: 64

Re: Distribute Days between 2 dates

This looks great Patrick I can work with this now, thanks very much for your quick response
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 122 views
  • 2 likes
  • 2 in conversation