## Longitudinal Data - Difference between Two dates

Occasional Contributor
Posts: 8

# Longitudinal Data - Difference between Two dates

I would like to calculate difference between the dates in the following data set:

Patient #     visit date

123             12-02-16

123             12-05-16

123             12-10-16

456             01-02-16

456             01-07-16

456             01-14-16

To create result:

Patient #     # of days

123             0

123             3

123             5

456             0

456             5

456             7

Posts: 1,337

## Re: Longitudinal Data - Difference between Two dates

Use the DIF function, which is effectively x=lag(x).  Use it inside an IFN function, to substiitute a zero when at first.id:

``````data have;
input id date :mmddyy10.;  format date date9.;
datalines;
123             12-02-2016
123             12-05-2016
123             12-10-2016
456             01-02-2016
456             01-07-2016
456             01-14-2016
run;

data want;
set have;
by id;
days=ifn(first.id,0,dif(date));
run;``````
Occasional Contributor
Posts: 8

## Re: Longitudinal Data - Difference between Two dates

If i had 2 fields, how would i change the data step?

INPUT:

ID      State      Date

123     MD        1-1-2016

123     MD         1-3-2016

123     TX          2-4-2016

123     TX          2-7-2016

Need Output

ID      State      Days

123     MD        0

123     MD         2

123     TX          0

123     TX          3

PROC Star
Posts: 8,163

## Re: Longitudinal Data - Difference between Two dates

```data want;
set have;
by id state;
days=ifn(first.state,0,dif(date));
run;
```

Art, CEO, AnalystFinder.com

Discussion stats
• 3 replies
• 204 views
• 0 likes
• 3 in conversation