My data looks as follows:
LoanID Modification_Year
3322 .
3322 .
3322 .
3322 .
3322 .
3322 2010
3322 .
3322 .
3322 2013
3322 .
1341 .
1341 .
1341 .
1341 .
1341 .
1341 2011
1341 .
1341 .
1341 .
Instead, I would like to have it look like the data below. Does anyone have any code to recommend? Thanks in advance.
LoanID Modification_Year
3322 2010
3322 2010
3322 2010
3322 2010
3322 2010
3322 2010
3322 2010
3322 2010
3322 2013
3322 2013
1341 2011
1341 2011
1341 2011
1341 2011
1341 2011
1341 2011
1341 2011
1341 2011
This sort of problem has appeared before, but without the need to backfill before the first nonmissing. Here is one way:
data first_nonmissing;
set have;
by LoanID;
where Modification_year > .;
if first.LoanID;
rename Modification_year = Replacement_Year;
run;
data want;
merge have first_nonmissing;
by LoanID;
if Modification_year = . then Modification_year = Replacement_Year;
else Replacement_Year = Modification_Year;
run;
A trick to the logic is realizing that Replacement_Year is automatically retained, and never re-read from FIRST_NONMISSING.
It's untested, but it should work.
Good luck.
The main question is will this need to be performed on other data? Since your example data does not have a value for the first record in each of your loanid's then there is some difficulty in knowing what each loan id might need to start with.
Suppose the next loan afte the last shown 1341, call it 9999, also starts with a missing value for modification year. What is the process I would go through to identify the starting value of modification year? I could guess that it might be the minimum value for the year within the loan id group but would that be correct? And by any chance to you have any records with no modification year at all? If so, what to do?
This sort of problem has appeared before, but without the need to backfill before the first nonmissing. Here is one way:
data first_nonmissing;
set have;
by LoanID;
where Modification_year > .;
if first.LoanID;
rename Modification_year = Replacement_Year;
run;
data want;
merge have first_nonmissing;
by LoanID;
if Modification_year = . then Modification_year = Replacement_Year;
else Replacement_Year = Modification_Year;
run;
A trick to the logic is realizing that Replacement_Year is automatically retained, and never re-read from FIRST_NONMISSING.
It's untested, but it should work.
Good luck.
Hi. Given your posted data, to use the correct solution you have to add ...
proc sort data=have;
by loanid;
run;
prior to the two data steps.
Without the sort, you could use BY LOANID NOTSORTED; in the first data step, but not the for a MERGE.
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.