2013-07-07 662 views
1

我想在SAS中执行以下操作。说我有以下数据来自拍卖书来:SAS:计算运行最大值和最小值(使用哈希对象)

Auction Book: Current

我要填充的最后四列(百思买,单位担保条件在百思买,百思卖,单位担保条件在最佳。卖)的SAS

​​

任何一个对如何计算在SAS的最后四列的任何建议吗?

正如您所看到的,这四列最后一列记录了买入和卖出的最佳可用价格以及这些价格在每个时期的可用单位数量。每次在“买入机会”或“卖出机会”中增加更好的买入或卖出价格时,必须更新最后两列。当以最佳可用价格购买或出售某些单位时也是如此。

+0

这并没有太大意义。您对“最佳可用价格”的定义是什么?我认为这仅仅是迄今为止最低买入和最高卖出的情况,但是你的数字会上下跳动。 –

+0

在“买入机会”一栏中,说直到时间= 8,这意味着有人愿意以不同的价格(80和82之间的差价)卖给你一个单位,因此可用的“最佳买入”是80. – Plug4

+0

我增加了两个专栏,以最好的购买和最畅销的方式跟踪可用的单位。这两列对我的工作至关重要。 – Plug4

回答

4

我能想到的三个选项是(1)转置和使用数据步骤数组,(2)使用双和号和宏数组(3)创建一个哈希对象。哈希可能是你最好的选择。 Here是一个很好的介绍哈希。

我想过使用保留,但我不认为这会起作用,因为当您购买或出售时,似乎需要重新计算最大销售价格和最低购买价格,不包括您购买的价格或在...处售卖。

编辑::以下是我如何使用哈希来使这项工作。首先它建立两个数据集,一个用于销售,另一个用于购买。然后在下一个datastep中将它们变成散列对象。它指的是什么时候买卖某物以更新需求量/供应量,并以此价格找到新的最佳价格和数量时的散列对象。

data hashset (keep=time2 buyprice2 ntobuy2 sellprice2 ntosell2); 
    set book2; 
    buyprice2=buyprice; ntobuy2=ntobuy; sellprice2=sellprice; ntosell2=ntosell; time2=time; 
    if (buyprice2 ne .) or (sellprice2 ne .) then output; 
run; 

data bestprices; 
    retain time buyprice ntobuy sellprice ntosell buy sell bestbuy nbestbuy tbestbuy bestsell nbestsell tbestsell; 
    set book2 end=setdone; 
    if _n_ = 1 then do; 
     *Set up hash hh, which contains the data in the data set hashset, with the key time2; 
     set hashset; 
     declare hash hh(dataset:'hashset', ordered:'a'); 
     hh.definekey('time2'); 
     hh.definedata(all:'yes'); 
     hh.definedone(); 
     declare hiter hiter('hh'); *Hash iterator allows iterating through the hash; 
    end; 

    *Buy section; 
    if (buyprice ne . and ((bestbuy=.) or (bestbuy>buyprice))) then do; 
     bestbuy=buyprice; nbestbuy=ntobuy; tbestbuy=time; 
    end; 
    bamper=index(buy, '@'); 
    if bamper>0 then do; 
     time2=tbestbuy; 
     num=substr(buy, 1, bamper-1)*1; 
     if hh.find()=0 then do; 
      ntobuy2=ntobuy2-num; 
      hh.replace(); 
      found=0; 
      rc=hiter.first(); 
      do while(rc=0); 
       if (ntobuy2>0) and (time2<time) and ((found=0) or (bestbuy>buyprice2)) then do; 
        found=1; 
        bestbuy=buyprice2; nbestbuy=ntobuy2; tbestbuy=time2; 
       end; 
       rc=hiter.next(); 
      end; 
     end; 
    end; 

    *Sell section; 
    if (sellprice ne . and ((bestsell=.) or (bestsell<sellprice))) then do; 
     bestsell=sellprice; nbestsell=ntosell; tbestsell=time; 
    end; 
    samper=index(sell, '@'); 
    if samper>0 then do; 
     time2=tbestsell; 
     num=substr(sell, 1, samper-1)*1; 
     if hh.find()=0 then do; 
      ntosell2=ntosell2-num; 
      hh.replace(); 
      found=0; 
      rc=hiter.first(); 
      do while(rc=0); 
       if (ntosell2>0) and (time2<time) and ((found=0) or (bestsell<sellprice2)) then do; 
        found=1; 
        bestsell=sellprice2; nbestsell=ntosell2; tbestsell=time2; 
       end; 
       rc=hiter.next(); 
      end; 
     end; 
    end; 
    keep time buyprice ntobuy sellprice ntosell buy sell bestbuy nbestbuy bestsell nbestsell; 
    if setdone then hh.output(dataset:'hh'); 
run; 

这是我用来设置初始数据集的代码。您不应该需要它,因为您已经拥有该数据集,但仅供参考:

data book; 
input buyprice sellprice buy $ sell $ ntobuy ntosell ; 
datalines; 
80  78  na  na  10 13 
80.5 79.5 na  na  12 15 
80.4 .  na  na  11 . 
81  .  na  na  13 . 
80.1 78.1 na  na  12 11 
80.2 77  na  na  11 12 
82  76  na  na  14 11 
.  .  [email protected] na  . . 
.  .  [email protected] na  . . 
.  78.5 na  na  . 12 
.  .  na  [email protected] . . 
.  79  na  na  . 14 
79.5 79.1 na  na  10 13 
.  .  na  [email protected] . . 
79.4 .  na  na  5 . 
run; 

data book; 
    retain time; 
    set book; 
    time = _n_; 
    if buy = 'na' then buy = ''; 
    if sell = 'na' then sell = ''; 
run; 
+0

感谢您的帮助!你需要重新计算最大销售价格和最低购买价格,并且不包括你购买或出售的价格。我将有机会在晚些时候或明天测试您的代码。再次感谢,请让我知道,一旦你有机会尝试它,它是否也能在你身边运作。 – Plug4

+1

我真的想downvote这个答案,但我不会因为建议是健全的。想要这样做的原因是因为你在复杂的嵌套逻辑中使用滞后函数。这只是要求麻烦。经常使用像这样的滞后函数会导致代码看起来正在工作,但实际上会返回不正确的结果。特别是随着数据变得更大/更复杂。我认为你提出的最好的选择是散列。当我有一段时间的时候,我会尝试使用它的解决方案。 –

+0

看起来他们改变了StackOverflow UI。因为8分钟前我做了这个,所以无法撤销我的失望。对不起=/ –