BookmarkSubscribeRSS Feed
Timzactive
Calcite | Level 5

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

2 REPLIES 2
Reeza
Super User

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;

Timzactive
Calcite | Level 5

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

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 1664 views
  • 3 likes
  • 2 in conversation