I'd like to fill missing values with the previous one like in below example:
Date | Value |
2021-08-01 | 10 |
2021-08-02 |
Result:
Date | Value |
2021-08-01 | 10 |
2021-08-02 | 10 |
How can I do it in a fast way?
Please clarify your request.
In the title, you say you want the NEXT value, but in your text you say you want the PREVIOUS value. These are not the same. Which is it?
You can do something like this, but I'm also quite certain that there are additional complexities that you didn't capture in your example. Nonetheless, try this:
data have;
infile datalines missover;
input date :yymmdd10. value;
format date yymmdd10.;
datalines;
2021/08/01 10
2021/08/02 .
;
data want;
set have;
lag_val = lag(value);
if missing(value) then value = lag_val;
drop lag_val;
run;
date value 2021-08-01 10 2021-08-02 10
You can remove the
drop lag_val;
if you want to see what's going on.
... but I'm also quite certain that there are additional complexities that you didn't capture in your example ...
I'm glad you pointed that out @maguiremq , I was thinking the same thing.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.