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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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