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;
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;
What does your desired result look like then?
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
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;
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
data want;
update have(obs=0) have;
by id;
output;
run;
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.