Oh My God!
It is the most complicated logic I have ever meeted.
After works for half a day.Only can get partly outcome. Hope this will help you a bit.
I will promote this code after. Actually I am very tired ,need some rest.
[pre]
data i;
input tdate : DDMMYY10. fund : $10. ISIN : $20. Price : commaX8. transmod $ numberstocks;
format tdate DDMMYY10. price commaX8.2;
datalines;
19/02/2004 100174701 BE0003565737 47 buy 3000
19/03/2004 100174701 BE0003565737 44,8 buy 3800
24/03/2004 100174701 BE0003565737 44,59 buy 12050
23/06/2005 100174701 BE0003565737 66,65 buy 3900
11/11/2005 100174701 BE0003565737 73,15 buy 3000
14/11/2005 100174701 BE0003565737 73,2 buy 3800
17/05/2006 100174701 BE0003565737 84,3 buy 250
12/06/2006 100174701 BE0003565737 78,05 buy 800
11/08/2003 100174701 BE0003565737 87,85 sell 40000
11/08/2006 100174701 BE0003565737 87,85 sell 3800
21/09/2006 100174701 BE0003565737 84,75 buy 530
23/11/2006 100174701 BE0003565737 85,3 buy 30
4/12/2006 100174701 BE0003565737 85,95 buy 20
23/01/2007 100174701 BE0003565737 97,65 buy 260
13/02/2007 100174701 BE0003565737 97,8 sell 400
23/02/2007 100174701 BE0003565737 96,2 buy 150
6/03/2007 100174701 BE0003565737 89,67 sell 300
6/03/2007 100174701 BE0003565737 89,67 sell 40000
;
run;
proc sort data=i ;
by tdate ;
run;
data i;
set i;
if _n_=1 then delete;
run;
data buy sell(rename=(tdate=_tdate fund=_fund isin=_isin price=_price transmod=_transmod numberstocks=_numberstocks));
set i;
if transmod eq 'buy' then output buy;
else if transmod eq 'sell' then output sell;
run;
data want;
b=1;s=1; sum=0;_sum=0;
set buy nobs=b_obs;set sell nobs=s_obs;
do i=1 to 100000; *10000 is whatever you want ,just to be enough bigger than number of obs.;
if numberstocks lt _numberstocks then do;
do until(_numberstocks lt sum);
set buy point=b ; b+1;
output;
sum=sum(sum,numberstocks);
end; s+1;
_numberstocks=_numberstocks - ( sum - numberstocks ); sum=0;
put _numberstocks= numberstocks= sum= b= s= b_obs= s_obs=;
if b ge b_obs then stop;
*if s ge s_obs then stop;
end;
else if numberstocks gt _numberstocks then do;
do until( numberstocks lt _sum );
set sell point=s ; s+1;
output;
_sum=sum(_sum,_numberstocks);
end; b+1;
numberstocks=numberstocks - ( _sum - _numberstocks ); _sum=0;
put _numberstocks= numberstocks= _sum= b= s= b_obs= s_obs=;
if s ge s_obs then stop;
*if b ge b_obs then stop;
end;
end;
run;
data op;
set want;
range=_numberstocks - numberstocks;
cul_sum+range;
dif=range-cul_sum;
run;
[/pre]
Ksharp
... View more