Hi,
I have a huge data with a lot of missing values for a variable. For example,
Data a;
input x y;
cards;
1 1
1 0
1 .
1 .
2 2
2 3
2 9
2 .
2 .
;
run;
Now I want to fill in the missing y with its lagged value. That is, for x=1, y=0 for the missing ones and x=2, y=9 for the last two observations. But when I use lag function, it only fills in the first missing observation.
Can anyone please tell me how to do this? Please note, the data is large. So it's better to avoid open/close data too many times.
Thanks,
Sunny
data want(drop=y rename=(y1=y));
set a;
retain y1;
if y>. then y1=y;
run;
It is probably simpler than you thought with UPDATE processing:
data want;
update a(obs=0) a;
by x;
output;
run;
PG
data want(drop=y rename=(y1=y));
set a;
retain y1;
if y>. then y1=y;
run;
The lag function in SAS is a queue functionality and is quite different then the lag en lead functions in Oracle / MS-Sql
For not getting confused avoid using that same name for that.
What you are possible trying to do is known as "impute missings" as that is part of the mining steps.
Just putting in the previous value is not the best strategy to do that, there are several more approaches .
Many thanks all of you!
Thanks,
Sunny
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.