2012-06-16 72 views
0

这有点复杂(至少对我来说)。SAS创建动态时间间隔

这是我必须做的: 说我有以下数据集:

date price volume 
02-Sep 40 100 
03-Sep 45 200 
04-Sep 46 150 
05-Sep 43 300 

说,我有一个断点,我希望在我的数据集创建的间隔。例如,让我的断点= 200卷交易。

我想要创建一个ID列并记录一个ID变量= 1,2,3,...对于每个断点= 200.当您将每个ID的所有音量相加时,该值必须在所有音量上保持不变ID变量。

所以用我上面的例子中,我最终的数据集应该看起来像下面这样:

date price volume id 
02-Sep 40 100 1 
03-Sep 45 100 1 
03-Sep 45 100 2 
04-Sep 46 100 2 
04-Sep 46 50 3 
05-Sep 43 150 3 
05-Sep 43 150 4 

(最后一排会错过一些价值,但是这很好,我会踢了最后一个ID。)

正如你所看到的,我必须“分解”一些行(比如第二行,例如,我把200分成两个100卷),以便在所有ID中具有总值200的恒定值。

回答

2

看起来像是在进行流量毒性VPIN计算的容量分流。我想这样的作品:

%let bucketsize = 200; 

data buckets(drop=bucket volume rename=(vol=volume)); 
    set tmp; 
    retain bucket &bucketsize id 1; 

    do until(volume=0); 
     vol=min(volume,bucket); 
     output; 
     volume=volume-vol; 
     bucket=bucket-vol; 
     if bucket=0 then do; 
      bucket=&bucketsize; 
      id=id+1; 
     end; 
    end; 
run; 

我与你的数据集测试此,它看起来正确的,但我会仔细检查几种情况,以确认其作品的权利。

+0

哇!不错的猜测!你也做过VPIN吗?谢谢您的帮助! – Plug4

+0

另外我忘了补充一点,我知道量交易是买入还是卖出,因此我不能在这个过程中失去这些信息。谢谢! – Plug4

+0

我一直在尝试一些事情,但没有成功... – Plug4

1

如果您有一个表示'买'或'卖'的变量,那么您可以试试这个。假设这个变量被称为类型并且取值为'B'或'S'。使用这种方法的一个优点是,如果有'分组',处理更容易。

%let bucketsize = 200; 

data tmp2; 
    set tmp; 
    retain volsumb idb volusums ids; 

    /* Initialize. */ 
    volusumb = 0; idb = 1; volsums = 0; ids = 1; 

    /* Store the current total for each type. */ 
    if type = 'B' then volsumb = volsumb + volume; 
    else if type = 'S' then volsums = volsums + volume; 

    /* If the total has reached 200, then reset and increment id. */ 
    /* You have not given the algorithm if the volume exceeds 200, for example the first two values are 150 and 75. */ 
    if volsumb = &bucketsize then do; idb = idb + 1; volsumb = 0; end; 
    if volsums = &bucketsize then do; ids = ids + 1; volsums = 0; end; 

    drop volsumb volsums; 
run;