You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为每个交易日的TAQ数据创建独立SAS数据集并优化处理

按日拆分TAQ数据生成独立SAS数据集

我有一个月的每日交易TAQ数据,存储在EQY_US_ALL_TRADE_202107文件夹中,内含多个对应交易日的GZ压缩文件,命名格式如EQY_US_ALL_TRADE_202210701.gz、EQY_US_ALL_TRADE_202210702.gz等。

此前我将所有数据处理后存入同一数据集D:\MainDataset,该方式耗时且占用过多磁盘空间,因此希望改为每个交易日生成独立的SAS数据集。同时计划通过dif(datetime)计算交易时长,仅保留相关数据以缩小数据集体积,方便后续聚合分析。

以下是我目前的代码:

data "D:\MainDataset" (keep= filename time exchange symbol saleCondition tradeVolume tradePrice   tradeStopStock
            tradeCorrection sequenceNumber tradeId sourceOfTrade tradeReportingFacility 
            participantTimeStamp tradeReportingFacilityTimeStamp); 
     length folderef $8 time $15. exchange $1. symbol $17. saleCondition $4. tradeStopStock $1.
            sourceOfTrade $1. tradeReportingFacility $1. 
            participantTimeStamp $15. tradeReportingFacilityTimestamp $15.;

    rc=filename(folderef,"D:\EQY_US_ALL_TRADE_202107");
    did = dopen(folderef);
    putlog did=;
    /* do k = 1 to dnum(did);   Use this to run the loop over all files in the folder */
    do k = 1 to 3;   
    filename = dread(did,k);
    putlog filename=;
    if scan(filename,-1,'.') ne 'gz' then continue;
    fullname = pathname(folderef) || '\' || filename;
    putlog fullname=;

    do while(1);
    infile archive zip filevar=fullname gzip dlm='|' firstobs=2 obs=5000000 dsd truncover eof=nextfile;
    input time exchange symbol saleCondition tradeVolume tradePrice tradeStopStock
            tradeCorrection sequenceNumber tradeId sourceOfTrade tradeReportingFacility 
            participantTimeStamp tradeReportingFacilityTimeStamp;
    output;
    end;
    nextfile:
    end;
    stop;
    run;
    Proc contents data = "D:\MainDataset";
run;

proc print data ="D:\MainDataset" (obs = 110);
run;

解决方案:修改后的SAS代码

%let root_folder = D:\EQY_US_ALL_TRADE_202107;
%let output_path = D:\DailyTAQDatasets;

/* 自动创建输出目录(不存在则生成) */
%sysfunc(mkdir(&output_path));

data _null_;
  length folderef $8 filename $100 fullname $200 dsname $30;
  rc = filename(folderef, "&root_folder");
  did = dopen(folderef);
  
  /* 检查目录是否打开成功 */
  if did <= 0 then do;
    putlog "ERROR: 无法访问目录 &root_folder";
    stop;
  end;

  /* 遍历目录下所有GZ格式文件 */
  do k = 1 to dnum(did);
    filename = dread(did, k);
    /* 跳过非GZ文件 */
    if scan(filename, -1, '.') ne 'gz' then continue;
    
    fullname = pathname(folderef) || '\' || filename;
    putlog "正在处理文件: " fullname;

    /* 从文件名提取日期,生成对应数据集名 */
    dsname = substr(filename, length("EQY_US_ALL_TRADE_") + 1, 8);
    dsname = catt("&output_path.\TAQ_", dsname);

    /* 处理单个文件,生成每日独立数据集 */
    data &dsname. (keep= datetime exchange symbol saleCondition tradeVolume tradePrice 
                     tradeStopStock tradeCorrection sequenceNumber tradeId 
                     sourceOfTrade tradeReportingFacility trade_duration);
      length time $15 exchange $1 symbol $17 saleCondition $4 tradeStopStock $1
             sourceOfTrade $1 tradeReportingFacility $1;
      infile archive zip filevar=fullname gzip dlm='|' firstobs=2 dsd truncover;
      input time exchange symbol saleCondition tradeVolume tradePrice tradeStopStock
            tradeCorrection sequenceNumber tradeId sourceOfTrade tradeReportingFacility 
            participantTimeStamp tradeReportingFacilityTimestamp;
      
      /* 将字符型时间转换为SAS datetime格式 */
      datetime = input(time, anydtdtm20.);
      format datetime datetime20.;

      /* 计算与上一笔交易的时间差(单位:秒) */
      trade_duration = dif(datetime);
      /* 保留第一笔交易(dif值为0)或后续交易 */
      if _n_ = 1 or trade_duration > 0 then output;
    run;
  end;

  /* 关闭目录引用 */
  rc = dclose(did);
  rc = filename(folderef);
run;

核心改进说明

  • 按日拆分数据集:从GZ文件名中提取8位日期,生成如TAQ_202210701的独立数据集,统一存储到指定输出目录
  • 交易时长计算:先将字符型time转换为SAS原生datetime格式,再用dif()函数计算相邻交易的时间间隔(单位秒)
  • 精简数据体积:通过keep语句仅保留分析必需字段,移除冗余的时间戳字段,大幅减小数据集大小
  • 全量文件遍历:取消原代码中do k=1 to 3的测试限制,自动遍历目录下所有GZ文件
  • 容错处理:增加目录打开失败的错误提示,自动创建输出目录避免路径不存在的报错

内容的提问来源于stack exchange,提问作者zannatus saba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 11:35:31