全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
3673 9
2014-03-08
如何用SAS循环填补股票缺失月份数据了。
如下:现有2000支股票,下面是一部分 ,手动解决太费事,

    dm

  month

     x

101

200601

0.143852

101

200602

0.107505

101

200603

0.199634

101

200604

-0.04886

101

200605

-0.04173

101

200606

-0.0536

101

200608

0.245487

101

200609

0.097101

101

200610

0.081902

101

200611

0.462759

101

200612

0.288815

102

200701

-0.00842

102

200702

-0.03723

102

200703

0.125509

102

200704

0.089813

102

200705

0.620586

102

200706

-0.01494

102

200707

0.480126

102

200708

0.194346

102

200709

-0.10258

102

200710

0.289073

102

200712

-0.0859


上面101缺失7月份的数据,102武二十11月份的。怎么弄个循环程序让它把2000支股票中凡是有缺失月份的都补上,并且对应的x值为相邻x的均值。
求大神指点程序。
二维码

扫码加我 拉你入群

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

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

全部回复
2014-3-8 16:21:26
data tmp;
input dm $ month x;
cards;
101 200601 0.143852
101 200602 0.107505
101 200603 0.199634
101 200604 -0.04886
101 200605 -0.04173
101 200606 -0.0536
101 200608 0.245487
101 200609 0.097101
101 200610 0.081902
101 200611 0.462759
101 200612 0.288815
102 200701 -0.00842
102 200702 -0.03723
102 200703 0.125509
102 200704 0.089813
102 200705 0.620586
102 200706 -0.01494
102 200707 0.480126
102 200708 0.194346
102 200709 -0.10258
102 200710 0.289073
102 200712 -0.0859
;
run;

proc sort;
by dm;

data tmp_1;
a='%a(';
c=");";
set tmp;
by dm;
if last.dm=1;
file "d:\tmp.txt";
put a $ dm $ c$;
run;

data fc(keep=month dm);
set tmp;
delete;
run;

%macro a(x);
data a_&x;
set tmp;
if dm=&x;
y=dif(month);

data a;
set a_&x;
dif=(x+lag(x))/2;
if y^=1 and y^=. then do;
aa=month-1;
end;
else do;
aa=0;
end;

data b(keep=aa dm dif rename=(aa=month dif=x));
set a;
if aa^=0;
run;

proc sql;
create table c as
select * from b;
quit;

data tmp;
set tmp c;
run;

proc sql;
create table tmp_2 as
select * from tmp
order by month;
quit;
%mend a;
%include "d:\tmp.txt";
run;





我应该是做麻烦了,不过sql检索不太会,so。。。。
二维码

扫码加我 拉你入群

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

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

2014-3-8 16:22:19
data tmp;
input dm $ month x;
cards;
101 200601 0.143852
101 200602 0.107505
101 200603 0.199634
101 200604 -0.04886
101 200605 -0.04173
101 200606 -0.0536
101 200608 0.245487
101 200609 0.097101
101 200610 0.081902
101 200611 0.462759
101 200612 0.288815
102 200701 -0.00842
102 200702 -0.03723
102 200703 0.125509
102 200704 0.089813
102 200705 0.620586
102 200706 -0.01494
102 200707 0.480126
102 200708 0.194346
102 200709 -0.10258
102 200710 0.289073
102 200712 -0.0859
;
run;

proc sort;
by dm;

data tmp_1;
a='%a(';
c=");";
set tmp;
by dm;
if last.dm=1;
file "d:\tmp.txt";
put a $ dm $ c$;
run;

data fc(keep=month dm);
set tmp;
delete;
run;

%macro a(x);
data a_&x;
set tmp;
if dm=&x;
y=dif(month);

data a;
set a_&x;
dif=(x+lag(x))/2;
if y^=1 and y^=. then do;
aa=month-1;
end;
else do;
aa=0;
end;

data b(keep=aa dm dif rename=(aa=month dif=x));
set a;
if aa^=0;
run;

proc sql;
create table c as
select * from b;
quit;

data tmp;
set tmp c;
run;

proc sql;
create table tmp_2 as
select * from tmp
order by month;
quit;
%mend a;
%include "d:\tmp.txt";
run;


结果是tmp_2;
二维码

扫码加我 拉你入群

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

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

2014-3-8 17:34:18

RE: 如何用SAS循环填补缺失月份的数据(均值填补)

intheangel 发表于 2014-3-8 16:22
data tmp;
input dm $ month x;
cards;
谢谢,现在逛街了,晚上回去看看,
二维码

扫码加我 拉你入群

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

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

2014-3-8 22:16:56
下面的程序添充一行缺失值。如果有多个连续的缺失值,可以将下面的程序重复运行。
复制代码
二维码

扫码加我 拉你入群

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

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

2014-3-13 12:10:55
yongyitian 发表于 2014-3-8 22:16
下面的程序添充一行缺失值。如果有多个连续的缺失值,可以将下面的程序重复运行。
看起来很简洁呀!
二维码

扫码加我 拉你入群

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

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

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

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