Dear Sas Community:
I am trying to calculate a a weighted Average Interest Rate for a company that may have several subsidiaries that borrow at different interest rates.
I have the following table:
CompanyNum | SubsidiaryNum | Amount | Rate |
1 | 23 | 45000000 | 355 |
1 | 24 | 35000000 | 405 |
I want the following table:
CompanyNum | Total Amount | Weighted Average Rate |
1 | 80000000 | 376.875 |
How can I do that using SAS Proc SQL. Any suggestions. Basically I would like to have my analysis at the company level.
data watch;
input CompanyNum SubsidiaryNum Amount Rate;
datalines;
1 23 45000000 355
1 24 35000000 405
;
run;
proc sql;
select
CompanyNum,
sum(Amount) as Total_Amount,
sum(Amount*Rate)/sum(Amount) as wRate
from watch
group by CompanyNum;
quit;
Why not use the procedure that SAS intended for this purpose? It would be easy to expand the number of vars, or request other statistics (std, var, min, max, median, various percentiles etc.)
proc summary data=have noprint nway ;
class companynum;
var rate;
weight amount;
output out=want (drop=_type_ _freq_) sumwgt(rate)=total_amount mean(rate)=wrate ;
run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.