Filling in blanks

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 120
Accepted Solution

Filling in blanks

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


Accepted Solutions
Solution
‎10-15-2015 07:47 PM
Respected Advisor
Posts: 4,972

Re: Filling in blanks

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.

View solution in original post


All Replies
Grand Advisor
Posts: 10,210

Re: Filling in blanks

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?

 

Solution
‎10-15-2015 07:47 PM
Respected Advisor
Posts: 4,972

Re: Filling in blanks

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.

Valued Guide
Posts: 763

Re: Filling in blanks

[ Edited ]

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.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 274 views
  • 0 likes
  • 4 in conversation