I have following data :
data _tmp_;
length Customer $6. Type $4.;
input
Customer $ Type $ Leakage_amount year;
datalines;
ABC123 Mid 1500 2019
ABC123 Mid 7000 2020
ABC123 SB&A 2000 2020
;run;
And I want my output as, summing Leakage_Amount and picking Type with highest Leakage_Amount -
ABC123 Mid 1500 2019
ABC123 Mid 9000 2020
I can get this by
proc sql;
select a.*,b.Type from
(select Customer, year, sum(Leakage_amount) as Leakage_amount from _tmp_ group by 1,2) as a
left join
(select Customer, year, Type from _tmp_ group by 1,2 having Leakage_amount = max(Leakage_amount)) as b
on A.Customer=B.Customer and A.Year=B.Year
;quit;
But this seems like overkill, there must be an easier and neater way to do this. Please help.
data _tmp_;
length Customer $6. Type $4.;
input
Customer $ Type $ Leakage_amount year;
datalines;
ABC123 Mid 1500 2019
ABC123 Mid 7000 2020
ABC123 SB&A 2000 2020
;run;
proc sql;
create table want as
select *, sum(Leakage_amount) as sum_Leakage_amount
from _tmp_
group by customer,year
having Leakage_amount=max(Leakage_amount);
quit;
A good use case to take advantage of auto-remerge by the SQL processor.
You can use PROC MEANS or PROC SUMMARY with the SUM output statistic and the MAXID output statistic.
data _tmp_;
length Customer $6. Type $4.;
input
Customer $ Type $ Leakage_amount year;
datalines;
ABC123 Mid 1500 2019
ABC123 Mid 7000 2020
ABC123 SB&A 2000 2020
;run;
proc sql;
create table want as
select *, sum(Leakage_amount) as sum_Leakage_amount
from _tmp_
group by customer,year
having Leakage_amount=max(Leakage_amount);
quit;
A good use case to take advantage of auto-remerge by the SQL processor.
Hi,
Maybe double DoW-loop:
data have;
length Customer $6. Type $4.;
input
Customer $ Type $ Leakage_amount year;
datalines4;
ABC123 Mid 1500 2019
ABC123 Mid 7000 2020
ABC123 SB&A 2000 2020
;;;;
run;
data want;
_max=.;
_out=0;
_tot=0;
do _N_ = 1 by 1 until(last.year);
set have;
by year;
if Leakage_amount > _max then
do;
_max = Leakage_amount;
_out = _N_;
end;
_tot + Leakage_amount;
end;
do _N_ = 1 to _N_;
set have;
if _out = _N_ then
do;
Leakage_amount = _tot;
output;
end;
end;
drop _:;
run;
proc print;
run;
Bart
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.