问题
Should be an easy question. I have a dataset like this:
id mtna
1 .
2 .
3 1.7
4 .
5 .
6 5.2
7 9.6
8 .
9 .
10 .
And I want something like this:
id mtna
1 .
2 .
3 1.7
4 1.7
5 1.7
6 5.2
7 9.6
8 9.6
9 9.6
10 9.6
Basically just retain the pervious observation when there is a missing value. So I try something like this:
data check; set check;
retain lag_mtna;
lag_mtna=lag1(mtna);
if mtna=. then mtna=lag_mtna;
run;
But then it turns out like this:
id mtna
1 .
2 .
3 1.7
4 1.7
5 .
6 5.2
7 9.6
8 9.6
9 .
10 .
How can I retain all those values? Thanks for any help.
回答1:
There's no need to retain variable that is coming from dataset. Adding few tricks, it could be like this:
data check;
set check (rename=(mtna=orig_mtna));
retain mtna;
mtna=coalesce(orig_mtna, mtna);
drop orig_mtna;
run;
回答2:
Thanks for any attention. I solve it via a simple way.
data check; set check;
retain mtna_new mtna;
if mtna ne . then mtna_new=mtna;
run;
来源:https://stackoverflow.com/questions/19873284/retain-variables-for-missing-value