DATA Step, Macro, Functions and more

Forward or backward fill missing values

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 91
Accepted Solution

Forward or backward fill missing values

I'm trying to fill missing values in a sorted set.  So if the previous observation is 1 and the next is NULL then it should be 1.  Basically I'm trying to replicated the functionality of pandas.DataFrame.fillna(method='ffill').  


Accepted Solutions
Solution
‎01-16-2018 11:28 AM
PROC Star
Posts: 1,570

Re: Forward or backward fill missing values

Posted in reply to tomcmacdonald

here is a demo:

 

data have;
do n=1 to 10;
if n in (1,4,7) then k=1;
else k=.;
output;
end;
run;

data want;
set have;
retain _k;
if not missing(k) then _k=k;
else if missing(k) then k=_k;
drop _k;
run;

View solution in original post


All Replies
Solution
‎01-16-2018 11:28 AM
PROC Star
Posts: 1,570

Re: Forward or backward fill missing values

Posted in reply to tomcmacdonald

here is a demo:

 

data have;
do n=1 to 10;
if n in (1,4,7) then k=1;
else k=.;
output;
end;
run;

data want;
set have;
retain _k;
if not missing(k) then _k=k;
else if missing(k) then k=_k;
drop _k;
run;
☑ This topic is solved.

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

Discussion stats
  • 1 reply
  • 158 views
  • 0 likes
  • 2 in conversation