Dear All,
I have data in wide format with a start date and an end date. I want to add 30 days to the start date until the end date. How do I do this?
The final data should look like this:
ID Start_date end_date start_time end_time
1 1/1/2019 5/1/2019 0 30
1 1/1/2019 5/1/2019 30 60
1 1/1/2019 5/1/2019 60 90
2 1/1/2018 9/1/2019 0 30
2 1/1/2018 9/1/2019 0 60
2 1/1/2018 9/1/2019 0 90
2 1/1/2018 9/1/2019 0 120
...
Thanks!
data want;
set have;
start_time = 0;end_time=0;
do date=start_date to end_date by 30;
end_time+30;
output;
start_time+30+1;
end;
run;
This may need changes depending on how your input data is specified, but will get you started. You do need to think about how to end the series when it's not exactly a 30 day time period.
@sarahsasuser wrote:
Dear All,
I have data in wide format with a start date and an end date. I want to add 30 days to the start date until the end date. How do I do this?
The final data should look like this:
ID Start_date end_date start_time end_time
1 1/1/2019 5/1/2019 0 30
1 1/1/2019 5/1/2019 30 60
1 1/1/2019 5/1/2019 60 90
2 1/1/2018 9/1/2019 0 30
2 1/1/2018 9/1/2019 0 60
2 1/1/2018 9/1/2019 0 90
2 1/1/2018 9/1/2019 0 120
...
Thanks!
Thanks Reza, this is almost what I want. As other people have pointed out there is another variable in the dataset. This variable is called other date and the step after this is to see whether this date is within the time frame of start_time and end_time for each row. Reza's code creates multiple rows with the same value for other_date and then starts the counter over for each unique date, which it should not.
ID Start_date end_date start_time end_time other_date
1 1/1/2019 5/1/2019 0 30 1/30/2019
1 1/1/2019 5/1/2019 30 60 2/15/2019
1 1/1/2019 5/1/2019 60 90 3/10/2019
2 1/1/2018 9/1/2019 0 30 1/30/2018
2 1/1/2018 9/1/2019 0 60 2/30/2018
2 1/1/2018 9/1/2019 0 90 3/30/2018
2 1/1/2018 9/1/2019 0 120
It would be helpful if you could tell us what variables you are starting with. Just ID, start date, and end date or you starting with start_time and end_time also? Another question, are you wanting exactly 30 days from the start_date even if it is in a different month? For example, 30 days from 2/1/2019 would be in March not February.
Can't agree more. I was about to mention the same but you got it squeaky clean. Plus @Reeza has also mentioned "You do need to think about how to end the series when it's not exactly a 30 day time period. "
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.