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
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.