i want compound interest for eaach month till 25 years but something is wrong with my code kindly help;-
data Compound_intrest2;
rate = 0.07;
do years = 1 to 25;
amount = 500000;
intrest + (intrest+amount)*rate;
totalamount + (amount + intrest);
output;
end;
run;
Well, you do have several issues with the code. Here are the biggest ones. There is no mention of MONTH anywhere. AMOUNT gets added to the total amount every year. Here's one possibility:
data compounded;
rate = 0.07;
current_amount = 500000;
do years = 1 to 25;
do months = 1 to 12;
interest = current_amount * (rate / 12);
current_amount = current_amount + interest;
output;
end;
end;
run;
The OUTPUT statement is optional, depending on whether you want to see a separate set of numbers for each month or not.
Your code seems to be syntactily correct, not sure how I can guess what is "wrong". Follow the guidance found by Post button on new posts - post example test data as a datastep (so we can see some data and structure), post example required output, and explain any logic between the two. The compound part seems to indicate you want to retain the value, but can't tell from that.
I like to use a formula with exponentiation instead of adding up:
data compound_intrest2;
rate = 0.07;
amount = 500000;
year = 1;
month = 1;
endyear = 25;
endmonth = 12;
total_months = 0;
do until (year >= endyear and month >= endmonth);
total_months + 1;
tot_amount = amount * (rate/12 + 1) ** total_months; /* I guess that rate is a yearly value */
curr_interest = tot_amount - amount * (rate/12 + 1) ** (total_months - 1);
tot_interest = tot_amount - amount;
output;
month + 1;
if month > 12
then do;
year + 1;
month = 1;
end;
end;
drop endyear endmonth total_months; /* not needed any longer */
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.