【问题标题】:SAS: Rolling window regressions for daily values [closed]SAS:每日值的滚动窗口回归[关闭]
【发布时间】:2018-03-01 04:44:05
【问题描述】:

我需要每天滚动回归输出。数据集是股票收益和交易量,模型只有收益=交易量。 我需要过去 30 天的回归系数,每天再多一天,这样 30 天的窗口保持不变。

【问题讨论】:

  • 我认为最简单的方法就是按日期限制数据:data model; set begin(where=(date_var >= date()-30)); run; 然后通过模型运行这些数据。
  • 谢谢。问题是我在 5 年的数据中大约有 3 亿个观察结果......
  • 那么你要做的是回顾性地运行模型?
  • 没错,每天倒退 30 天
  • Communities.sas.com 有一些非常好的代码,可以使用数据步骤来处理它并使用临时数组。由于您只使用 30 天的窗口,因此这是一种相对简单快捷的方法。您只有一次数据传递,它可以扩展到其他长度/公式。我认为答案是通过论坛上的 mkeintz。

标签: sas regression sliding-window


【解决方案1】:

根据我了解到的问题,您希望每个月都为一个庞大的数据集回顾性地运行模型。这是我最近做的类似的事情。

/*Select the parts of the data from base data based on input.*/
%macro model_by_dates(id, date1, date2);
    data to_model;
        set begin(where=( &date1. < date_var < &date2.));
    run;

   /*YOUR model code here*/

    data result_file; 
        set From_model; 
        id=&id; /*To recognize from which model this is from.*/
    run;

    /*If you have huge files, do permanent data after every 10 loops or so. Depends.*/
    proc append base=all_Results data=result_file force; run;
%mend model_by_dates;

/*here we create the dates list. You can do this in multiple ways. Datdiff maybe?*/

%let maxmonths=3;

data dates;
    do i=0 to &maxmonths. by 1;
        begin=date()-(i*30);
        end = date()-( (i+1)*30);
        output;
    end;
run;


/*This is the master switch. It executes the macro for each row of the dates.*/
data _NULL_;
    set dates;
    call execute('%nrstr(%model_by_dates('
            ||strip(id)||','
            ||strip(begin)||','
            ||strip(end)||
    '))');
run;

编辑根据进一步的说明,我正在更改日期列表的创建:

data dates;
    do i=0 to 30 by 1;
        begin=date()-(i+30);
        end = date()- i ;
        output;
    end;
run;

【讨论】:

  • 很棒的框架!我会尽快尝试回归
  • @MaBo88 这个方法非常耗时非常快。先用几行代码运行。 (嗯,当然取决于。)
  • @MaBo88 成功了吗?
  • 是的!对不起,我忘了把它标记为成功!谢谢@pinegulf
猜你喜欢
  • 2020-08-25
  • 1970-01-01
  • 2017-09-16
  • 2017-11-29
  • 2013-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多