Retaining Lag value for next rows calculation

Reply
New Contributor
Posts: 2

Retaining Lag value for next rows calculation

New here so I'd really appreciate any help I can get.

So I have loan data which is already sorted by lender, I.D. and date_taken.  First, for all count=1 the newest date(created var) = date_paid.  Then I want the code to look at the lagged value(of the new variable(newest date)) to see if it is greater than the current date paid on the loan.  If it is, I want the larger date in the new variable's observation.  This new variable's value should then be looked at to calculate the next observations value.  Below is an example of what IT SHOULD LOOK LIKE and the Right most column is what I actually get. Followed by the current code I am running.

LenderI.DCountDate Loan TakenDate PaidNewest Date(correct)Newest Date(actual)
POP554

1

5/15/20136/12/20136/12/20136/12/2013
POP55425/30/20136/30/20146/30/20146/30/2014
POP55436/20/20136/15/20136/30/20146/15/2013
POP55447/30/20136/30/20136/30/20146/30/2013
POP788112/10/20122/15/20132/15/20132/15/2013
POP788212/25/20122/27/20132/27/20132/27/2013

data input.loan_sequence_count_2;

  set input.loan_sequence_count_1;

     Newest_Date=. ;

run;

data input.loan_sequence_count_3;

  set input.loan_sequence_count_2;

  by lender ID;

  if first.Newest_Date then Newest_Date= Date_Paid;

  else do;

  if Date_Paid> lag(Newest_Date) then Newest_Date= Date_Paid;

  else Newest_Date= lag(Newest_Date);

  retain Newest_Date;

  end;

run;

Now I know that the lag function won't work here except for the second observation of a group.  So I am curious if there is a two step process I can do or something else I haven't thought of to look at the subsequent lag values when creating a new value.

Thanks,

Tim

Super User
Posts: 19,033

Re: Retaining Lag value for next rows calculation

UNTESTED:

Data Want;

Set Have;

BY lender id;

retain latest_date;

if first.id then latest_date=date_paid;

else latest_date=max(date_paid, latest_date);

run;

New Contributor
Posts: 2

Re: Retaining Lag value for next rows calculation

^^^This earlier thread answers my question perfectly with a by statement.  Thanks for your help.

Ask a Question
Discussion stats
  • 2 replies
  • 205 views
  • 3 likes
  • 2 in conversation