现在的位置: 首页 > 综合 > 正文

股票数据合并

2018年10月21日 ⁄ 综合 ⁄ 共 1726字 ⁄ 字号 评论关闭

一个月在论坛上提问过个股数据合并问题,问题如下:

现有100只股票数据txt,命名形如:daysh600000,daysh600004...,daysz000001....,600000等是股票代码(id);数据的结构都一样,内含变量,date,open,high,close等,但不含股票代码:
data daysh600000;
    input date open hight low clsoe;
    cards;
2011 29.5 29.8 27 27.75
;
run;

现在想请教,如何合并这些数据,新数据中包含股票代码?新的数据格式:
id       date    open  high  low close
600000     2011   29.5  29.8  27   27.75
600000      ....     ....    ...    ...     .... 
600004     2011   7.72  8.56  7.69 8.25
    ...

感谢yamat/webgu/蓝莓夹心等网友的解答,解决方法如下:

 
1.个人数据为sas格式的数据

libname yu "E:\yugao\datasets\datasets\day\hsa";

proc sql noprint;
   select memname into:dsnlist separated by " "
      from dictionary.tables
      where libname="YU";
quit;
data tar;
   length id $ 32;
   set yu.daysh600000;
   stop;
run;
%macro inserttable(dsnlist);
%let n=%eval(%sysfunc(count(&dsnlist,%str( )))+1);
%do i=1 %to &n;
   %let dsn=%scan(&dsnlist,&i);
   proc sql;
      insert into tar
         select  "&dsn",
                   Time,
                   Open,
                   High,
                   Low,
                   Close,
                   Volume,
                   Amount
          from yu.&dsn;
   quit;
%end;
%mend;
%inserttable(dsnlist=&dsnlist);

*另一种思路;

/*macro for appending data set*/
options  symbolgen mprint mlogic;
%macro app(inlib);
proc sql;
  select  distinct memname  into : dslist separated by " "
  from sashelp.vcolumn
  where libname=%upcase("&inlib");
quit;

  %let i=1;
  %do %while(%scan(&dslist,&i) ne );
     %let ds=%scan(&dslist,&i);
       data &ds;
           id="&ds";
                set  &ds;
       run;
 proc append base=all  data=&ds  force;
 run;

  %let i=%eval(&i+1);
  %put  %scan(&dslist,&i);
  %end;
%mend app;

/*test: cation,do it only once*/
%app(YU);

 

2.文件为txt等格式文件,利用infile命令中的filename参数

data all;
   length fn $100.;
   length filename $100.;
   infile 'e:\tmp\*' delimiter=   filename=fn;
   input date $ open high low close volume amount;
   id=scan(fn,2,'\');
run;

 

 

 

抱歉!评论已关闭.