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

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:

 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

v/r,

Torrance

Super User
Posts: 23,323

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

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

v/r,

Torrance

Frequent Contributor
Posts: 91

Posts: 5,482

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

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:

 ID Completed Date Timepoint Time(days) 1 2/19/2018 1 0 1 2/20/2018 2 1 1 2/21/2018 3 2 2 2/13/2018 1 0 2 2/14/2018 2 1 2 2/15/2018 3 1 3 2/15/2015 1 0 3 2/16/2015 2 1
PROC Star
Posts: 8,149

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

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

``````retain baseline;