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
- /
- loops

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

07-03-2017 09:33 AM

I want to do using loops

Hi, i have below data

p=100000

interest=10%

will be the total amount after 4 years

i want below screen result

Year | Principal | interest | sum |

1 | 100000 | 0.1 | 110000 |

2 | 11000 | 0.1 | 12100 |

3 | 12100 | 0.1 | 13310 |

4 | 13310 | 0.1 | 14641 |

Accepted Solutions

Solution

07-03-2017
10:21 AM

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

07-03-2017 10:06 AM

shivamarrora0 wrote:

Thanks for the explanation , if possible can you please tell the code

I already supplied the code.

I usually solve such problems a little differently, though:

```
data test;
base = 100000;
int = .10;
do i = 0 to 5;
p = base * (1 + int) ** i;
output;
end;
run;
```

With that formula, you can calculate the value for any year in one single step.

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

Maxims of Maximally Efficient SAS Programmers

Maxims of Maximally Efficient SAS Programmers

All Replies

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

07-03-2017 09:39 AM

Please show the code you already have (as this "problem" is really simple), and where it does not meet your expectations.

If you get unexpected messages in the log, post the log of the whole step.

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

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

07-03-2017 09:45 AM

i just started learning SAS.

data test;

p=100000;

int=.10;

do i = 1 to 5;

p+p*int;

end;

run;

this code will over right the orginal data

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

07-03-2017 09:46 AM

i just started learning SAS.

data test;

p=100000;

int=.10;

do i = 1 to 5;

p+p*int;

end;

run;

this code will over right the original data

data test;

p=100000;

int=.10;

do i = 1 to 5;

p+p*int;

end;

run;

this code will over right the original data

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

07-03-2017 09:50 AM - edited 07-03-2017 09:51 AM

shivamarrora0 wrote:

i just started learning SAS.

data test;

p=100000;

int=.10;

do i = 1 to 5;

p+p*int;

end;

run;

this code will over right the orginal data

When you run a data step that does not have an explicit output statement, SAS will do one everytime it reaches the end of a data step iteration. See it as a hidden "output" right before the run statement.

If you do not have a statement that let's SAS iterate automatically (like a set that reads from a dataset), then SAS will only do one iteration, and that's it.

So in your case, SAS performs the loop, and then writes one observation with the final values.

To get a series of observations, add the explicit output statement:

```
data test;
p = 100000;
int = .10;
do i = 1 to 5;
p + p * int;
output;
end;
run;
```

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

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

07-03-2017 09:57 AM

Thanks for the explanation , if possible can you please tell the code

Solution

07-03-2017
10:21 AM

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

07-03-2017 10:06 AM

shivamarrora0 wrote:

Thanks for the explanation , if possible can you please tell the code

I already supplied the code.

I usually solve such problems a little differently, though:

```
data test;
base = 100000;
int = .10;
do i = 0 to 5;
p = base * (1 + int) ** i;
output;
end;
run;
```

With that formula, you can calculate the value for any year in one single step.

Maxims of Maximally Efficient SAS Programmers