|
|
Re: 再问下“死猪头” :D
呵呵,很久没有写东西了,写出来的都像垃圾,没有检查,没有优化。
[code:2c4ajr1l]
data a;
input ID$ Date yymmdd8. Num Act Note;
format Date yymmdd10.;
cards;
001 08-07-01 100 1 .
001 08-07-02 20 0 .
001 08-07-05 100 1 .
001 08-07-06 80 0 .
001 08-07-07 40 0 .
001 08-07-08 40 0 .
001 08-07-09 100 1 .
001 08-07-10 120 1 0
001 08-07-12 240 0 .
002 08-07-01 100 1 .
002 08-07-02 20 0 .
002 08-07-05 80 0 .
002 08-07-06 100 1 .
002 08-07-08 50 0.
;
run;
data c(keep=date sell InStckDt lsting );
array stock{0:9} _temporary_ (10*0); /*stock Amount*/
array dt{0:9} _temporary_ (10*0); /*Stocking date*/
retain StckNo 0; /*Stock number*/
set a(where=(ID="001"));
if act=1 then do;
if note=0 then do;
TotStck=0;
i=StckNo;
do while(i>=0);
TotStck=TotStck+stock{i};
i=i-1;
end;
i=StckNo;
do while(i>=0);
stock{i}=stock{i}+Num*stock{i}/TotStck;
i=i-1;
end;
end;
else do;
StckNo+1;
i=StckNo;
do while(i>0);
stock{i}=stock{i-1};
dt{i}=dt{i-1};
i=i-1;
end;
stock{0}=num;
dt{0}=date;
end;
end;
if act=0 then do;
t=num;
i=StckNo-1;
do while(i>=0);
u=stock{i};
stock{i}=max(stock{i}-t,0);
t=t-u;
put i=;
if stock{i}=0 then StckNo=StckNo-1;
if stock{i}<u then do;
lsting=datdif(dt{i},date,'act/act');
sell=u-stock{i};
InStckDt=dt{i};
output c;
end;
if stock{i}=0 then dt{i}=0;
if t=0 then return;
i=i-1;
end;
end;
format InStckDt yymmdd10.;
run;
[/code:2c4ajr1l] |
|