BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Myurathan
Quartz | Level 8

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

View solution in original post

6 REPLIES 6
Shmuel
Garnet | Level 18

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;
Myurathan
Quartz | Level 8
Thanks @Shmuel. but it also runs forever. 😞
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Myurathan
Quartz | Level 8
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
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Myurathan
Quartz | Level 8
Thank you so much @RW9

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1180 views
  • 0 likes
  • 3 in conversation