## rollover difference between dates

Solved
Regular Contributor
Posts: 171

# rollover difference between dates

Hi All,

I have the following dataset.Pid and labdate.

I need the find the diff between the lbd. if lbd is <35  then , lbd check with next lbd.

In the below example 06apr2006 check with 20 apr2006 if it is less than <35 then 06apr2006 check with 04may2006 and so on..

if it matches with condition then i need to flg it as 1. that means (diff is >35) then flag it as one.

Now the lbd is the

PID               LBD

0090 34686 06APR2006

0090 34686 20APR2006

0090 34686 04MAY2006

0090 34686 18MAY2006

0090 34686 22JUN2006

0090 34686 20JUL2006

0090 34686 31AUG2006

0090 34686 30NOV2006

0090 34686 15FEB2007

0090 34686 09OCT2008

0090 34686 12MAR2009

0090 34686 27AUG2009

0090 34686 25FEB2010

0090 34686 23SEP2010

0090 34686 24MAR2011

want:

PID               LBD                         flg                diff

0090 34686 06APR2006

0090 34686 20APR2006                                    (20apr2006-06apr2006)

0090 34686 04MAY2006                                    (04may2006-06apr2006)

0090 34686 18MAY2006                  1               (18may2006-06apr2006)

0090 34686 22JUN2006                                    (22jun2006-18may2006)

0090 34686 20JUL2006                  1                (20jul2006-18may2006)

0090 34686 31AUG2006                1                   so on.......

0090 34686 30NOV2006               1                   so on.......

0090 34686 15FEB2007               1                   so on.......

0090 34686 09OCT2008                 1

0090 34686 12MAR2009              1

0090 34686 27AUG2009              1

0090 34686 25FEB2010                  1

0090 34686 23SEP2010               1

0090 34686 24MAR2011              1

Sam

Accepted Solutions
Solution
‎10-01-2012 04:44 PM
Posts: 3,167

## Re: rollover difference between dates

Here is an option, had to make up one variable name, and had to assume that the data is presorted by pid uid and lbd. So not 100% sure if this is exactly what  you are after:

data have;

input (PID UID) (:\$10.)  LBD :date9.;

format lbd date9.;

cards;

0090 34686 06APR2006

0090 34686 20APR2006

0090 34686 04MAY2006

0090 34686 18MAY2006

0090 34686 22JUN2006

0090 34686 20JUL2006

0090 34686 31AUG2006

0090 34686 30NOV2006

0090 34686 15FEB2007

0090 34686 09OCT2008

0090 34686 12MAR2009

0090 34686 27AUG2009

0090 34686 25FEB2010

0090 34686 23SEP2010

0090 34686 24MAR2011

;

data want;

set have;

by pid uid lbd;

retain _dt;

diff=lbd-_dt;

_dt=ifn(first.pid or diff>35, lbd, _dt);

flag=ifn(diff>35, 1,.);

drop _:;

run;

proc print;run;

Haikuo

All Replies
Solution
‎10-01-2012 04:44 PM
Posts: 3,167

## Re: rollover difference between dates

Here is an option, had to make up one variable name, and had to assume that the data is presorted by pid uid and lbd. So not 100% sure if this is exactly what  you are after:

data have;

input (PID UID) (:\$10.)  LBD :date9.;

format lbd date9.;

cards;

0090 34686 06APR2006

0090 34686 20APR2006

0090 34686 04MAY2006

0090 34686 18MAY2006

0090 34686 22JUN2006

0090 34686 20JUL2006

0090 34686 31AUG2006

0090 34686 30NOV2006

0090 34686 15FEB2007

0090 34686 09OCT2008

0090 34686 12MAR2009

0090 34686 27AUG2009

0090 34686 25FEB2010

0090 34686 23SEP2010

0090 34686 24MAR2011

;

data want;

set have;

by pid uid lbd;

retain _dt;

diff=lbd-_dt;

_dt=ifn(first.pid or diff>35, lbd, _dt);

flag=ifn(diff>35, 1,.);

drop _:;

run;

proc print;run;

Haikuo

Regular Contributor
Posts: 171

## Re: rollover difference between dates

Thank you so much haikuo.

You made my day. Yo just did it in simple... where i am working so lengthy using lag functions

Thanks

Sam

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
• 2 replies
• 201 views
• 0 likes
• 2 in conversation