DATA Step, Macro, Functions and more

Calculate numbers of months between dates in a long formatted data set

Reply
Occasional Contributor
Posts: 5

Calculate numbers of months between dates in a long formatted data set

Greetings!

 

I'm looking for some assistance in calculating the amount of time between multiple dates by subject id and time point in a long formatted data set.  An example of the data format is below:  

 

IDCompleted DateTimepointTime
12/19/201810
12/20/20182 
12/21/20183 
22/13/201810
22/14/20182 
22/15/20183 
32/15/201510
32/16/20152 

 Thanks in advance for your assistance!

 

v/r,

 

Torrance 

Super User
Posts: 23,323

Re: Calculate numbers of months between dates in a long formatted data set

Posted in reply to crimsonpa2009

It's not clear what you want as output, however, look at:

 

  • LAG() and the DIF() function. 
  • RETAIN

 

LAG() provides the previous value and DIF() provides the difference. Make sure to reset it at the first record for each ID. 

RETAIN would allow you to keep the value across rows, this will be necessary if you're trying to find the date difference from the first date. 

 


crimsonpa2009 wrote:

Greetings!

 

I'm looking for some assistance in calculating the amount of time between multiple dates by subject id and time point in a long formatted data set.  An example of the data format is below:  

 

ID Completed Date Timepoint Time
1 2/19/2018 1 0
1 2/20/2018 2  
1 2/21/2018 3  
2 2/13/2018 1 0
2 2/14/2018 2  
2 2/15/2018 3  
3 2/15/2015 1 0
3 2/16/2015 2  

 Thanks in advance for your assistance!

 

v/r,

 

Torrance 


 

 

Frequent Contributor
Posts: 91

Re: Calculate numbers of months between dates in a long formatted data set

Posted in reply to crimsonpa2009
Esteemed Advisor
Posts: 5,482

Re: Calculate numbers of months between dates in a long formatted data set

Posted in reply to crimsonpa2009

Could you show some example data where the expected amount of time would be something else than zero?

PG
Occasional Contributor
Posts: 5

Re: Calculate numbers of months between dates in a long formatted data set

Basically I would like the new time variable for timepoint 1 to be coded as zero; however, each subsequent time should be the difference between date associated with timepoint 2 or timepoint 3 and timepoint 1. 

 

so the final output should as follows:

 

IDCompleted DateTimepointTime(days)
12/19/201810
12/20/20182 1
12/21/20183 2
22/13/201810
22/14/20182
22/15/20183 1
32/15/201510
32/16/20152 1
PROC Star
Posts: 8,149

Re: Calculate numbers of months between dates in a long formatted data set

Posted in reply to crimsonpa2009

Why is feb 15, for id=2, based on the second rather than the first record for that id?

 

Art, CEO, AnalystFinder.com

 

Super User
Posts: 23,323

Re: Calculate numbers of months between dates in a long formatted data set

Posted in reply to crimsonpa2009
retain baseline;

if timepoint=1 then baseline=completed_date;
else time = completed_date - baseline;
Ask a Question
Discussion stats
  • 6 replies
  • 116 views
  • 0 likes
  • 5 in conversation