DATA Step, Macro, Functions and more

calculate amount (compounded annually & compounded monthly)

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

calculate amount (compounded annually & compounded monthly)

[ Edited ]

Hi all SAS experts,

 

I am new to base SAS programming,

I am calculating amount(compounded annually & compounded monthly), the detailed question is below:

by using DO loops.

  

 

the code for 1 part :

 

 

 

 

data earn_3;

amount=500000;
int_rate=0.7;

do year=1 to 25;

earned+(amount+earned)*int_rate;

output;
end;
run;


for second part :


data earn_4;

amount=500000;
int_rate=0.7;

do month=1 to 25*12;

earned+(amount+earned)*int_rate/12;

output;
end;
run;


please suggest.

thanks

 


Accepted Solutions
Solution
‎02-26-2018 12:11 PM
Trusted Advisor
Posts: 1,309

Re: calculate amount (compounded annually & compounded monthly)

Posted in reply to sumitbe139

I presume you are trying to compare differences arising from varying frequency of compounding using the same annual interest rate.

 

If you want yearly values (as opposed to just the final value), I would recommend a loop for Y=1 to 25.  The annual "compounding" is trivial, because it is effectively a simple interest rate.  The monthly compounding (within each year) just needs the compound function, applying a rate of .07/12 and number of intervals=12.

 

data want;
  Y=0;
  amounta=50000;
  amountm=amounta;
rate=.07; output; do Y=1 to 25; amounta=(1+rate)*amounta; amountm=compound(amountm,.,rate/12,12); output; end; format amount: comma12.2; run;

 

Of course, in the real world each amount might be rounded to the nearest penny at the end of the year, so you would nest each calculation in the round function:

    amounta=round((1+rate)*amounta,.01);
    amountm=round(compound(amountm,.,rate/12,12),.01);

Finally, if rounding is required monthly, then there is no benefit to the COMPOUND function.  You have to make a monthly loop for each year and apply a simple monthly interest:

 

  do Y=1 to 25;
    amounta=round((1+rate)*amounta,.01);
    do m=1 to 12;
      amountm=round(amountm*(1+rate/12),.01);
    end;
    output;
  end;

View solution in original post


All Replies
PROC Star
Posts: 1,209

Re: calculate amount (compounded annually & compounded monthly)

Posted in reply to sumitbe139

Take a look at the Compound Function

Super User
Super User
Posts: 9,397

Re: calculate amount (compounded annually & compounded monthly)

Posted in reply to sumitbe139

There are many posts on the subject on here.  Please use the search function:

https://communities.sas.com/t5/forums/searchpage/tab/message?advanced=false&allow_punctuation=false&...

 

As for your code, why not do it in one step:

data earn_3;
  amount=500000;
  int_rate=0.7; 
  do year=1 to 25;
    earned=earned+(amount+earned)*int_rate;
    do month=1 to 12;
      earned_month=earned_month+(amount+earned)*(int_rate/12);
    end;
  end;
run;

Something like that.  Always put the assignement in, i.e. the variable before the =, also good idea to check the brackets as order of precedence might spoil your equation (i.e. calculations in brackets are resovled first, then in order of operator). 

Occasional Contributor
Posts: 9

Re: calculate amount (compounded annually & compounded monthly)

[ Edited ]

thanks for suggesting.

 

but its not  working as desired.

 

 

the code for the same is :

 

 

data earn_4;

amount=500000;
int_rate=0.07;


do year=1 to 25;
interest_y=amount*int_rate;
amount+interest_y;
do month=1 to 25*12;
interest_m=amount*(0.07/12);
amount+interest_m;
output;
end;
end;
run;

it giving numbers in exponential form.

please suggest. 

Super User
Super User
Posts: 9,397

Re: calculate amount (compounded annually & compounded monthly)

Posted in reply to sumitbe139

"but its not  working as desired.

 

please suggest. "

 

I would suggest posting some information about what is "not working".  Start by providing some test data in the form of a datastep, then show what the output you want is.  Show the code you are using.  There are a lot of posts under that link I have provided, have you read those and tried to implement the provided solutions?
"but I have to do it by using DO LOOPS only." - is this some sort of homework assignment?  If not use the tools provided.

Super User
Posts: 6,626

Re: calculate amount (compounded annually & compounded monthly)

[ Edited ]
Posted in reply to sumitbe139

One change you will certainly want to make ... an interest rate of 7% should be 0.07, not 0.7.

 

Other than that, why do you say that your programs are inadequate?  What needs to be fixed?

Occasional Contributor
Posts: 9

Re: calculate amount (compounded annually

Posted in reply to Astounding
yes please correct me on that.


data earn_4;

amount=500000;
int_rate=0.07;


do year=1 to 25;
interest_y=amount*int_rate;
amount+interest_y;
do month=1 to 25*12;
interest_m=amount*(0.07/12);
amount+interest_m;
output;
end;
end;
run;


Super User
Posts: 23,237

Re: calculate amount (compounded annually

Posted in reply to sumitbe139

1. Format your code (indents) - makes it easier to spot errors. You'll save the time back in quicker debugging. 

2. Comment your code - so we understand what you're expecting to happen in each line and you can understand it later when studying for your exams. 

 

Here's some starters that show what I mean. 

 

data earn_4;
    amount=500000; *starting amount;
    int_rate=0.07; *annual interest rate;

    do year=1 to 25;
        interest_y=amount*int_rate; * This isn't correct you'll always start from the same amount each loop;
        amount+interest_y;

        do month=1 to 25*12; *this loop is 25*12 years, too many loops;
            interest_m=amount*(0.07/12);
            amount+interest_m;
            output;
        end;
    end;
run;

 


sumitbe139 wrote:
yes please correct me on that.


data earn_4;

amount=500000;
int_rate=0.07;


do year=1 to 25;
interest_y=amount*int_rate;
amount+interest_y;
do month=1 to 25*12;
interest_m=amount*(0.07/12);
amount+interest_m;
output;
end;
end;
run;



 

Super User
Posts: 23,237

Re: calculate amount (compounded annually & compounded monthly)

Posted in reply to sumitbe139

I can see you’ve edited the question , but can’t see the original anymore. So I have no idea of what you’re looking for anymore. Is your question answered? If you still need help you’ll have to post way more details. Please don’t edit your question, rather post it as a new response. 

 


sumitbe139 wrote:

Hi all SAS experts,

 

I am new to base SAS programming,

I am calculating amount(compounded annually & compounded monthly), the detailed question is below:

by using DO loops.

  

 

the code for 1 part :

 

 

 

 

data earn_3;

amount=500000;
int_rate=0.7;

do year=1 to 25;

earned+(amount+earned)*int_rate;

output;
end;
run;


for second part :


data earn_4;

amount=500000;
int_rate=0.7;

do month=1 to 25*12;

earned+(amount+earned)*int_rate/12;

output;
end;
run;


please suggest.

thanks

 


 

Occasional Contributor
Posts: 9

Re: calculate amount (compounded annually & compounded monthly)

 

 
 
for the same I have done this code :
it will give the amount at monthly levels.
 
but, I need to figure it out at yearly levels also.
 
 
 
 
 
Super User
Posts: 23,237

Re: calculate amount (compounded annually & compounded monthly)

Posted in reply to sumitbe139

If you want the annual values, either use a different calculation with only 25 years that would calculate the interest for each year. The Theory of Interest text has the formula (as does google). 

 

Or you can just take the 12 value from your monthly table, use MOD() function to determine which records are the 12th month. 

 

Spoiler

sumitbe139 wrote:

 

 
 
for the same I have done this code :
it will give the amount at monthly levels.
 
but, I need to figure it out at yearly levels also.
 
 
 
 
 

Super User
Posts: 6,626

Re: calculate amount (compounded annually & compounded monthly)

Posted in reply to sumitbe139

Is there any reason you want to complicate the problem by computing year and monthly results in one step?  It can be done, but you need to be careful.  For example, looking at just the monthly program that you posted, you are not careful enough.  The interior loop now looks like this:

 

do month = 1 to 25*12;

 

Since this is already inside a loop that has year going from 1 to 25, the interior loop should simply be:

 

do month = 1 to 12;

 

Moreover, you should actually run the program and inspect the results, to see if they look reasonable to you.  You would have caught that change if you had actually checked the results.

Solution
‎02-26-2018 12:11 PM
Trusted Advisor
Posts: 1,309

Re: calculate amount (compounded annually & compounded monthly)

Posted in reply to sumitbe139

I presume you are trying to compare differences arising from varying frequency of compounding using the same annual interest rate.

 

If you want yearly values (as opposed to just the final value), I would recommend a loop for Y=1 to 25.  The annual "compounding" is trivial, because it is effectively a simple interest rate.  The monthly compounding (within each year) just needs the compound function, applying a rate of .07/12 and number of intervals=12.

 

data want;
  Y=0;
  amounta=50000;
  amountm=amounta;
rate=.07; output; do Y=1 to 25; amounta=(1+rate)*amounta; amountm=compound(amountm,.,rate/12,12); output; end; format amount: comma12.2; run;

 

Of course, in the real world each amount might be rounded to the nearest penny at the end of the year, so you would nest each calculation in the round function:

    amounta=round((1+rate)*amounta,.01);
    amountm=round(compound(amountm,.,rate/12,12),.01);

Finally, if rounding is required monthly, then there is no benefit to the COMPOUND function.  You have to make a monthly loop for each year and apply a simple monthly interest:

 

  do Y=1 to 25;
    amounta=round((1+rate)*amounta,.01);
    do m=1 to 12;
      amountm=round(amountm*(1+rate/12),.01);
    end;
    output;
  end;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 12 replies
  • 273 views
  • 4 likes
  • 6 in conversation