BookmarkSubscribeRSS Feed
PatrykSAS
Obsidian | Level 7

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?

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
maguiremq
SAS Super FREQ

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.

PaigeMiller
Diamond | Level 26

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

--
Paige Miller
maguiremq
SAS Super FREQ
Things are never as simple as they seem, right?

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 966 views
  • 1 like
  • 3 in conversation