Fluorite | Level 6

calculate amount (compounded annually & compounded monthly)

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 `

1 ACCEPTED SOLUTION

Accepted Solutions
PROC Star

Re: calculate amount (compounded annually & compounded monthly)

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;``````
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
12 REPLIES 12
Tourmaline | Level 20

Re: calculate amount (compounded annually & compounded monthly)

Take a look at the Compound Function

Diamond | Level 26

Re: calculate amount (compounded annually & compounded monthly)

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

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).

Fluorite | Level 6

Re: calculate amount (compounded annually & compounded monthly)

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.

Diamond | Level 26

Re: calculate amount (compounded annually & compounded monthly)

"but its not  working as desired.

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.

PROC Star

Re: calculate amount (compounded annually & compounded monthly)

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?

Fluorite | Level 6

Re: calculate amount (compounded annually

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

Re: calculate amount (compounded annually

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

Re: calculate amount (compounded annually & compounded monthly)

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 `

Fluorite | Level 6

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

Re: calculate amount (compounded annually & compounded monthly)

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.

PROC Star

Re: calculate amount (compounded annually & compounded monthly)

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.

PROC Star

Re: calculate amount (compounded annually & compounded monthly)

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;``````
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Discussion stats
• 12 replies
• 5316 views
• 4 likes
• 6 in conversation