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 until end?

Topic Options

- 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
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-03-2018 04:53 AM

I am new to sas. I am trying to run following code. It runs for ever. What am I doing wrong? I am tryting to see how many years it takes to payback loan.

```
data loan;
initial_payment=1000;
loan=30000-initial_payment;
payment=500;
years=0;
do until (loan_end=0);
years+1;
loan_end=loan-payment;
end;
run;
```

Thanks in advance.

**run**;

Accepted Solutions

Solution

05-03-2018
05:34 AM

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

Posted in reply to Myurathan

05-03-2018 05:24 AM

In @Shmuel's example the value of loan_end is always loan-payment, 28500, hence it never lowers down to 0 or less. Update this to :

data loan; initial_payment=1000; loan=30000-initial_payment; payment=500; years=0; loan_end=loan; do until (loan_end le 0); years+1; loan_end=loan_end-payment; end; run;

So we set loan_end to be loan, then take payment off loan_end each time, so loan_end goes down We could alos write it as:

data loan; initial_payment=1000; loan=30000-initial_payment; payment=500; years=0; do until (loan le 0); years+1; loan=loan-payment; end; run;

Basically you need to ensure the condition in the until is triggered, which as it was being created each round never happened.

All Replies

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

Posted in reply to Myurathan

05-03-2018 04:59 AM

Try:

```
data loan;
initial_payment=1000;
loan=30000-initial_payment;
payment=500;
years=0;
do until (loan_end LE 0);
years+1;
loan_end=loan-payment;
end;
run;
```

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

Posted in reply to Shmuel

05-03-2018 05:02 AM

Thanks @Shmuel. but it also runs forever.

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

Posted in reply to Myurathan

05-03-2018 05:13 AM

Why do you need the loop, it is a simple calculation:

data loan; initial_payment=1000; loan=30000-initial_payment; payment=500; years=loan / payment; run;

As for why it does not work, well, your condition loan_end le 0 must never be true.

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

05-03-2018 05:15 AM

Thanks RW9. I am aware that it is a simple calulation. I am trying to learn looping. Can you please explain why loan_end le 0 never be true?. Thanks

Solution

05-03-2018
05:34 AM

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

Posted in reply to Myurathan

05-03-2018 05:24 AM

In @Shmuel's example the value of loan_end is always loan-payment, 28500, hence it never lowers down to 0 or less. Update this to :

data loan; initial_payment=1000; loan=30000-initial_payment; payment=500; years=0; loan_end=loan; do until (loan_end le 0); years+1; loan_end=loan_end-payment; end; run;

So we set loan_end to be loan, then take payment off loan_end each time, so loan_end goes down We could alos write it as:

data loan; initial_payment=1000; loan=30000-initial_payment; payment=500; years=0; do until (loan le 0); years+1; loan=loan-payment; end; run;

Basically you need to ensure the condition in the until is triggered, which as it was being created each round never happened.