4

我有超过 3400 个 CSV 文件,大小在 10kb 到 3mb 之间。每个 CSV 文件都有这个通用文件名:AAPL、GOOG、YHOO 等股票代码stockticker-Ret.csv在哪里,并且在给定日期的每一分钟都有股票回报。我的 SAS 代码首先从SAS 数据集中的文件中stockticker加载所有股票代码名称。stockticker-Ret.csv我遍历每个股票代码以在.csv名为. 可以想象,这个过程需要很长时间。有没有办法改进下面的代码以使这个过程更快? wantwantwantglobalDO LOOP

/*Record in a sas dataset all the csv file name to extract the stock ticker*/
    data yfiles;
    keep filename;
    length fref $8 filename $80; 
    rc = filename(fref, 'F:\data\'); 
    if rc = 0 then do; did = dopen(fref); 
    rc = filename(fref); end; else do; length msg $200.; msg = sysmsg(); put msg=; did = .; end;
    if did <= 0 then putlog 'ERR' 'OR: Unable to open directory.';
    dnum = dnum(did);
    do i = 1 to dnum; filename = dread(did, i); /* If this entry is a file, then output. */ fid = mopen(did, filename); if fid > 0 then output; end;
    rc = dclose(did);
    run;

/*store in yfiles all the stock tickers*/
    data yfiles(drop=filename1 rename=(filename1=stock));
    set yfiles;
    filename1=tranwrd(filename,'-Ret.csv','');
    run;

    proc sql noprint;
    select stock into :name separated by '*' from work.yfiles;
    %let count2 = &sqlobs;
    quit;


    *Create the template of the desired GLOBAL SAS dataset;
    proc sql;
    create table global
    (stock char(8), time_gap num(5), avg_ret num(5));
    quit;

    proc sql;
    insert into global
    (stock, time_gap,avg_ret)
    values('',0,0);
    quit;

    %macro y1;
    %do i = 1 %to &count2;
    %let j = %scan(&name,&i,*);
    proc import out = want datafile="F:\data\&j-Ret.csv"
    dbms=csv replace;
    getnames = yes;
    run;


    data want;
    set want; ....

    ....[Here I do 5 Datasteps on the WANT sasfile] 


/*Store the want file in a global SAS dataset that will contain all the stock tickers from the want file*/

    data global;
    set global want; run;

    %end;
    %mend y1;
    %y1()

如您所见,全局 SAS 数据集针对want我存储的每个数据集展开global

4

1 回答 1

4

假设文件有一个共同的布局,你不应该用循环导入它们PROC IMPORT或做循环。您应该使用一个数据步将它们全部阅读。IE:

data want;
length the_file $500;
infile "f:\data\*.csv" dlm=',' lrecl=32767 dsd truncover firstobs=2 filename=the_file;
input
myvar1 myvar2 myvar3 myvar4;
stock_ticker=scan(the_file,'\',-1); *or whatever gets you the ticker name;
run;

现在,如果它们没有相同的布局,或者 readin 有一些复杂性,您可能需要比这更复杂的输入语句,但几乎总是可以通过这种方式实现。由于 IMPORT 的开销,具有大量 PROC IMPORT 的循环总是效率低下。

如果您不希望文件夹中的每个 .csv 文件(并且无法为您想要的内容编写掩码),或者如果您有一部分布局,则可以使用 FILEVAR 选项从通用数据集。然后,如果需要,您可以分支到各种输入语句。

data want;
set yfiles;
infile a filevar=filename;
if filevar [some rule] then do;
input ... ;
end
;else if ... then do;
input ... ;
end;
run;
于 2014-01-22T19:04:37.040 回答