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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.