BookmarkSubscribeRSS Feed
jonty
Fluorite | Level 6

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;

 

3 REPLIES 3
Astounding
PROC Star

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Kurt_Bremser
Super User

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 891 views
  • 0 likes
  • 4 in conversation