楼主: sasuser168
3730 9

如何用SAS循环填补缺失月份的数据(均值填补) [推广有奖]

  • 2关注
  • 0粉丝

本科生

44%

还不是VIP/贵宾

-

威望
0
论坛币
163 个
通用积分
0.0001
学术水平
1 点
热心指数
0 点
信用等级
0 点
经验
173 点
帖子
31
精华
0
在线时间
105 小时
注册时间
2014-1-19
最后登录
2017-2-20

楼主
sasuser168 发表于 2014-3-8 10:31:18 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
如何用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的均值。
求大神指点程序。
二维码

扫码加我 拉你入群

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

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

关键词:如何用 month mont 11月份 Mon 如何

沙发
intheangel 学生认证  发表于 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。。。。
我是一只瘦瘦的小猪~~~
╭︿︿︿╮
{/-◎◎-/}
( (oo) )
  ︶︶︶

藤椅
intheangel 学生认证  发表于 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;
已有 2 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
sasuser168 + 1 + 1 + 1 热心帮助其他会员
admin_kefu + 100 热心帮助其他会员

总评分: 论坛币 + 100  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

我是一只瘦瘦的小猪~~~
╭︿︿︿╮
{/-◎◎-/}
( (oo) )
  ︶︶︶

板凳
sasuser168 发表于 2014-3-8 17:34:18

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

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

报纸
yongyitian 发表于 2014-3-8 22:16:56
下面的程序添充一行缺失值。如果有多个连续的缺失值,可以将下面的程序重复运行。
  1. data filled;
  2.    do until(last.dm);
  3.       set test;
  4.       by dm;
  5.        month_lag = lag(month);
  6.            x_lag = lag(x);
  7.        if not missing(month_lag) and month - month_lag = 1 then output;
  8.         else
  9.        if month - month_lag > 1 then do;
  10.          temp_m = month;
  11.          temp_x = x;
  12.           month = month_lag+1;
  13.               x = mean(x_lag, x);
  14.           output;
  15.           month = temp_m;
  16.               x = temp_x;
  17.           output;
  18.        end;
  19.    end;
  20.    keep dm month x;
  21. run;
复制代码
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
lachance + 1 + 1 + 1 精彩帖子

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

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

7
sasuser168 发表于 2014-3-13 19:03:14

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

intheangel 发表于 2014-3-8 16:21
data tmp;
input dm $ month x;
cards;
不好意思,好像运行不出来,您再帮着看看

8
sasuser168 发表于 2014-3-13 19:04:51

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

yongyitian 发表于 2014-3-8 22:16
下面的程序添充一行缺失值。如果有多个连续的缺失值,可以将下面的程序重复运行。
程序运行完觉得错的了,运行出来增加了13月份~您再帮着看看~

9
intheangel 学生认证  发表于 2014-3-13 19:14:13
sasuser168 发表于 2014-3-13 19:03
不好意思,好像运行不出来,您再帮着看看
把错误提示给我看看,亲~
我是一只瘦瘦的小猪~~~
╭︿︿︿╮
{/-◎◎-/}
( (oo) )
  ︶︶︶

10
yongyitian 发表于 2014-3-13 20:36:13
sasuser168 发表于 2014-3-13 19:03
不好意思,好像运行不出来,您再帮着看看
data test;
input dm month  x;
datalines;
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
;
data filled;
   do until(last.dm);
      set test;
      by dm;
      month_lag = lag(month);
        x_lag = lag(x);
        if first.dm then output;
        else
          if month - month_lag > 1 then do;
            temp_m = month;
            temp_x = x;
            month = month_lag+1;
                x = mean(x_lag, x);
            output;
            month = temp_m;
                x = temp_x;
            output;
          end; else output;
   end;
   keep dm month x;
run;
proc print data=filled; title 'filled'; run; title;

Filled.JPG

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-24 12:29