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?

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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