내가 생각할 수있는 3 가지 옵션은 (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;
이것은별로 의미가 없습니다. "최고의 가용 가격"에 대한 귀하의 정의는 무엇입니까? 나는 그것이 지금까지 가장 낮은 구매와 가장 높은 판매의 경우라고 생각했지만 귀하의 숫자는 위아래로 뛰어 올랐습니다. –
"구입할 수있는 기회"열에서 시간 = 8까지 말하면 차이 가격 (80에서 82까지 다양 함)로 판매 할 의향이있는 사람들이 있다는 뜻이므로 "가장 좋은 구매"는 80입니다. – Plug4
베스트 바이와 베스트 셀러에서 사용 가능한 유닛을 추적하는 두 개의 컬럼을 추가했습니다. 이 두 컬럼은 제 작업에서 매우 중요합니다. – Plug4