Retain variables for missing value [duplicate]

坚强是说给别人听的谎言 提交于 2019-12-25 09:39:13

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!