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.
Lender | I.D | Count | Date Loan Taken | Date Paid | Newest Date(correct) | Newest Date(actual) |
---|---|---|---|---|---|---|
POP | 554 | 1 | 5/15/2013 | 6/12/2013 | 6/12/2013 | 6/12/2013 |
POP | 554 | 2 | 5/30/2013 | 6/30/2014 | 6/30/2014 | 6/30/2014 |
POP | 554 | 3 | 6/20/2013 | 6/15/2013 | 6/30/2014 | 6/15/2013 |
POP | 554 | 4 | 7/30/2013 | 6/30/2013 | 6/30/2014 | 6/30/2013 |
POP | 788 | 1 | 12/10/2012 | 2/15/2013 | 2/15/2013 | 2/15/2013 |
POP | 788 | 2 | 12/25/2012 | 2/27/2013 | 2/27/2013 | 2/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
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.