楼主: zs694461601
7292 25

[有偿编程] 怎样找出数据缺失的股票 [推广有奖]

11
bolus123 发表于 2012-6-20 16:36:43 |只看作者 |坛友微信交流群
data a;
input stock $8. @13 month 8. @24 index 8.4;
datalines;
000001      192801     0.3
000001      192802     0.4
000001      192803     0.3
000001      192804     0.4
000001      192805     0.3
000001      192806     0.4
000001      192807     0.3
000001      192809     0.4
000002      192801     0.3
000002      192802     0.8
000002      192804     0.4
000002      192805     0.3
000002      192806     0.4
000002      192807     0.3
000002      192808     0.4
;
run;

proc sort data=a;
by stock month;
run;

data b;
set a;
retain bg_mon 0;
by stock month;
if first.stock then do;bg_mon=month;end;
if last.stock then do;
year1=input(substr(compress(put(bg_mon,8.)),1,4),8.);
month1=input(substr(compress(put(bg_mon,8.)),5,2),8.);
year2=input(substr(compress(put(month,8.)),1,4),8.);
month2=input(substr(compress(put(month,8.)),5,2),8.);

if year1=year2 then do month3=month1 to month2;
                        year3=year1;
                        if month3<10 then do;
                                        month4=input(compress(put(year3,8.))||'0'||compress(put(month3,8.)),8.);
                        end;
                        else do;
                                        month4=input(compress(put(year3,8.))||compress(put(month3,8.)),8.);
                        end;
                        output;
end;

else do year3=year1 to year2;
                        if year3=year1 then do month3=month1 to 12;
                                                if month3<10 then do;
                                                                month4=input(compress(put(year3,8.))||'0'||compress(put(month3,8.)),8.);
                                                end;
                                                else do;
                                                                month4=input(compress(put(year3,8.))||compress(put(month3,8.)),8.);
                                                end;
                                                output;
                        end;
                        else if year3=year2 then do month3=1 to month2;
                                                if month3<10 then do;
                                                                month4=input(compress(put(year3,8.))||'0'||compress(put(month3,8.)),8.);
                                                end;
                                                else do;
                                                                month4=input(compress(put(year3,8.))||compress(put(month3,8.)),8.);
                                                end;
                                                output;
                        end;
                        else do month3=1 to 12;
                                                if month3<10 then do;
                                                                month4=input(compress(put(year3,8.))||'0'||compress(put(month3,8.)),8.);
                                                end;
                                                else do;
                                                                month4=input(compress(put(year3,8.))||compress(put(month3,8.)),8.);
                                                end;
                                                output;
                        end;
                        end;

end;
keep stock month4;
rename month4=month;
run;

proc sort data=b;
by stock month;
run;

data a;
merge a(in=a) b(in=b);
by stock month;
if b;
run;

数据集a中,index缺失的便是缺失的月份

使用道具

12
mymine 发表于 2012-6-20 21:14:23 |只看作者 |坛友微信交流群
data a;
input stk $ time $ c ;
cards;
000001      192801     0.3
000001      192802     0.4
000001      192803     0.3
000001      192804     0.4
000001      192805     0.3
000001      192806     0.4
000001      192807     0.3
000001      192809     0.4
000002      192801     0.3
000002      192802     0.8
000002      192804     0.4
000002      192805     0.3
000002      192806     0.4
000002      192807     0.3
000002      192808     0.4
;
run;

data b;
do i=1900 to 2050;
do j=1 to 12;
output;
end;
end;
run;
data b;
set b;
format k $6. ;
if j/10<1 then k=compress(i||'0'||j);
else k=compress(i||j);
run;

proc sql;
create table c as
select distinct stk,min(time) as tmin,max(time) as tmax
from a group by stk;
quit;
proc sql;
create table d as
select distinct c.stk,k
from c left join b
on tmin<=k<=tmax;
quit;
proc sql;
create table e as
select *
from d
where compress(stk||'*'||k) not in (select compress(stk||'*'||time) from a);
quit;

使用道具

13
wodematlab 发表于 2012-6-20 22:14:07 |只看作者 |坛友微信交流群
不错啊!

使用道具

14
bobguy 发表于 2012-6-21 10:02:55 |只看作者 |坛友微信交流群
Here is a simple idea.

1) build a template with all months
2) merge it with your data

Here is an example,

data template;
do stknum='000001','000002';
   do i=0 by 1 until( yymm='1sep1928'd);
     yymm=intnx('month', '1jan1928'd, i);   
     output;
   end;
  end;
   format yymm yymm7.;
   keep stknum yymm;
run;


data in;
input stknum $6. yearmon $7. v;
yymm=input(substr(yearmon,1,4)||substr(yearmon,5,2)||'01', yymmdd10.);
format yymm yymm7.;
cards;
000001 192801 0.3
000001 192802 0.4
000001 192803 0.3
000001 192804 0.4
000001 192805 0.3
000001 192806 0.4
000001 192807 0.3
000001 192809 0.4
000002 192801 0.3
000002 192802 0.8
000002 192804 0.4
000002 192805 0.3
000002 192806 0.4
000002 192807 0.3
000002 192808 0.4
;

data missing;
  merge in(in=a ) template(in=b);
  by stknum yymm;
  if a=0 and b=1;
  keep stknum yymm;
run;

proc print data=missing;
run;

使用道具

15
jingju11 发表于 2012-6-21 10:44:11 |只看作者 |坛友微信交流群
题目还是挺清楚的。不同的人也有不同的思路和方法。都很不错。
在数据步里。我除了数组的概念,别的也不很理解和擅长。京剧
http://blog.sina.com.cn/s/blog_a3a92636010146h1.html

已有 1 人评分学术水平 热心指数 信用等级 收起 理由
webgu + 1 + 1 + 1 京剧出手,出手不凡!

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

使用道具

16
dyes0506 发表于 2012-6-28 13:30:02 |只看作者 |坛友微信交流群
data test;
input stock $ ym $ target;
cards;
000001 192801 0.3
000001 192802 0.4
000001 192803 0.3
000001 192804 0.4
000001 192805 0.3
000001 192806 0.4
000001 192807 0.3
000001 192809 0.4
000002 192801 0.3
000002 192802 0.8
000002 192804 0.4
000002 192805 0.3
000002 192806 0.4
000002 192807 0.3
000002 192808 0.4
000003 192801 0.3
000003 192802 0.8
000003 192806 0.4
000003 192807 0.3
000003 192808 0.4
;
run;
proc sort data=test;
        by stock ym;
run;
data result;
        set test;
        by stock ym;
        fdate=input(ym,yymmn6.);/*月初*/
        if first.stock ne 1 then do;
                n=intck('month',lag1(fdate),fdate);/*上下两个月所差月份*/
                if n >1 then do;
                        do i=n-1 to 1 by -1;/*将缺失的月份输出*/
                                ym=put(intnx('month',fdate,-i),yymmn6.);
                                target=.;
                                output;/*输出*/
                        end;
                end;
        end;
        drop n i fdate;
run;

proc print data=result;
run;
从数据到结论过程关键的是人不是软件......

使用道具

17
yx95 发表于 2012-9-14 13:00:00 |只看作者 |坛友微信交流群
Try this idea:

data a;
input stk $ time $ c ;
cards;
000001      192801     0.3
000001      192802     0.4
000001      192803     0.3
000001      192804     0.4
000001      192805     0.3
000001      192806     0.4
000001      192807     0.3
000001      192809     0.4
000002      192801     0.3
000002      192802     0.8
000002      192804     0.4
000002      192805     0.3
000002      192806     0.4
000002      192807     0.3
000002      192808     0.4
;
run;

proc sort data=a out=b (keep=stk time) nodupkey; run;

this step will give a unique time for each stock.

next, create a dummy data containing full range of time:

data c;
do time=192801 to 192809;
output;
end;
run;

next merge data c with b, by time:

proc sort data=b; by time; run;

data d;
      merge b   (in = one)
               c   (in = two);
      by time;
      if not one;
run;

proc sort data=d; by stk time; run;

look at data d if it is what you need.
Thanks.

使用道具

18
erzi_yhj 发表于 2013-1-22 10:39:20 |只看作者 |坛友微信交流群
简单的方法找出每只股票首尾月份中缺失的月份。比如如下数据:

data raw_data;
input Code $ Time V;
datalines;
000001 192801 0.3
000001 192802 0.4
000001 192806 0.4
000001 192807 0.3
000001 192808 0.2
000001 192809 0.3
000002 192801 0.3
000002 192802 0.8
000002 192804 0.4
000002 192805 0.3
000002 192806 0.4
000002 192807 0.3
000002 192808 0.4
;
run;

proc sort data=raw_data;
by Code;
run;

data missing_data;
set raw_data;
by Code;
Add_Time=Time-lag(Time)-1;
if first.Code then Add_Time=0;
else
      do i=1 to Add_Time;
                Missing_Time=Time-i;
                output;
      end;

drop Time V Add_time i;              

run;

使用道具

19
zhengbo8 发表于 2013-1-22 14:34:25 |只看作者 |坛友微信交流群
不用宏、SQL,下面的data步方法:

在数据集中创建一个标示标量 is_missing,判断该条观测是否为缺失值的月份。

原始数据文件:data.txt

  1. 000001 192801 0.3
  2. 000001 192802 0.4
  3. 000001 192803 0.3
  4. 000001 192804 0.4
  5. 000001 192805 0.3
  6. 000001 192806 0.4
  7. 000001 192807 0.3
  8. 000001 192809 0.4
  9. 000002 192801 0.3
  10. 000002 192802 0.8
  11. 000002 192804 0.4
  12. 000002 192805 0.3
  13. 000002 192806 0.4
  14. 000002 192807 0.3
  15. 000002 192808 0.4
复制代码

SAS代码:


  1. data a;
  2.     infile "D:\data.txt";
  3.     input no $ date x;
  4. run;

  5. data b;
  6.    
  7.     retain _no _date _x;
  8.     set a end=end;
  9.    
  10.     /* 保留当前观测 */
  11.     curr_no=no;  curr_date=date; curr_x=x;
  12.    
  13.     /* 用于date判断的初始值 */
  14.     if _N_=1 then _date=date;

  15.     /* 计算当前date与前一个date之间的月份数 */
  16.     date_dif=date-_date;
  17.     /* 特殊情况:每年之间相差的月份数、或最后一年差的月份数 */
  18.     if (no ^= _no & no ^= 1) | end then date_dif=12-input(substr(put(_date,6.),5,2),2.)+1;
  19.    
  20.     /* 当前date与前一个date相差的月份数大于2,就是有缺失的情况出现 */
  21.     if date_dif >=2  then
  22.         do;
  23.             do i=1 to (date_dif-1);
  24.                 no=_no; date=_date+i; x=.; is_missing='YES';
  25.                 if substr(put(date,6.),5,2)='01' then no=sum(no,1); output;
  26.             end;
  27.         end;

  28.     /* 输出正常的月份的观测 */
  29.     if not end then
  30.     do;
  31.         no=curr_no; date=curr_date; x=curr_x; is_missing='No '; output;
  32.     end;

  33.     /* 保留当前观测,用于下一次比较 */
  34.     _no=no; _date=date;  _x=x;

  35.     /* 删除所有临时变量 */
  36.     drop _no _date _x date_dif curr_no curr_date curr_x i;

  37. run;
复制代码

运行结果:

1.gif

求论坛币?


使用道具

20
scarfacetony 发表于 2013-1-22 19:09:30 |只看作者 |坛友微信交流群
有个很笨的方法,先把股票代码筛选出来,然后给每个代码加上月份,弄个全库。
然后进行一次匹配,就可以发现哪些没有匹配上了。
看了这个视频,我于是有了进股市的信心!
https://bbs.pinggu.org/thread-2787427-1-1.html

使用道具

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

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-28 06:11