Above Data step method is not correct. Please see following code.
proc sort data=source;
by label;
run;
data main;
set source;
by label;
retain fenzi_sum 0 fenmu_sum 0;
if first.label then do;
fenzi_sum = price*multi;
fenmu_sum = price;
end;
if not first.label then do;
fenzi_sum= fenzi_sum + price*multi;
fenmu_sum = fenmu_sum + price;
end;
if last.label then do;
answer = fenzi_sum/fenmu_sum;
output;
end;
run;
proc sort data=main; by label ; run;
data final;
merge source main(keep=label answer);
by label;
run;
In summary, proc sql and data step are all fine with this problem. Proc sql looks even simpler.
|