BookmarkSubscribeRSS Feed
gahlot1999
Fluorite | Level 6

Hi Community,

In my dataset, there are multiple missing values. I want those missing values to be filled with last value. If two or more than two missing value then all consecutive missing values should take last value which existed.

For ex.

Source Data:

SourceOutput

1

1

11
11
22
22
 2
 2
 2
44

 

1 REPLY 1
Tom
Super User Tom
Super User

That is want is called Last Observation Carried Forward (LOCF).

For that data a simple retained variable will work.  Note that the retained variable needs to be a NEW variable (not one already in the source dataset) otherwise the retained value is overwritten when the next observation is read.

data have;
  input Source Output;
cards;
1 1
1 1
1 1
2 2
2 2
. 2
. 2
. 2
4 4
;

data want;
  set have;
  want = coalesce(source,want);
  retain want;
run;

Result

Obs    Source    Output    want

 1        1         1        1
 2        1         1        1
 3        1         1        1
 4        2         2        2
 5        2         2        2
 6        .         2        2
 7        .         2        2
 8        .         2        2
 9        4         4        4

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1 reply
  • 223 views
  • 0 likes
  • 2 in conversation