Contributor
Posts: 26

# problem in creating compound intrest for months

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;

Super User
Posts: 5,497

## Re: problem in creating compound intrest for months

[ Edited ]

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.

Super User
Posts: 7,942

## Re: problem in creating compound intrest for months

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.

Super User
Posts: 7,760

## Re: problem in creating compound intrest for months

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;``````
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Discussion stats
• 3 replies
• 105 views
• 0 likes
• 4 in conversation