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