1.Write a SAS program to solve the given problem using Do
Loop:
A new account was opened in the SBI with initial deposit of
Rs. 10,000/. SBI offers an interest rate of 4%. Find the balance
after 5 years. Also store the every year and the balance.
YEAR PRINCIPAL INTEREST AMOUNT
1 10,000 (P * R)/100 A = P + INTEREST
2 P (P * R)/100 A = A1 + INTEREST
2. In the previous program what if the interest was paid
quarterly.
3. Calculate the amount for the following :
Amount deposited = Rs. 5000/month.
Rate of interest = 6 %
Duration = 3 Years.
Interest added yearly.
Sorry, what's your question?
Read the log, and you'll find the error. It's THAT obvious, really.
BTW your retain statement makes no sense. As your datastep only undergoes one iteration, it has no effect.
RETIAN is used to save data in memory uninitialized at reading input.
As you don't have input file nor sas dataset, you don't need use the retain.
Your example is:
YEAR PRINCIPAL INTEREST AMOUNT 1 10,000 (P * R)/100 A = P + INTEREST 2 P (P * R)/100 A = A1 + INTEREST
in 2nd year you add interest to A1, no to A - so it should be comulative.
So try next code:
data new;
priciple=10000;
Amount = priciple; /* starting amount */
do i= 1 to 5;
interest=priciple*4/100; /* parenthesis not needed */
Amount=priciple+interest;
output;
end;
run;
Try this code:
data new;
format priciple interest amount 20.2;
priciple = 10000;
do i = 1 to 5;
interest = priciple * 4 / 100;
amount = priciple + interest;
priciple = amount + interest;
output;
end;
run;
proc print data=new noobs;
run;
Note how proper indentation and the use of spaces makes the code more readable
Note the semicolon before the output statement (was missing in your code and causing an error)
Note the omitted retain statement
Note the use of a numeric format to make the results more readable
The log:
16 data new; 17 format priciple interest amount 20.2; 18 priciple = 10000; 19 do i = 1 to 5; 20 interest = (priciple * 4) / 100; 21 amount = priciple + interest; 22 priciple = amount + interest; 23 output; 24 end; 25 run; NOTE: The data set WORK.NEW has 5 observations and 4 variables. NOTE: DATA statement used (Total process time): real time 0.17 seconds cpu time 0.00 seconds 26 27 proc print data=new noobs; 28 run; NOTE: There were 5 observations read from the data set WORK.NEW. NOTE: The PROCEDURE PRINT printed page 1. NOTE: PROCEDURE PRINT used (Total process time): real time 0.10 seconds cpu time 0.01 seconds
Clean and no extra NOTEs
The result:
priciple interest amount i 10800.00 400.00 10400.00 1 11664.00 432.00 11232.00 2 12597.12 466.56 12130.56 3 13604.89 503.88 13101.00 4 14693.28 544.20 14149.09 5
You might consider dropping i from the dataset.
@Kurt_Bremser, are you not adding the interest twice:
amount = priciple + interest;
priciple = amount + interest;
should it not be:
amount = priciple + interest;
priciple = amount;
@Shmuel wrote:
@Kurt_Bremser, are you not adding the interest twice:
amount = priciple + interest; priciple = amount + interest;
should it not be:
amount = priciple + interest; priciple = amount;
I have to admit that I was not giving attention to the logic, just making the code syntactically correct and easy to read, as that was what caught my attention in the first place.
May summarize up to now:
1) I agree with @Kurt_Bremser that identation is important in order that program will be more readable.
2) According to example, one could think that interest is fixed, the same each year,
than the code should be:
data new;
priciple=10000;
Amount = priciple; /* starting amount */
do i= 1 to 5;
interest=priciple*4/100; /* parenthesis not needed */
Amount=priciple+interest;
output;
end;
run;
In case, interset is to be calculated on comulative pricipe and yearly interests, than the code is:
data new;
priciple=10000;
Amount = priciple; /* starting amount */
do i= 1 to 5;
interest=priciple*4/100; /* parenthesis not needed */
Amount=priciple+interest;
output;
pricipe = amount;
end;
format pricipe amount interest comma8.2;
run;
My preferred method for calculating amounts + interest is to use a simple multiplication, as that can be used to calculate a deliberately set number of years in one step:
amount = start * (1 + rate / 100) ** years;
Interest for a given year would then be
interest = (start * (1 + rate / 100) ** (years - 1)) * rate / 100;
The code for the original problem would look like
data new;
format priciple amount interest 20.2;
priciple = 10000;
rate = 4;
do years = 1 to 5;
amount = priciple * (1 + rate / 100) ** years;
interest = (priciple * (1 + rate / 100) ** (years - 1)) * rate / 100;
output;
end;
run;
The nice thing about this is that one does not have to run through all years starting with 1. One could run the calculations for years 20 to 25 only, and get the correct values.
This looks like a SAS class quiz/test problem 😉
@nehalsanghvi wrote:
This looks like a SAS class quiz/test problem 😉
A literal copy and paste of the assignment 🙂
@Yavuz please mark one of the solutions provided as the correct answer
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.