我正在尝试在SAS中执行以下操作。假设我从拍卖书中得到以下数据:
我希望在SAS中填写最后四列(百思买,单位可用,百思买,畅销和单位可用)。
任何人都有关于如何计算SAS中最后四列的任何建议?
正如您所看到的,这四个最后一列记录了买卖的最佳可用价格以及每个时期这些价格的可用单位数量。每当“购买机会”或“销售机会”中添加更好的买入或卖出价格时,必须更新最后两列。如果以最优惠的价格购买或出售部分单位,也是如此。
答案 0 :(得分: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
. . 9@80 na . .
. . 1@80 na . .
. 78.5 na na . 12
. . na 4@79.5 . .
. 79 na na . 14
79.5 79.1 na na 10 13
. . na 11@79.5 . .
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;