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;
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.
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;
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.
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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.