I wrote this SAS amortization schedule program. First without macro the program compiles fine. Please see code below:
data amortzAB;
balance=480000;
month=0;
do until (month>=30*12);
month=month+1;
/*Compute interest rate amortized;*/
rate= 0.06/12 ;
/*Compute the monthly payment;*/
monthly =(480000)*(rate*(1+rate)**(30*12))/((1+rate)**(30*12)-1);
/*Compute interest paid every month;*/
interest_amount=(balance)*rate;
/*Compute principal toward amount borrowed;*/
principal_toward_paymt=monthly-interest_amount;
/*Updated the new balance after each month;*/
balance=(balance)-principal_toward_paymt;
if (balance<monthly) then monthly=balance;
if (balance <0) then stop;
output;
END;
run;
proc print data=amortzAB;
var balance rate monthly principal_toward_paymt interest_amount month ;
title "Amortization schedule &theplan. with &theperiod. years";
title2 "and a downpayment of &thedeposit.";
run;
Now I turned over to the use of macro but I keep getting this error :
OPTIONS MPRINT MLOGIC SYMBOLGEN;
%macro amortz_macro (theplan=,thebalance=,therate=, theperiod=, thedeposit=);
data amortzAB;
balance=&thebalance.;
month=0;
%do %until (month>=&theperiod.*12);
month=month+1;
/*Compute interest rate amortized;*/
rate= &therate./12 ;
/*Compute the monthly payment;*/
monthly =(&thebalance.)*(rate*(1+rate)**(&theperiod.*12))/((1+rate)**(&theperiod.*12)-1);
/*Compute interest paid every month;*/
interest_amount=(balance)*rate;
/*Compute principal toward amount borrowed;*/
principal_toward_paymt=monthly-interest_amount;
/*Updated the new balance after each month;*/
balance=(balance)-principal_toward_paymt;
%if (balance<monthly) %then monthly=balance;
%if (balance <0) %then stop;
output;
%END;
run;
proc print data=amortzAB;
var balance rate monthly principal_toward_paymt interest_amount month ;
title "Amortization schedule &theplan. with &theperiod. years";
title2 "and a downpayment of &thedeposit.";
run;
%mend amortz_macro;
%amortz_macro(theplan=A,thebalance=480000,therate=0.06, theperiod=30, thedeposit=120000);
%amortz_macro(theplan=B,thebalance=499200,therate=0.055, theperiod=30, thedeposit=120000);
%amortz_macro(theplan=A,thebalance=480000,therate=0.06, theperiod=15, thedeposit=120000);
Can anybody help determine what is the real problem.-Thanks.
Just because you are writing a macro doesn't mean you have to switch to %IF and %DO. Get rid of all the percent signs and you should be fine (keep the ampersands, however).
@Astounding Simply Brilliant!!!!!! line
Thanks for responding.
I noticed two things. First is that the %do %until was the problem. When I replaced it by the %do i=1 %to (&theperiod.*12) ; then the code compiled successfully and produced the expected result.
The second thing that made it work is by removing the % sign from the if then.
Can someone explain to me why especially for the case of the %do %until which did not work?
Thanks.
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.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.