|
|
9#

楼主 |
发表于 2009-8-6 09:40:02
|
只看该作者
Re: 求助:怎么将一行数据和多行数据进行匹配,然后生成新变量?
[code:2utn7j7u]data store sellandgift;
infile datalines truncover;
input ID$ Date yymmdd8. Num Act Note ;
format Date yymmdd10.;
if act=1 and note=. then output store;
else output sellandgift;
datalines;
001 08-07-01 100 1 .
001 08-07-02 10 0 .
001 08-07-06 30 0 .
001 08-07-10 100 1 .
001 08-07-15 60 0 .
001 08-07-16 100 1 .
001 08-07-18 160 1 0
001 08-07-22 100 0 .
001 08-07-25 220 0 .
002 08-07-01 100 1 .
003 08-07-02 100 1 .
003 08-07-03 50 1 .
003 08-07-04 100 0 .
003 08-07-08 30 0 .
003 08-07-09 20 0 .
;
run;
%let offsetid=0;
%macro updateStore(num,note,id,selldate) ;
%let offsetid=%eval(&offsetid+1);
%if &note=. %then
%do;
data store(keep=id date num act note) avg(keep=id date num diff);
set store ;
by id date;
retain number &num diff 0;
if id=&id then
do;
offnum=min(num,number);
num=num-offnum;
number=number-offnum;
diff=diff+(&selldate-date)*offnum;
end;
if num>0 then output store;
if id=&id and last.id then do;diff=diff/#date=&selldate;output avg; end;
run;
proc append base=allavg
data=avg;
run;
%end;
%if &note=0 %then
%do;
proc sql;
create table store(drop=oldnum ) as
select *,oldnum+oldnum/sum(oldnum)*&num*(id="&id") as num
from store(rename=(num=oldnum))
group by id
order by id,date;
%end;
%mend;
data _null_;
set sellandgift;
length command $200;
command=compress('%updateStore('||num||','||note||','||id||','||date||')');
call execute(command);
run;[/code:2utn7j7u]
我把供应商送的货变成160了,不然都不够卖 |
|