crackman 发表于 2010-5-1 12:46 
不好意思
你还一个方法
做出来了我给你100论坛币
这个是真的么?版主你可要说话算数哈
下面改进了一点,解决了sum() 中的变量太多不方便一一手动输入的问题
N_total=sum(N_bonds,N_others,N_equity);
-------------------------------------------------------------------------------
data a;
input customer fund$ balances@;
cards;
1 equity 100
1 equity 200
1 bonds 120
2 bonds 300
2 others 210
3 bonds 100
;
run;
proc sql noprint;
select count(distinct ( fund)) into: count from a;
select distinct fund into: fund from a;
create table tt as select distinct (fund) from a;quit;
%macro s;
data _null_;
set tt;
call symput ('n',_n_);
call symput (compress('f'||_n_),fund);
run;
%mend;
%s;
%macro k;
%DO i = 1 %TO &count;
data j&i;
set a;if fund="&&f&i"
then n_&&f&i=1 ;
%end;
run;
data w;
merge %do i=1 %to &count;
j&i %end;;
run;
%do i=1 %to &count;
proc sql;create table tmp&i as
select customer ,sum(balances) as total_balances,
sum(n_&&f&i) as n_&&f&i from w group customer;quit; %end;
data w2;
merge %do i=1 %to &count;tmp&i %end;;
run;
data ww;set w2;run;
data _null_;
if 0 then set w2;
array _allvar_(*) _all_;
call execute('proc datasets; modify w2;rename ') ;
do i=3 to dim(_allvar_);
rename=catt(vname(_allvar_(i)),'=col',i);
call execute(rename) ;
end;
call execute(';run;quit;') ;
stop;
run;
%mend;
%k;
%let t=%eval(&count+2);
%put &t;
data w3;
set w2;
n_total=sum(of col3-col&t.);
keep customer n_total;
run;
data final;
merge ww w3;
run;