BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sam369
Obsidian | Level 7

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

Thanks in advance

Sam

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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


View solution in original post

2 REPLIES 2
Haikuo
Onyx | Level 15

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


sam369
Obsidian | Level 7

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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