Hello Sandeep,
The code here below should do the job:
data results (drop= SUM_:);
set input;
retain SUM_COST SUM_QTY;
if _N_ = 1 then do;
SUM_COST = 0;
SUM_QTY = 0;
end;
if strip(upcase(Flag)) = 'BUY' then do;
SUM_COST = SUM(SUM_COST, QTY * PRICE);
SUM_QTY = SUM(SUM_QTY, QTY);
AVG_PRICE = .;
end; else
if strip(upcase(Flag)) = 'SEL' then do;
AVG_PRICE = SUM_COST / SUM_QTY;
end;
run;
Considering that the content of your "input" dataset has the following colomns: FLAG, QTY, PRICE.
Please let me know should there be something missing in my answer.
Regards,
Florent
... View more