BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sumitbe139
Fluorite | Level 6

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
mkeintz
PROC Star

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

--------------------------

View solution in original post

12 REPLIES 12
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

sumitbe139
Fluorite | Level 6

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. 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

Astounding
PROC Star

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?

sumitbe139
Fluorite | Level 6
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;


Reeza
Super User

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;



 

Reeza
Super User

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

 


 

sumitbe139
Fluorite | Level 6

 

 
 
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.
 
 
 
 
 
Reeza
Super User

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.
 
 
 
 
 

Astounding
PROC Star

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.

mkeintz
PROC Star

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

--------------------------

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 12 replies
  • 4749 views
  • 4 likes
  • 6 in conversation