Hello everyone. I have a piece of code that I wrote using a macro loop, and It is very inefficient (takes ~ .5-.6 seconds to run, and I need to run it at least 10,000 times). Please see the code attached below. Can anyone take a peek at the code and try to replace it with more efficient code (I am assuming a datastep would be better, but am having problems with lagging variables due to the logic). The main logic is " 1) for each loan it has a 3% chance to default each Month, IF it is not already deaulted. 2) If the loan is already defaulted it has a 99% chance to REMAIN DEFAULTED, and a 1% chance to "CURE" itself back to current). I can't think of a way to do this in a datastep because of the fault that you need the previous months default to determine the odds of a loan being defaulted in the current month, however you have to set the previous default on whether or not it was already defaulted, so it seems like a circular logic reference. Any help would be appreciated. I need to get the entire code run time to < .1 seconds if at all possible. I am stuck on where to go next. If anyone has any questions with the code please let me know! data SummaryData; infile datalines delimiter=','; input NumberOfloans pmtamount Category; length pmtamount 4.; datalines; 5000,1200,1 10000,1500,2 ; data montecarlomaybe(drop=i p Category NumberOfloans); set SummaryData; length loannumber $9.; do i=1 to NumberofLoans; do p=1 to 12; LoanNumber=strip(put(category,6.))||strip(put(i,6.)); month=p; output; end; end; run; %macro LoopVar; data starttime; starttime=datetime(); run; %do i=1 %to 12; %let Ione=%eval(&i-1); %if &i=1 %then %do; data montecarlomaybe&i.(drop=newvar); set montecarlomaybe; length month default mindefaultmonth monthssincedefault 3.; where month=&i.; newvar=ranuni(0); if newvar<.03 then do; default=1; MinDefaultMonth=1; end; MonthsSinceDefault=.; run; %end; %else %do; data montecarlomaybe&i.(drop=Curedrand defaultrand); set montecarlomaybe&Ione.; length month default mindefaultmonth monthssincedefault 3.; Month=&i.; monthssincedefault=Month-MinDefaultMonth; Curedrand=ranuni(0); defaultrand=ranuni(0); if default=1 and curedrand < .01 then do; default=.; MinDefaultMonth=.; monthssincedefault=.; end; else if default=1 and curedrand > .01 then do; Default=1; MinDefaultMonth=MinDefaultMonth; end; else if missing(default)=1 and defaultrand<.03 then do; default=1; MinDefaultMonth=Month; end; run; %end; proc datasets; append base=Analysis data=montecarlomaybe&i.; run; proc sort data=analysis; by loannumber month; run; data starttime1; set starttime; endtime=datetime(); totaltime=endtime-starttime; run; %end; %mend LoopVar; %loopvar
... View more