BookmarkSubscribeRSS Feed
sarahsasuser
Quartz | Level 8

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!

6 REPLIES 6
Reeza
Super User
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!


 

sarahsasuser
Quartz | Level 8

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

Reeza
Super User
I think you need to show more details of what you have and what you need then. If this all to determine if a date is within a period of enrollment, this may not be the best approach either.
mcgaugh
Fluorite | Level 6

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. 

novinosrin
Tourmaline | Level 20

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. "  

sarahsasuser
Quartz | Level 8
I need the start stop dates in 30 day increments like this for a survival analysis. The start, stop is my time scale and then the other_dates indicate whether the exposure occurred within that time period.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 6 replies
  • 2183 views
  • 2 likes
  • 4 in conversation