turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- DO LOOP question

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-24-2017 12:44 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to tappu440

02-24-2017 12:57 AM

Sorry, what's your question?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Reeza

02-24-2017 01:03 AM

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;

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to tappu440

02-24-2017 02:07 AM

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

Maxims of Maximally Efficient SAS Programmers

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to tappu440

02-24-2017 02:16 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to tappu440

02-24-2017 02:48 AM

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

Maxims of Maximally Efficient SAS Programmers

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to KurtBremser

02-24-2017 02:56 AM

@KurtBremser, are you not adding the interest twice:

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

should it not be:

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Shmuel

02-24-2017 03:00 AM

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

Maxims of Maximally Efficient SAS Programmers

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to KurtBremser

02-24-2017 03:11 AM

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Shmuel

02-24-2017 03:45 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to tappu440

02-24-2017 05:22 AM

excellent solutions, thanks for all

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to tappu440

02-24-2017 09:53 AM

This looks like a SAS class quiz/test problem

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to nehalsanghvi

02-24-2017 09:57 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to tappu440

02-24-2017 11:58 AM

Ok Reeza thank you for informing me.