BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ronein
Onyx | Level 15

Hello

I want to re-calculate column Y with the following rule:

The calculation is done per customer ID.

IF Y in time 1 is not null and Y in time 2 is null then Y in time 2 will be equal to Y in time 1.

What is the way to do it please?

Data have;
input ID time Y;
cards;
111 1 8
111 2 .
111 3 7
111 4 7
111 5 6
111 6 8
222 1 4
222 2 5
222 3 4
222 4 6
222 5 6
222 6 7
;
Run;

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

The acronym for this is LOCF (Last Observation Carry Forward).

data want;
set have;
by id;
retain _y;
if first.id then _y = y;
if missing(y)
then y = _y;
else _y = y;
drop _y;
run;

View solution in original post

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

What does your desired result look like then?

maguiremq
SAS Super FREQ

This sounds a lot like carrying the last observation forward. It doesn't follow your request parameters, but it gets what you want, I think.

 

Data have;
input ID time Y;
cards;
111 1 8
111 2 .
111 3 7
111 4 7
111 5 6
111 6 8
222 1 4
222 2 5
222 3 4
222 4 6
222 5 6
222 6 7
;
Run;

data want;
	set have;
	by id;
	retain y2;
		if first.id then call missing(y2);
		if not missing(y) then y2 = y;
run;

/* Removing the original variable for cleaner output (not shown) */

data want (drop = y rename = (y2 = y));
	set have;
	by id;
	retain y2;
		if first.id then call missing(y2);
		if not missing(y) then y2 = y;
run;
ID time Y y2 
111 1 8 8 
111 2 . 8 
111 3 7 7 
111 4 7 7 
111 5 6 6 
111 6 8 8 
222 1 4 4 
222 2 5 5 
222 3 4 4 
222 4 6 6 
222 5 6 6 
222 6 7 7 
Ronein
Onyx | Level 15
111 1 8
111 2 8
111 3 7
111 4 7
111 5 6
111 6 8
222 1 4
222 2 5
222 3 4
222 4 6
222 5 6
222 6 7
Kurt_Bremser
Super User

The acronym for this is LOCF (Last Observation Carry Forward).

data want;
set have;
by id;
retain _y;
if first.id then _y = y;
if missing(y)
then y = _y;
else _y = y;
drop _y;
run;
Ronein
Onyx | Level 15

Hello,

I think that you didn't relate your code to the request of filling null value only in second row per cudtomer ID.It means that if 3rd,4th ,5th,6th rows are with null Y value then no need to change it.

The only potetnail change is in 2nd row per customer ID

r_behata
Barite | Level 11
data want;
	update have(obs=0) have;
	by id;
	output;
run;
Ronein
Onyx | Level 15
May you please explain what this code is doing?

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 7 replies
  • 2072 views
  • 2 likes
  • 5 in conversation