New Contributor
Posts: 2

# DO LOOP question

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.

Super User
Posts: 23,247

## Re: DO LOOP question

Sorry, what's your question?

New Contributor
Posts: 2

## Re: DO LOOP question

hi I couldn't able to get the logic.
data new;
priciple=10000;
do i= 1 to 5;
retain priciple 0;
interest=(priciple*4)/100;
Amount=priciple+interest;
priciple=amount+interest
output;
end;
run;
Super User
Posts: 9,880

## Re: DO LOOP 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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Posts: 1,831

## Re: DO LOOP question

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.

```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;``````

Super User
Posts: 9,880

## Re: DO LOOP question

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Posts: 1,831

## Re: DO LOOP question

Posted in reply to KurtBremser

@KurtBremser, are you not adding the interest twice:

``````  amount = priciple + interest;
priciple = amount + interest;``````

should it not be:

``````  amount = priciple + interest;
priciple = amount;``````
Super User
Posts: 9,880

## Re: DO LOOP question

Shmuel wrote:

@KurtBremser, 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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Posts: 1,831

## Re: DO LOOP question

Posted in reply to KurtBremser

May summarize up to now:

1) I agree with @KurtBremser 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;``````
Super User
Posts: 9,880

## Re: DO LOOP question

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 54

## Re: DO LOOP question

excellent solutions, thanks for all
Frequent Contributor
Posts: 75

## Re: DO LOOP question

This looks like a SAS class quiz/test problem

Super User
Posts: 23,247

## Re: DO LOOP question

Posted in reply to nehalsanghvi

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

Contributor
Posts: 54

## Re: DO LOOP question

Ok Reeza thank you for informing me.
Discussion stats
• 13 replies
• 346 views
• 7 likes
• 6 in conversation