Contributor
Posts: 27

# missing consecutive dates

I have a situation like below.

patno visitdate
101 10-feb-09
101 11-feb-09
101 14-feb-09
101 15-feb-09

In the above example dates 12-feb and 13-feb are missing. I want to identify patients where atleast 2 consecutive dates are missing.

Super Contributor
Posts: 359

## Re: missing consecutive dates

Try something like where not first.patno and (visitdate - lag(visitdate) >2);
Posts: 3,852

## Re: missing consecutive dates

Check out DIF function:
[pre]
Returns differences between the argument and its nth lag
[/pre]

[pre]
data pats;
input patno:\$3. visitdate:date.;
cards;
101 10-feb-09
101 11-feb-09
101 14-feb-09
101 15-feb-09
102 10-feb-09
102 13-feb-09
102 14-feb-09
102 16-feb-09
;;;;
run;
data pats(keep=patno);
f = 0;
do until(last.patno);
set pats;
by patno;
d = ifn(not first.patno,dif(visitDate),0);
if not f and d ge 2 then do;
output;
f=1;
end;
end;
run;
proc print;
run;[/pre]
Posts: 4,736

## Re: missing consecutive dates

This approach following Flip's suggestion:

data pats;
input patno:\$3. visitdate:date.;
cards;
101 10-feb-09
101 11-feb-09
101 14-feb-09
101 15-feb-09
102 10-feb-09
102 13-feb-09
102 14-feb-09
102 16-feb-09
103 16-feb-09
103 17-feb-09
103 19-feb-09
;;;;
run;

data want(keep=patno);
set pats;
by patno;
retain flag 0;
if visitdate-lag(visitdate)>2 then
if not first.patno then flag=1;
if last.patno and flag=1 then
do;
output;
flag=0;
end;
run;

proc print data=want;
run;

HTH
Patrick
Discussion stats
• 3 replies
• 217 views
• 0 likes
• 4 in conversation