全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
2551 5
2013-07-08
悬赏 2500 个论坛币 已解决
有很多个SHEET,需要对SHEET里面的一列数据进行就近填补,数据如下:
obs x
1 .
2..
3 .
4 Non-missing value
5Non-missing value
6 .
7 .
8 .
9 .
10 .
11 .
12 Non-missing value
13 .
14 .

想达到如下目的:1)就近填补缺失值,整列都要填满。
2)如果是连续的缺失值,比如OBS6-OBS10,离上面近的用上面的值填补,离下面近的用下面的填补。
2)如果缺失值里离上下的值一样近,那么就用上面的填补。
3)多个SHEET,我已经导入了WORK库,如果填补呢4) 每个SHEET里面NON-MISSING value 数量不确定,但是每个SHEET中变量都统一为X,OBS相同
跪求高手,2500论坛币相送。

最佳答案

lutian08 查看完整内容

proc sql noprint; create table result as select * from ( select distinct a.id,b.id as id_match,a.old_value,b.old_value as new_value from test as a,test as b where b.old_value ne . group by a.id having a.id=b.id or (abs(a.id-b.id)=min(abs(a.id-b.id)) )) ...
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2013-7-8 20:17:32
proc sql noprint;

create table result

        as select * from (

                select distinct a.id,b.id as id_match,a.old_value,b.old_value as new_value

                        from test as a,test as b

                        where b.old_value ne .

                        group by a.id

                        having a.id=b.id or (abs(a.id-b.id)=min(abs(a.id-b.id)) ))

        group by id

        having id_match=min(id_match);

quit;

如果数据比较大,不知这个速度如何
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-7-8 21:30:50
data ffile;
input obs x;
cards;
1 .
2 566
3 .
4 .
5 .
6 .
7 .
8 .
9 .
10 .
11 998
12 .
13 .
14 223
15 .
16 .
17 .
18 87
19 .
20 56
21 .
22 .
23 .
24 .
25 .
;
run;
proc print;run;

data ffile1;
set ffile;
order=_n_;
x_origin=x;
retain ord_1 0;
if missing(x) then ord_1=ord_1+1;else ord_1=0;
run;

proc sort data=ffile1 ;
by  descending order;
run;

data ffile1;
set ffile1;
retain ord_2 0;
if missing(x) then ord_2=ord_2+1;else ord_2=0;
run;

proc sort data=ffile1 ;
by  order;
run;

data ffile2;
set ffile1;
if ord_1<=ord_2 and ord_1^=0 then inter=1;
if ord_1>ord_2  then inter=2;
run;

data ffile3;
set ffile2;
retain value;
if x^=. then value=x;
if inter=1 then x=value;
drop value;
run;

proc sort data=ffile3 ;
by  descending order;
run;

data ffile4;
set ffile3;
retain value;
if x^=. then value=x;
if inter=2 then x=value;
drop value;
run;

data ffile5;
set ffile4;
retain value;
if x^=. then value=x;
if x=. then x=value;
drop value;
run;

proc sort data=ffile5 ;
by  order;
run;

data ffile6;
set ffile5;
retain value;
if x^=. then value=x;
if x=. then x=value;
drop value;
run;

proc print ;run;

问题解决了,楼主再简单调下就行了
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-7-8 22:12:32
lutian08 发表于 2013-7-8 21:58
proc sql noprint;

create table result
Dear my friend,I will try, and thank you for your reply.
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-7-9 08:02:01
lutian08 发表于 2013-7-8 21:30
data ffile;
input obs x;
cards;
今天用第二种方法跑了一遍,计算量确实很大,LOG平均2分钟满一次屏
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-7-9 08:39:34
lutian08 发表于 2013-7-8 20:17
proc sql noprint;

create table result
这不就是前一个帖子的答案吗
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群