Statistical programming, matrix languages, and more

Long simulation code generates "Error: An exception has been encountered"

Reply
Occasional Contributor
Posts: 13

Long simulation code generates "Error: An exception has been encountered"

Hi all,

 

I am running a simulation with 100 iterations. It is lengthy--each iteration takes about 7 minutes. When I run the code below, it runs for many hours and eventually stops running, displaying the error listed in the title. 


When I run the code with only a few iterations (less than five), it runs correctly with no issues. Any help is greatly appreciated. Thanks!

 

 

 

Here is the code:

 


%macro rule_stay(output,avc,cutoff_stay);
proc iml;
use &output;
read all var {jun04 jul04 aug04 sep04 oct04 nov04 dec04 jan05 feb05 mar05 apr05 may05 jun05 jul05 aug05 sep05 oct05 nov05 dec05 jan06 feb06 mar06 apr06 may06 jun06 jul06 aug06 sep06 oct06 nov06 dec06 jan07 feb07 mar07 apr07 may07 jun07 jul07 aug07 sep07 oct07 nov07 dec07 jan08 feb08 mar08 apr08 may08 jun08 jul08 aug08 sep08 oct08 nov08 dec08 jan09 feb09 mar09 apr09 may09 jun09 jul09 aug09 sep09 oct09 nov09 dec09 jan10 feb10 mar10 apr10 may10 jun10 jul10 aug10 sep10 oct10 nov10 dec10 jan11 feb11 mar11 apr11 may11 jun11 jul11 aug11 sep11 oct11 nov11 dec11 jan12 feb12 mar12 apr12 may12 jun12 jul12 aug12 sep12 oct12 nov12 dec12 jan13 feb13 mar13 apr13 may13 jun13 jul13 aug13 sep13 oct13 nov13 dec13 jan14 feb14 mar14 apr14 may14 jun14 jul14 aug14 sep14 oct14 nov14 dec14 jan15 feb15 mar15 apr15 may15} into month_lb;
n_months=ncol(month_lb);
n_farms=nrow(month_lb);

use &avc;
read all var {jun04 jul04 aug04 sep04 oct04 nov04 dec04 jan05 feb05 mar05 apr05 may05 jun05 jul05 aug05 sep05 oct05 nov05 dec05 jan06 feb06 mar06 apr06 may06 jun06 jul06 aug06 sep06 oct06 nov06 dec06 jan07 feb07 mar07 apr07 may07 jun07 jul07 aug07 sep07 oct07 nov07 dec07 jan08 feb08 mar08 apr08 may08 jun08 jul08 aug08 sep08 oct08 nov08 dec08 jan09 feb09 mar09 apr09 may09 jun09 jul09 aug09 sep09 oct09 nov09 dec09 jan10 feb10 mar10 apr10 may10 jun10 jul10 aug10 sep10 oct10 nov10 dec10 jan11 feb11 mar11 apr11 may11 jun11 jul11 aug11 sep11 oct11 nov11 dec11 jan12 feb12 mar12 apr12 may12 jun12 jul12 aug12 sep12 oct12 nov12 dec12 jan13 feb13 mar13 apr13 may13 jun13 jul13 aug13 sep13 oct13 nov13 dec13 jan14 feb14 mar14 apr14 may14 jun14 jul14 aug14 sep14 oct14 nov14 dec14 jan15 feb15 mar15 apr15 may15} into avc;
n_months=ncol(avc);
n_farms=nrow(avc);

use prices;
read all var {jun04 jul04 aug04 sep04 oct04 nov04 dec04 jan05 feb05 mar05 apr05 may05 jun05 jul05 aug05 sep05 oct05 nov05 dec05 jan06 feb06 mar06 apr06 may06 jun06 jul06 aug06 sep06 oct06 nov06 dec06 jan07 feb07 mar07 apr07 may07 jun07 jul07 aug07 sep07 oct07 nov07 dec07 jan08 feb08 mar08 apr08 may08 jun08 jul08 aug08 sep08 oct08 nov08 dec08 jan09 feb09 mar09 apr09 may09 jun09 jul09 aug09 sep09 oct09 nov09 dec09 jan10 feb10 mar10 apr10 may10 jun10 jul10 aug10 sep10 oct10 nov10 dec10 jan11 feb11 mar11 apr11 may11 jun11 jul11 aug11 sep11 oct11 nov11 dec11 jan12 feb12 mar12 apr12 may12 jun12 jul12 aug12 sep12 oct12 nov12 dec12 jan13 feb13 mar13 apr13 may13 jun13 jul13 aug13 sep13 oct13 nov13 dec13 jan14 feb14 mar14 apr14 may14 jun14 jul14 aug14 sep14 oct14 nov14 dec14 jan15 feb15 mar15 apr15 may15} into price;
n_months=ncol(price);
n_farms=nrow(price);

call randseed(datetime());
epsilon=j(n_farms,n_months,.);
mu=0;
sigma= 4.0233929;
call randgen(epsilon,"normal",mu,sigma);

stoch_avc=j(n_farms,n_months,0);
month_cwt=j(n_farms,n_months,0);
month_prof=j(n_farms,n_months,0);
profits=j(n_farms,n_months,0);
outputs=j(n_farms,n_months,0);
vprofpercwt=j(n_farms,n_months,0);

real_exit=j(n_farms,n_months,.);
real_stay=j(n_farms,n_months,.);
theory_exit=j(n_farms,n_months,0);
theory_stay=j(n_farms,n_months,0);

stay_acc=j(n_farms,n_months,.);
exit_acc=j(n_farms,n_months,.);

stay_output=j(301,2,.);

stay_rule=j(1,2,.);
exit_rule=j(1,2,.);


do i=1 to n_farms;
do j=7 to n_months;

if avc[i,j]>0 then stoch_avc[i,j]=(avc[i,j]+epsilon[i,j]);

month_cwt[i,j]=month_lb[i,j]/100;

if month_cwt[i,j]>0 then month_prof[i,j]=(price[i,j]-stoch_avc[i,j])*month_cwt[i,j];

outputs[i,j]=(month_cwt[i,j-1]+month_cwt[i,j-2]+month_cwt[i,j-3]+month_cwt[i,j-4]+month_cwt[i,j-5]+month_cwt[i,j-6]);
profits[i,j]=(month_prof[i,j-1]+month_prof[i,j-2]+month_prof[i,j-3]+month_prof[i,j-4]+month_prof[i,j-5]+month_prof[i,j-6]);
if outputs[i,j]>0 then vprofpercwt[i,j]=profits[i,j]/outputs[i,j];

if month_cwt[i,j]>0 then real_stay[i,j]=1;

if vprofpercwt[i,j]>=(&cutoff_stay/100) then theory_stay[i,j]=1;
if real_stay[i,j]=. then theory_stay[i,j]=.;

if real_stay[i,j]^=. then stay_acc[i,j]=(real_stay[i,j]=theory_stay[i,j]);

if ((month_cwt[i,j]=0)*(month_cwt[i,j-1]^=0))=1 then real_exit[i,j]=1;
if ((month_cwt[i,j]=0)*(month_cwt[i,j-1]=0))=1 then real_exit[i,j]=.;

if vprofpercwt[i,j]<(&cutoff_stay/100) then theory_exit[i,j]=1;
if real_exit[i,j]=. then theory_exit[i,j]=.;

if real_exit[i,j]^=. then exit_acc[i,j]=(real_exit[i,j]=theory_exit[i,j]);

end;
end;
stay=stay_acc[:];

s=&cutoff_stay+601;

*creates outlier fence for stay;
stay=stay_acc[:];
stay_output[s,1]=stay;
stay_output[s,2]=&cutoff_stay;
if stay_output[s,1]<.95 then stay_output[s,1]=.;
stay_max=stay_output[><, ];
if stay_output[s,1]>stay_max[1,1] then stay_output[s,1]=.;

if stay_output[s,1]>0 then stay_rule[1,1]=stay_output[s,1];
stay_rule[1,2]=stay_output[s,2];
stay_outlier_percent=stay_rule[1,1];
stay_outlier_fence=stay_rule[1,2];
create temp_stay var {stay_outlier_fence stay_outlier_percent};
append var {stay_outlier_fence stay_outlier_percent};

quit;
%mend;

 

%macro rule_exit(output,avc,cutoff_exit);
proc iml;
use &output;
read all var {jun04 jul04 aug04 sep04 oct04 nov04 dec04 jan05 feb05 mar05 apr05 may05 jun05 jul05 aug05 sep05 oct05 nov05 dec05 jan06 feb06 mar06 apr06 may06 jun06 jul06 aug06 sep06 oct06 nov06 dec06 jan07 feb07 mar07 apr07 may07 jun07 jul07 aug07 sep07 oct07 nov07 dec07 jan08 feb08 mar08 apr08 may08 jun08 jul08 aug08 sep08 oct08 nov08 dec08 jan09 feb09 mar09 apr09 may09 jun09 jul09 aug09 sep09 oct09 nov09 dec09 jan10 feb10 mar10 apr10 may10 jun10 jul10 aug10 sep10 oct10 nov10 dec10 jan11 feb11 mar11 apr11 may11 jun11 jul11 aug11 sep11 oct11 nov11 dec11 jan12 feb12 mar12 apr12 may12 jun12 jul12 aug12 sep12 oct12 nov12 dec12 jan13 feb13 mar13 apr13 may13 jun13 jul13 aug13 sep13 oct13 nov13 dec13 jan14 feb14 mar14 apr14 may14 jun14 jul14 aug14 sep14 oct14 nov14 dec14 jan15 feb15 mar15 apr15 may15} into month_lb;
n_months=ncol(month_lb);
n_farms=nrow(month_lb);

use &avc;
read all var {jun04 jul04 aug04 sep04 oct04 nov04 dec04 jan05 feb05 mar05 apr05 may05 jun05 jul05 aug05 sep05 oct05 nov05 dec05 jan06 feb06 mar06 apr06 may06 jun06 jul06 aug06 sep06 oct06 nov06 dec06 jan07 feb07 mar07 apr07 may07 jun07 jul07 aug07 sep07 oct07 nov07 dec07 jan08 feb08 mar08 apr08 may08 jun08 jul08 aug08 sep08 oct08 nov08 dec08 jan09 feb09 mar09 apr09 may09 jun09 jul09 aug09 sep09 oct09 nov09 dec09 jan10 feb10 mar10 apr10 may10 jun10 jul10 aug10 sep10 oct10 nov10 dec10 jan11 feb11 mar11 apr11 may11 jun11 jul11 aug11 sep11 oct11 nov11 dec11 jan12 feb12 mar12 apr12 may12 jun12 jul12 aug12 sep12 oct12 nov12 dec12 jan13 feb13 mar13 apr13 may13 jun13 jul13 aug13 sep13 oct13 nov13 dec13 jan14 feb14 mar14 apr14 may14 jun14 jul14 aug14 sep14 oct14 nov14 dec14 jan15 feb15 mar15 apr15 may15} into avc;
n_months=ncol(avc);
n_farms=nrow(avc);

use prices;
read all var {jun04 jul04 aug04 sep04 oct04 nov04 dec04 jan05 feb05 mar05 apr05 may05 jun05 jul05 aug05 sep05 oct05 nov05 dec05 jan06 feb06 mar06 apr06 may06 jun06 jul06 aug06 sep06 oct06 nov06 dec06 jan07 feb07 mar07 apr07 may07 jun07 jul07 aug07 sep07 oct07 nov07 dec07 jan08 feb08 mar08 apr08 may08 jun08 jul08 aug08 sep08 oct08 nov08 dec08 jan09 feb09 mar09 apr09 may09 jun09 jul09 aug09 sep09 oct09 nov09 dec09 jan10 feb10 mar10 apr10 may10 jun10 jul10 aug10 sep10 oct10 nov10 dec10 jan11 feb11 mar11 apr11 may11 jun11 jul11 aug11 sep11 oct11 nov11 dec11 jan12 feb12 mar12 apr12 may12 jun12 jul12 aug12 sep12 oct12 nov12 dec12 jan13 feb13 mar13 apr13 may13 jun13 jul13 aug13 sep13 oct13 nov13 dec13 jan14 feb14 mar14 apr14 may14 jun14 jul14 aug14 sep14 oct14 nov14 dec14 jan15 feb15 mar15 apr15 may15} into price;
n_months=ncol(price);
n_farms=nrow(price);

call randseed(datetime());
epsilon=j(n_farms,n_months,.);
mu=0;
sigma= 4.0233929;
call randgen(epsilon,"normal",mu,sigma);

stoch_avc=j(n_farms,n_months,0);
month_cwt=j(n_farms,n_months,0);
month_prof=j(n_farms,n_months,0);
profits=j(n_farms,n_months,0);
outputs=j(n_farms,n_months,0);
vprofpercwt=j(n_farms,n_months,0);

real_exit=j(n_farms,n_months,.);
real_stay=j(n_farms,n_months,.);
theory_exit=j(n_farms,n_months,0);
theory_stay=j(n_farms,n_months,0);

stay_acc=j(n_farms,n_months,.);
exit_acc=j(n_farms,n_months,.);

exit_output=j(301,2,.);

stay_rule=j(1,2,.);
exit_rule=j(1,2,.);

*loop does accuracy calculation for single cutoff value;
do i=1 to n_farms;
do j=7 to n_months;

if avc[i,j]>0 then stoch_avc[i,j]=(avc[i,j]+epsilon[i,j]);

month_cwt[i,j]=month_lb[i,j]/100;

if month_cwt[i,j]>0 then month_prof[i,j]=(price[i,j]-stoch_avc[i,j])*month_cwt[i,j];

outputs[i,j]=(month_cwt[i,j-1]+month_cwt[i,j-2]+month_cwt[i,j-3]+month_cwt[i,j-4]+month_cwt[i,j-5]+month_cwt[i,j-6]);
profits[i,j]=(month_prof[i,j-1]+month_prof[i,j-2]+month_prof[i,j-3]+month_prof[i,j-4]+month_prof[i,j-5]+month_prof[i,j-6]);
if outputs[i,j]>0 then vprofpercwt[i,j]=profits[i,j]/outputs[i,j];

if month_cwt[i,j]>0 then real_stay[i,j]=1;

if vprofpercwt[i,j]>=(&cutoff_exit/100) then theory_stay[i,j]=1;
if real_stay[i,j]=. then theory_stay[i,j]=.;

if real_stay[i,j]^=. then stay_acc[i,j]=(real_stay[i,j]=theory_stay[i,j]);

if ((month_cwt[i,j]=0)*(month_cwt[i,j-1]^=0))=1 then real_exit[i,j]=1;
if ((month_cwt[i,j]=0)*(month_cwt[i,j-1]=0))=1 then real_exit[i,j]=.;

if vprofpercwt[i,j]<(&cutoff_exit/100) then theory_exit[i,j]=1;
if real_exit[i,j]=. then theory_exit[i,j]=.;

if real_exit[i,j]^=. then exit_acc[i,j]=(real_exit[i,j]=theory_exit[i,j]);

end;
end;

exit=exit_acc[:];
r=&cutoff_exit-399;

*creates outlier fence for exit;
exit=exit_acc[:];
exit_output[r,1]=exit;
exit_output[r,2]=&cutoff_exit;
if exit_output[r,1]<.95 then exit_output[r,1]=.;
exit_max=exit_output[><, ];
if exit_output[r,1]>exit_max[1,1] then exit_output[r,1]=.;

if exit_output[r,1]>0 then exit_rule[1,1]=exit_output[r,1];
exit_rule[1,2]=exit_output[r,2];
exit_outlier_percent=exit_rule[1,1];
exit_outlier_fence=exit_rule[1,2];
create temp_exit var {exit_outlier_fence exit_outlier_percent};
append var {exit_outlier_fence exit_outlier_percent};

quit;
%mend;

 

 

 

 

 

 

 

 

 

 

 


%macro iterate(output,avc,cutoff_stay,cutoff_exit,prices,spring,winter,fall);
proc iml;
create perm_stay var {stay_outlier_fence stay_outlier_percent};
create perm_exit var {exit_outlier_fence exit_outlier_percent};
*loop evaluates accuracies of cutoff values and saves those over 95%;
%do cutoff_exit=400 %to 500;
%rule_exit(&output,&avc,&cutoff_exit);
proc append base=work.perm_exit data=work.temp_exit;run;
%end;

%do cutoff_stay=-600 %to -300;
%rule_stay(&output,&avc,&cutoff_stay);
proc append base=work.perm_stay data=work.temp_stay;run;
%end;

quit;

proc iml;
use work.perm_stay;
read all var {stay_outlier_fence stay_outlier_percent} into stay_ruleset;
n_trials=nrow(stay_ruleset);
n_var=ncol(stay_ruleset);
use work.perm_exit;
read all var {exit_outlier_fence exit_outlier_percent} into exit_ruleset;
n_trials=nrow(exit_ruleset);
n_var=ncol(exit_ruleset);

stay_fence= j(1,1,.);
stay_prob=j(1,1,.);

exit_fence= j(1,1,.);
exit_prob=j(1,1,.);
*loop defines outlier rules: saves rule closest to 95%;
do i=1 to n_trials;
do j=1 to n_var;
stay_max=stay_ruleset[><, ];
if stay_ruleset[i,2]>stay_max[1,2] then stay_ruleset[i,2]=.;
if stay_ruleset[i,2]>0 then stay_prob[1,1]=stay_ruleset[i,2];
if stay_ruleset[i,2]>0 then stay_fence[1,1]=stay_ruleset[i,1];

exit_max=exit_ruleset[><, ];
if exit_ruleset[i,2]>exit_max[1,2] then exit_ruleset[i,2]=.;
if exit_ruleset[i,2]>0 then exit_prob[1,1]=exit_ruleset[i,2];
if exit_ruleset[i,2]>0 then exit_fence[1,1]=exit_ruleset[i,1];
end;
end;

create exit_prob from exit_prob;
append from exit_prob;
create exit_fence from exit_fence;
append from exit_fence;

create stay_prob from stay_prob;
append from stay_prob;
create stay_fence from stay_fence;
append from stay_fence;

*FORECAST CODE;
use work.mmc_id;
read all var {jun04 jul04 aug04 sep04 oct04 nov04 dec04 jan05 feb05 mar05 apr05 may05 jun05 jul05 aug05 sep05 oct05 nov05 dec05 jan06 feb06 mar06 apr06 may06 jun06 jul06 aug06 sep06 oct06 nov06 dec06 jan07 feb07 mar07 apr07 may07 jun07 jul07 aug07 sep07 oct07 nov07 dec07 jan08 feb08 mar08 apr08 may08 jun08 jul08 aug08 sep08 oct08 nov08 dec08 jan09 feb09 mar09 apr09 may09 jun09 jul09 aug09 sep09 oct09 nov09 dec09 jan10 feb10 mar10 apr10 may10 jun10 jul10 aug10 sep10 oct10 nov10 dec10 jan11 feb11 mar11 apr11 may11 jun11 jul11 aug11 sep11 oct11 nov11 dec11 jan12 feb12 mar12 apr12 may12 jun12 jul12 aug12 sep12 oct12 nov12 dec12 jan13 feb13 mar13 apr13 may13 jun13 jul13 aug13 sep13 oct13 nov13 dec13 jan14 feb14 mar14 apr14 may14 jun14 jul14 aug14 sep14 oct14 nov14 dec14 jan15 feb15 mar15 apr15 may15} into ID;
n_months=ncol(ID);
n_farms=nrow(ID);

use work.exit;
read all var {jun04 jul04 aug04 sep04 oct04 nov04 dec04 jan05 feb05 mar05 apr05 may05 jun05 jul05 aug05 sep05 oct05 nov05 dec05 jan06 feb06 mar06 apr06 may06 jun06 jul06 aug06 sep06 oct06 nov06 dec06 jan07 feb07 mar07 apr07 may07 jun07 jul07 aug07 sep07 oct07 nov07 dec07 jan08 feb08 mar08 apr08 may08 jun08 jul08 aug08 sep08 oct08 nov08 dec08 jan09 feb09 mar09 apr09 may09 jun09 jul09 aug09 sep09 oct09 nov09 dec09 jan10 feb10 mar10 apr10 may10 jun10 jul10 aug10 sep10 oct10 nov10 dec10 jan11 feb11 mar11 apr11 may11 jun11 jul11 aug11 sep11 oct11 nov11 dec11 jan12 feb12 mar12 apr12 may12 jun12 jul12 aug12 sep12 oct12 nov12 dec12 jan13 feb13 mar13 apr13 may13 jun13 jul13 aug13 sep13 oct13 nov13 dec13 jan14 feb14 mar14 apr14 may14 jun14 jul14 aug14 sep14 oct14 nov14 dec14 jan15 feb15 mar15 apr15 may15} into exit;
n_months=ncol(exit);
n_farms=nrow(exit);

use &fall;
read all var {jun04 jul04 aug04 sep04 oct04 nov04 dec04 jan05 feb05 mar05 apr05 may05 jun05 jul05 aug05 sep05 oct05 nov05 dec05 jan06 feb06 mar06 apr06 may06 jun06 jul06 aug06 sep06 oct06 nov06 dec06 jan07 feb07 mar07 apr07 may07 jun07 jul07 aug07 sep07 oct07 nov07 dec07 jan08 feb08 mar08 apr08 may08 jun08 jul08 aug08 sep08 oct08 nov08 dec08 jan09 feb09 mar09 apr09 may09 jun09 jul09 aug09 sep09 oct09 nov09 dec09 jan10 feb10 mar10 apr10 may10 jun10 jul10 aug10 sep10 oct10 nov10 dec10 jan11 feb11 mar11 apr11 may11 jun11 jul11 aug11 sep11 oct11 nov11 dec11 jan12 feb12 mar12 apr12 may12 jun12 jul12 aug12 sep12 oct12 nov12 dec12 jan13 feb13 mar13 apr13 may13 jun13 jul13 aug13 sep13 oct13 nov13 dec13 jan14 feb14 mar14 apr14 may14 jun14 jul14 aug14 sep14 oct14 nov14 dec14 jan15 feb15 mar15 apr15 may15} into fall;
n_months=ncol(fall);
n_farms=nrow(fall);
use &winter;
read all var {jun04 jul04 aug04 sep04 oct04 nov04 dec04 jan05 feb05 mar05 apr05 may05 jun05 jul05 aug05 sep05 oct05 nov05 dec05 jan06 feb06 mar06 apr06 may06 jun06 jul06 aug06 sep06 oct06 nov06 dec06 jan07 feb07 mar07 apr07 may07 jun07 jul07 aug07 sep07 oct07 nov07 dec07 jan08 feb08 mar08 apr08 may08 jun08 jul08 aug08 sep08 oct08 nov08 dec08 jan09 feb09 mar09 apr09 may09 jun09 jul09 aug09 sep09 oct09 nov09 dec09 jan10 feb10 mar10 apr10 may10 jun10 jul10 aug10 sep10 oct10 nov10 dec10 jan11 feb11 mar11 apr11 may11 jun11 jul11 aug11 sep11 oct11 nov11 dec11 jan12 feb12 mar12 apr12 may12 jun12 jul12 aug12 sep12 oct12 nov12 dec12 jan13 feb13 mar13 apr13 may13 jun13 jul13 aug13 sep13 oct13 nov13 dec13 jan14 feb14 mar14 apr14 may14 jun14 jul14 aug14 sep14 oct14 nov14 dec14 jan15 feb15 mar15 apr15 may15} into winter;
n_months=ncol(winter);
n_farms=nrow(winter);
use &spring;
read all var {jun04 jul04 aug04 sep04 oct04 nov04 dec04 jan05 feb05 mar05 apr05 may05 jun05 jul05 aug05 sep05 oct05 nov05 dec05 jan06 feb06 mar06 apr06 may06 jun06 jul06 aug06 sep06 oct06 nov06 dec06 jan07 feb07 mar07 apr07 may07 jun07 jul07 aug07 sep07 oct07 nov07 dec07 jan08 feb08 mar08 apr08 may08 jun08 jul08 aug08 sep08 oct08 nov08 dec08 jan09 feb09 mar09 apr09 may09 jun09 jul09 aug09 sep09 oct09 nov09 dec09 jan10 feb10 mar10 apr10 may10 jun10 jul10 aug10 sep10 oct10 nov10 dec10 jan11 feb11 mar11 apr11 may11 jun11 jul11 aug11 sep11 oct11 nov11 dec11 jan12 feb12 mar12 apr12 may12 jun12 jul12 aug12 sep12 oct12 nov12 dec12 jan13 feb13 mar13 apr13 may13 jun13 jul13 aug13 sep13 oct13 nov13 dec13 jan14 feb14 mar14 apr14 may14 jun14 jul14 aug14 sep14 oct14 nov14 dec14 jan15 feb15 mar15 apr15 may15} into spring;
n_months=ncol(spring);
n_farms=nrow(spring);

use &prices;
read all var {jun04 jul04 aug04 sep04 oct04 nov04 dec04 jan05 feb05 mar05 apr05 may05 jun05 jul05 aug05 sep05 oct05 nov05 dec05 jan06 feb06 mar06 apr06 may06 jun06 jul06 aug06 sep06 oct06 nov06 dec06 jan07 feb07 mar07 apr07 may07 jun07 jul07 aug07 sep07 oct07 nov07 dec07 jan08 feb08 mar08 apr08 may08 jun08 jul08 aug08 sep08 oct08 nov08 dec08 jan09 feb09 mar09 apr09 may09 jun09 jul09 aug09 sep09 oct09 nov09 dec09 jan10 feb10 mar10 apr10 may10 jun10 jul10 aug10 sep10 oct10 nov10 dec10 jan11 feb11 mar11 apr11 may11 jun11 jul11 aug11 sep11 oct11 nov11 dec11 jan12 feb12 mar12 apr12 may12 jun12 jul12 aug12 sep12 oct12 nov12 dec12 jan13 feb13 mar13 apr13 may13 jun13 jul13 aug13 sep13 oct13 nov13 dec13 jan14 feb14 mar14 apr14 may14 jun14 jul14 aug14 sep14 oct14 nov14 dec14 jan15 feb15 mar15 apr15 may15} into price;
n_months=ncol(price);
n_farms=nrow(price);

use &avc;
read all var {jun04 jul04 aug04 sep04 oct04 nov04 dec04 jan05 feb05 mar05 apr05 may05 jun05 jul05 aug05 sep05 oct05 nov05 dec05 jan06 feb06 mar06 apr06 may06 jun06 jul06 aug06 sep06 oct06 nov06 dec06 jan07 feb07 mar07 apr07 may07 jun07 jul07 aug07 sep07 oct07 nov07 dec07 jan08 feb08 mar08 apr08 may08 jun08 jul08 aug08 sep08 oct08 nov08 dec08 jan09 feb09 mar09 apr09 may09 jun09 jul09 aug09 sep09 oct09 nov09 dec09 jan10 feb10 mar10 apr10 may10 jun10 jul10 aug10 sep10 oct10 nov10 dec10 jan11 feb11 mar11 apr11 may11 jun11 jul11 aug11 sep11 oct11 nov11 dec11 jan12 feb12 mar12 apr12 may12 jun12 jul12 aug12 sep12 oct12 nov12 dec12 jan13 feb13 mar13 apr13 may13 jun13 jul13 aug13 sep13 oct13 nov13 dec13 jan14 feb14 mar14 apr14 may14 jun14 jul14 aug14 sep14 oct14 nov14 dec14 jan15 feb15 mar15 apr15 may15} into avc;
n_months=ncol(avc);
n_farms=nrow(avc);

use &output;
read all var {jun04 jul04 aug04 sep04 oct04 nov04 dec04 jan05 feb05 mar05 apr05 may05 jun05 jul05 aug05 sep05 oct05 nov05 dec05 jan06 feb06 mar06 apr06 may06 jun06 jul06 aug06 sep06 oct06 nov06 dec06 jan07 feb07 mar07 apr07 may07 jun07 jul07 aug07 sep07 oct07 nov07 dec07 jan08 feb08 mar08 apr08 may08 jun08 jul08 aug08 sep08 oct08 nov08 dec08 jan09 feb09 mar09 apr09 may09 jun09 jul09 aug09 sep09 oct09 nov09 dec09 jan10 feb10 mar10 apr10 may10 jun10 jul10 aug10 sep10 oct10 nov10 dec10 jan11 feb11 mar11 apr11 may11 jun11 jul11 aug11 sep11 oct11 nov11 dec11 jan12 feb12 mar12 apr12 may12 jun12 jul12 aug12 sep12 oct12 nov12 dec12 jan13 feb13 mar13 apr13 may13 jun13 jul13 aug13 sep13 oct13 nov13 dec13 jan14 feb14 mar14 apr14 may14 jun14 jul14 aug14 sep14 oct14 nov14 dec14 jan15 feb15 mar15 apr15 may15} into month_lb;
n_months=ncol(month_lb);
n_farms=nrow(month_lb);

real_stay=j(n_farms,n_months,.);
real_exit=j(n_farms,n_months,.);
stoch_avc=j(n_farms,n_months,0);
profits=j(n_farms,n_months,0);
outputs=j(n_farms,n_months,0);
month_prof=j(n_farms,n_months,0);
month_cwt=j(n_farms,n_months,0);
vprofpercwt=j(n_farms,n_months,0);

price_lag_1mo=j(n_farms,n_months,.);
price_lag_2mo=j(n_farms,n_months,.);
price_lag_3mo=j(n_farms,n_months,.);
price_lag_4mo=j(n_farms,n_months,.);
price_lag_5mo=j(n_farms,n_months,.);
price_lag_6mo=j(n_farms,n_months,.);
avc_lag_1mo=j(n_farms,n_months,.);
avc_lag_2mo=j(n_farms,n_months,.);
avc_lag_3mo=j(n_farms,n_months,.);
avc_lag_4mo=j(n_farms,n_months,.);
avc_lag_5mo=j(n_farms,n_months,.);
avc_lag_6mo=j(n_farms,n_months,.);

exit_rule_fence=repeat(exit_fence,n_farms,n_months); /*variable for outlier removal in probit dataset*/

stay_rule_fence=repeat(stay_fence,n_farms,n_months); /*variable for outlier removal in probit dataset*/

call randseed(datetime());
epsilon=j(n_farms,n_months,.);
mu=0;
sigma= 4.0233929;
call randgen(epsilon,"normal",mu,sigma);

*loop defines probitdataset variables;
do i=1 to n_farms;
do j=7 to n_months;

month_cwt[i,j]=month_lb[i,j]/100;

if month_cwt[i,j]>0 then real_stay[i,j]=1;

if ((month_cwt[i,j]=0)*(month_cwt[i,j-1]^=0))=1 then real_exit[i,j]=1;
if ((month_cwt[i,j]=0)*(month_cwt[i,j-1]=0))=1 then real_exit[i,j]=.;

if avc[i,j]>0 then stoch_avc[i,j]=(avc[i,j]+epsilon[i,j]);

if month_cwt[i,j]>0 then month_prof[i,j]=(price[i,j]-stoch_avc[i,j])*month_cwt[i,j];

outputs[i,j]=(month_cwt[i,j-1]+month_cwt[i,j-2]+month_cwt[i,j-3]+month_cwt[i,j-4]+month_cwt[i,j-5]+month_cwt[i,j-6]);
profits[i,j]=(month_prof[i,j-1]+month_prof[i,j-2]+month_prof[i,j-3]+month_prof[i,j-4]+month_prof[i,j-5]+month_prof[i,j-6]);
if outputs[i,j]>0 then vprofpercwt[i,j]=profits[i,j]/outputs[i,j];

price_lag_1mo[i,j]=price[i,j-1];
price_lag_2mo[i,j]=price[i,j-2];
price_lag_3mo[i,j]=price[i,j-3];
price_lag_4mo[i,j]=price[i,j-4];
price_lag_5mo[i,j]=price[i,j-5];
price_lag_6mo[i,j]=price[i,j-6];

avc_lag_1mo[i,j]=stoch_avc[i,j-1];
avc_lag_2mo[i,j]=stoch_avc[i,j-2];
avc_lag_3mo[i,j]=stoch_avc[i,j-3];
avc_lag_4mo[i,j]=stoch_avc[i,j-4];
avc_lag_5mo[i,j]=stoch_avc[i,j-5];
avc_lag_6mo[i,j]=stoch_avc[i,j-6];

end;
end;
create stoch_avc from stoch_avc;
append from stoch_avc;

stay_fence=colvec(stay_rule_fence);
exit_fence=colvec(exit_rule_fence);
exit_vec = colvec(exit);
MMC_ID=colvec(ID);
Month_vec = colvec(col(month_cwt));
fall_Vec = colvec(fall);
win_vec = colvec(winter);
spring_vec = colvec(spring);
vprofpercwt_vec=colvec(vprofpercwt);
avc_lag_1mo_vec=colvec(avc_lag_1mo);
avc_lag_2mo_vec=colvec(avc_lag_2mo);
avc_lag_3mo_vec=colvec(avc_lag_3mo);
avc_lag_4mo_vec=colvec(avc_lag_4mo);
avc_lag_5mo_vec=colvec(avc_lag_5mo);
avc_lag_6mo_vec=colvec(avc_lag_6mo);
price_lag_1mo_vec=colvec(price_lag_1mo);
price_lag_2mo_vec=colvec(price_lag_2mo);
price_lag_3mo_vec=colvec(price_lag_3mo);
price_lag_4mo_vec=colvec(price_lag_4mo);
price_lag_5mo_vec=colvec(price_lag_5mo);
price_lag_6mo_vec=colvec(price_lag_6mo);

create probitdata var {MMC_ID Month_vec Exit_vec fall_vec win_vec spring_vec vprofpercwt_vec stay_fence exit_fence
price_lag_1mo_vec price_lag_2mo_vec price_lag_3mo_vec price_lag_4mo_vec price_lag_5mo_vec price_lag_6mo_vec
avc_lag_1mo_vec avc_lag_2mo_vec avc_lag_3mo_vec avc_lag_4mo_vec avc_lag_5mo_vec avc_lag_6mo_vec};
append;

data probitdata2;
set probitdata;
if vprofpercwt_vec>exit_fence/100 and exit_vec=1 then exit_vec=.;
if vprofpercwt_vec<=stay_fence/100 and exit_vec=0 then exit_vec=.;
exit_num=input(exit_vec,1.);
run;


proc qlim data=work.probitdata2;
model exit_num = fall_vec win_vec spring_vec price_lag_1mo_vec price_lag_2mo_vec price_lag_3mo_vec price_lag_4mo_vec price_lag_5mo_vec price_lag_6mo_vec avc_lag_1mo_vec avc_lag_2mo_vec avc_lag_3mo_vec avc_lag_4mo_vec avc_lag_5mo_vec avc_lag_6mo_vec/discrete;
ods output ParameterEstimates=beta_vec;
run;

proc iml;
use &fall;
read all var {jun04 jul04 aug04 sep04 oct04 nov04 dec04 jan05 feb05 mar05 apr05 may05 jun05 jul05 aug05 sep05 oct05 nov05 dec05 jan06 feb06 mar06 apr06 may06 jun06 jul06 aug06 sep06 oct06 nov06 dec06 jan07 feb07 mar07 apr07 may07 jun07 jul07 aug07 sep07 oct07 nov07 dec07 jan08 feb08 mar08 apr08 may08 jun08 jul08 aug08 sep08 oct08 nov08 dec08 jan09 feb09 mar09 apr09 may09 jun09 jul09 aug09 sep09 oct09 nov09 dec09 jan10 feb10 mar10 apr10 may10 jun10 jul10 aug10 sep10 oct10 nov10 dec10 jan11 feb11 mar11 apr11 may11 jun11 jul11 aug11 sep11 oct11 nov11 dec11 jan12 feb12 mar12 apr12 may12 jun12 jul12 aug12 sep12 oct12 nov12 dec12 jan13 feb13 mar13 apr13 may13 jun13 jul13 aug13 sep13 oct13 nov13 dec13 jan14 feb14 mar14 apr14 may14 jun14 jul14 aug14 sep14 oct14 nov14 dec14 jan15 feb15 mar15 apr15 may15} into fall;
n_months=ncol(fall);
n_farms=nrow(fall);
use &winter;
read all var {jun04 jul04 aug04 sep04 oct04 nov04 dec04 jan05 feb05 mar05 apr05 may05 jun05 jul05 aug05 sep05 oct05 nov05 dec05 jan06 feb06 mar06 apr06 may06 jun06 jul06 aug06 sep06 oct06 nov06 dec06 jan07 feb07 mar07 apr07 may07 jun07 jul07 aug07 sep07 oct07 nov07 dec07 jan08 feb08 mar08 apr08 may08 jun08 jul08 aug08 sep08 oct08 nov08 dec08 jan09 feb09 mar09 apr09 may09 jun09 jul09 aug09 sep09 oct09 nov09 dec09 jan10 feb10 mar10 apr10 may10 jun10 jul10 aug10 sep10 oct10 nov10 dec10 jan11 feb11 mar11 apr11 may11 jun11 jul11 aug11 sep11 oct11 nov11 dec11 jan12 feb12 mar12 apr12 may12 jun12 jul12 aug12 sep12 oct12 nov12 dec12 jan13 feb13 mar13 apr13 may13 jun13 jul13 aug13 sep13 oct13 nov13 dec13 jan14 feb14 mar14 apr14 may14 jun14 jul14 aug14 sep14 oct14 nov14 dec14 jan15 feb15 mar15 apr15 may15} into winter;
n_months=ncol(winter);
n_farms=nrow(winter);
use &spring;
read all var {jun04 jul04 aug04 sep04 oct04 nov04 dec04 jan05 feb05 mar05 apr05 may05 jun05 jul05 aug05 sep05 oct05 nov05 dec05 jan06 feb06 mar06 apr06 may06 jun06 jul06 aug06 sep06 oct06 nov06 dec06 jan07 feb07 mar07 apr07 may07 jun07 jul07 aug07 sep07 oct07 nov07 dec07 jan08 feb08 mar08 apr08 may08 jun08 jul08 aug08 sep08 oct08 nov08 dec08 jan09 feb09 mar09 apr09 may09 jun09 jul09 aug09 sep09 oct09 nov09 dec09 jan10 feb10 mar10 apr10 may10 jun10 jul10 aug10 sep10 oct10 nov10 dec10 jan11 feb11 mar11 apr11 may11 jun11 jul11 aug11 sep11 oct11 nov11 dec11 jan12 feb12 mar12 apr12 may12 jun12 jul12 aug12 sep12 oct12 nov12 dec12 jan13 feb13 mar13 apr13 may13 jun13 jul13 aug13 sep13 oct13 nov13 dec13 jan14 feb14 mar14 apr14 may14 jun14 jul14 aug14 sep14 oct14 nov14 dec14 jan15 feb15 mar15 apr15 may15} into spring;
n_months=ncol(spring);
n_farms=nrow(spring);

use &prices;
read all var {jun04 jul04 aug04 sep04 oct04 nov04 dec04 jan05 feb05 mar05 apr05 may05 jun05 jul05 aug05 sep05 oct05 nov05 dec05 jan06 feb06 mar06 apr06 may06 jun06 jul06 aug06 sep06 oct06 nov06 dec06 jan07 feb07 mar07 apr07 may07 jun07 jul07 aug07 sep07 oct07 nov07 dec07 jan08 feb08 mar08 apr08 may08 jun08 jul08 aug08 sep08 oct08 nov08 dec08 jan09 feb09 mar09 apr09 may09 jun09 jul09 aug09 sep09 oct09 nov09 dec09 jan10 feb10 mar10 apr10 may10 jun10 jul10 aug10 sep10 oct10 nov10 dec10 jan11 feb11 mar11 apr11 may11 jun11 jul11 aug11 sep11 oct11 nov11 dec11 jan12 feb12 mar12 apr12 may12 jun12 jul12 aug12 sep12 oct12 nov12 dec12 jan13 feb13 mar13 apr13 may13 jun13 jul13 aug13 sep13 oct13 nov13 dec13 jan14 feb14 mar14 apr14 may14 jun14 jul14 aug14 sep14 oct14 nov14 dec14 jan15 feb15 mar15 apr15 may15} into price;
n_months=ncol(price);
n_farms=nrow(price);

use work.stoch_avc;
read all var {col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 col13 col14 col15 col16 col17 col18 col19 col20 col21 col22 col23 col24 col25 col26 col27 col28 col29 col30 col31 col32 col33 col34 col35 col36 col37 col38 col39 col40 col41 col42 col43 col44 col45 col46 col47 col48 col49 col50 col51 col52 col53 col54 col55 col56 col57 col58 col59 col60 col61 col62 col63 col64 col65 col66 col67 col68 col69 col70 col71 col72 col73 col74 col75 col76 col77 col78 col79 col80 col81 col82 col83 col84 col85 col86 col87 col88 col89 col90 col91 col92 col93 col94 col95 col96 col97 col98 col99 col100 col101 col102 col103 col104 col105 col106 col107 col108 col109 col110 col111 col112 col113 col114 col115 col116 col117 col118 col119 col120 col121 col122 col123 col124 col125 col126 col127 col128 col129 col130 col131 col132} into avc;
n_months=ncol(avc);
n_farms=nrow(avc);

use &output;
read all var {jun04 jul04 aug04 sep04 oct04 nov04 dec04 jan05 feb05 mar05 apr05 may05 jun05 jul05 aug05 sep05 oct05 nov05 dec05 jan06 feb06 mar06 apr06 may06 jun06 jul06 aug06 sep06 oct06 nov06 dec06 jan07 feb07 mar07 apr07 may07 jun07 jul07 aug07 sep07 oct07 nov07 dec07 jan08 feb08 mar08 apr08 may08 jun08 jul08 aug08 sep08 oct08 nov08 dec08 jan09 feb09 mar09 apr09 may09 jun09 jul09 aug09 sep09 oct09 nov09 dec09 jan10 feb10 mar10 apr10 may10 jun10 jul10 aug10 sep10 oct10 nov10 dec10 jan11 feb11 mar11 apr11 may11 jun11 jul11 aug11 sep11 oct11 nov11 dec11 jan12 feb12 mar12 apr12 may12 jun12 jul12 aug12 sep12 oct12 nov12 dec12 jan13 feb13 mar13 apr13 may13 jun13 jul13 aug13 sep13 oct13 nov13 dec13 jan14 feb14 mar14 apr14 may14 jun14 jul14 aug14 sep14 oct14 nov14 dec14 jan15 feb15 mar15 apr15 may15} into month_lb;
n_months=ncol(month_lb);
n_farms=nrow(month_lb);

use work.beta_vec;
read all var {estimate} into beta_vec;
n_var=nrow(beta_vec);
set1=ncol(beta_vec);

use work.exit_fence;
read all var {col1} into exit_fence;
obs1=nrow(exit_fence);
var1=ncol(exit_fence);
use work.exit_prob;
read all var {col1} into exit_prob;
obs1=nrow(exit_prob);
var1=ncol(exit_prob);

use work.stay_fence;
read all var {col1} into stay_fence;
obs1=nrow(stay_fence);
var1=ncol(stay_fence);
use work.stay_prob;
read all var {col1} into stay_prob;
obs1=nrow(stay_prob);
var1=ncol(stay_prob);

real_stay=j(n_farms,n_months,.);
real_exit=j(n_farms,n_months,.);
profits=j(n_farms,n_months,0);
outputs=j(n_farms,n_months,0);
month_prof=j(n_farms,n_months,0);
month_cwt=j(n_farms,n_months,0);
vprofpercwt=j(n_farms,n_months,0);
z_score=j(n_farms,n_months,.); /*probit y-value*/
probability=j(n_farms,n_months,.); /*associated probability of exit with probit z_score*/
forecast=j(n_farms,n_months,0); /*forecast for exit or produce*/

beta=t(beta_Vec);

call randseed(datetime());/*draws from uniform~[0,1] to compare to exit probability -> determines forecast*/
u=j(n_farms,n_months,.);
call randgen(u,"uniform");

*Forecast and Probability loop;
do i = 1 to n_farms;
do j= 7 to n_months;

month_cwt[i,j]=month_lb[i,j]/100;

if month_cwt[i,j]>0 then real_stay[i,j]=1;

if ((month_cwt[i,j]=0)*(month_cwt[i,j-1]^=0))=1 then real_exit[i,j]=1;
if ((month_cwt[i,j]=0)*(month_cwt[i,j-1]=0))=1 then real_exit[i,j]=.;

z_score[i,j]=(beta[1,1]+beta[1,2]*fall[i,j]+beta[1,3]*winter[i,j]+beta[1,4]*spring[i,j]+
beta[1,5]*price[i,j-1]+beta[1,6]*price[i,j-2]+beta[1,7]*price[i,j-3]+beta[1,8]*price[i,j-4]+beta[1,9]*price[i,j-5]+beta[1,10]*price[i,j-6]+
beta[1,12]*avc[i,j-1]+beta[1,13]*avc[i,j-2]+beta[1,14]*avc[i,j-3]+beta[1,14]*avc[i,j-4]+beta[1,15]*avc[i,j-5]+beta[1,16]*avc[i,j-6]);

if ((real_exit[i,j]=.)*(real_stay[i,j]=.))=1 then z_score[i,j]=.;

if z_score[i,j]^=. then probability[i,j] = cdf("normal",z_score[i,j]);

if ((vprofpercwt[i,j]>exit_fence[1,1])*(real_exit[i,j]=1))=1 then probability[i,j]=(1-exit_prob[1,1]);
if ((vprofpercwt[i,j]<=stay_fence[1,1])*(real_stay[i,j]=1))=1 then probability[i,j]=(1-stay_prob[1,1]);

if u[i,j]<probability[i,j] then forecast[i,j]=1;

if forecast[i,j-1]=1 then forecast[i,j]=.;
if ((forecast[i,j-1]=.)*((real_exit[i,j]=.)*(real_stay[i,j]=.)=0))=1 then forecast[i,j]=.;
if ((real_stay[i,j-1]=.)*(real_stay[i,j]=1))=1 then forecast[i,j]=(u[i,j]<probability[i,j]);
if ((real_exit[i,j]=.)*(real_stay[i,j]=.))=1 then forecast[i,j]=.;

end;
end;

do i =1 to n_farms;
do j=1 to 6;
forecast[i,j]=.;
end;
end;
print forecast;
forecast_produced=j(n_farms,n_months,.);

do i =1 to n_farms;
do j=7 to n_months;
if forecast[i,j]=0 then forecast_produced[i,j]=1;
end;
end;

months_exit_col=forecast[ ,+];
months_exit_row=t(months_exit_col);

create months_exit_row from months_exit_row ;
append from months_exit_row;

months_produced_col=forecast_produced[ ,+];
months_produced_row=t(months_produced_col);

create months_produced_row from months_produced_row ;
append from months_produced_row;

create temp_beta from beta;
append from beta;

create temp_exit_rule var {exit_fence exit_prob};
append var {exit_fence exit_prob};

create temp_stay_rule var {stay_fence stay_prob};
append var {stay_fence stay_prob};
quit;
%mend;

 

 

 


%macro simulate(output,avc,cutoff_stay,cutoff_exit,prices,spring,winter,fall);
ods html close;
proc iml;
use &output;
read all var {jun04 jul04 aug04 sep04 oct04 nov04 dec04 jan05 feb05 mar05 apr05 may05 jun05 jul05 aug05 sep05 oct05 nov05 dec05 jan06 feb06 mar06 apr06 may06 jun06 jul06 aug06 sep06 oct06 nov06 dec06 jan07 feb07 mar07 apr07 may07 jun07 jul07 aug07 sep07 oct07 nov07 dec07 jan08 feb08 mar08 apr08 may08 jun08 jul08 aug08 sep08 oct08 nov08 dec08 jan09 feb09 mar09 apr09 may09 jun09 jul09 aug09 sep09 oct09 nov09 dec09 jan10 feb10 mar10 apr10 may10 jun10 jul10 aug10 sep10 oct10 nov10 dec10 jan11 feb11 mar11 apr11 may11 jun11 jul11 aug11 sep11 oct11 nov11 dec11 jan12 feb12 mar12 apr12 may12 jun12 jul12 aug12 sep12 oct12 nov12 dec12 jan13 feb13 mar13 apr13 may13 jun13 jul13 aug13 sep13 oct13 nov13 dec13 jan14 feb14 mar14 apr14 may14 jun14 jul14 aug14 sep14 oct14 nov14 dec14 jan15 feb15 mar15 apr15 may15} into month_lb;
n_months=ncol(month_lb);
n_farms=nrow(month_lb);

real_stay=j(n_farms,n_months,.);
real_exit=j(n_farms,n_months,.);
*for each farm: count number of months produced in true deterministic setting;
do i=1 to n_farms;
do j=7 to n_months;

if month_lb[i,j]>0 then real_stay[i,j]=1;

if ((month_lb[i,j-1]>0)*(month_lb[i,j]=0))=1 then real_exit[i,j]=1;
if ((month_lb[i,j-1]=0)*(month_lb[i,j]=0))=1 then real_exit[i,j]=.;

end;
end;

true_months_produced1=real_stay[ ,+];
true_months_produced=t(true_months_produced1);
create true_months_produced from true_months_produced;
append from true_months_produced;

true_months_exit1=real_exit[ ,+];
true_months_exit=t(true_months_exit1);
create true_months_exit from true_months_exit;
append from true_months_exit;
quit;

proc iml;
use temp_stay_rule;
read all var {stay_fence stay_prob} into stay_rule;
row1=nrow(stay_rule);
col1=ncol(stay_rule);
use temp_exit_rule;
read all var {exit_fence exit_prob} into exit_rule;
row1=nrow(exit_rule);
col1=ncol(exit_rule);
create ntrials_stay_rule var {stay_fence stay_prob};
create ntrials_exit_rule var {exit_fence exit_prob};
create ntrials_beta var {col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 col13 col14 col15 col16};
create ntrials_months_produced var {col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 col13 col14 col15 col16 col17 col18 col19 col20 col21 col22 col23 col24 col25 col26 col27 col28 col29 col30 col31 col32 col33 col34 col35 col36 col37 col38 col39 col40 col41 col42 col43 col44 col45 col46 col47 col48 col49 col50 col51 col52 col53 col54 col55 col56 col57 col58 col59 col60 col61 col62 col63 col64 col65 col66 col67 col68 col69 col70 col71 col72 col73 col74 col75 col76 col77 col78 col79 col80 col81 col82 col83 col84 col85 col86 col87 col88 col89 col90 col91 col92 col93 col94 col95 col96 col97 col98 col99 col100 col101 col102 col103 col104 col105 col106 col107 col108 col109 col110 col111 col112 col113 col114 col115 col116 col117 col118 col119 col120 col121 col122 col123 col124 col125 col126 col127 col128 col129 col130 col131 col132 col133 col134 col135 col136 col137 col138 col139 col140 col141 col142 col143 col144 col145 col146 col147 col148 col149 col150 col151 col152 col153 col154 col155 col156 col157 col158 col159 col160 col161 col162 col163 col164 col165 col166 col167 col168 col169 col170 col171 col172 col173 col174 col175 col176 col177 col178 col179 col180 col181 col182 col183 col184 col185 col186 col187 col188 col189 col190 col191 col192 col193 col194 col195 col196 col197 col198 col199 col200 col201 col202 col203 col204 col205 col206 col207 col208 col209 col210 col211 col212 col213 col214 col215 col216 col217 col218 col219 col220 col221 col222 col223 col224 col225 col226 col227 col228 col229 col230 col231 col232 col233 col234 col235 col236 col237 col238 col239 col240 col241 col242 col243 col244 col245 col246 col247 col248 col249 col250 col251 col252 col253 col254 col255 col256 col257 col258 col259 col260 col261 col262 col263 col264 col265 col266 col267 col268 col269 col270 col271 col272 col273 col274 col275 col276 col277 col278 col279 col280 col281 col282 col283 col284 col285 col286 col287 col288 col289 col290 col291 col292 col293 col294 col295 col296 col297 col298 col299 col300 col301 col302 col303 col304 col305 col306 col307 col308 col309 col310 col311 col312 col313 col314 col315 col316 col317 col318 col319 col320 col321 col322 col323 col324 col325 col326 col327 col328 col329 col330 col331 col332 col333 col334 col335 col336 col337 col338 col339 col340 col341 col342 col343 col344 col345 col346 col347 col348 col349 col350 col351 col352 col353 col354 col355 col356 col357 col358 col359 col360 col361 col362 col363 col364 col365 col366 col367 col368 col369 col370 col371 col372 col373 col374 col375 col376 col377 col378 col379 col380 col381 col382 col383 col384 col385 col386 col387 col388 col389 col390 col391 col392 col393 col394 col395 col396 col397 col398 col399 col400 col401 col402 col403 col404 col405 col406 col407 col408 col409 col410 col411 col412 col413 col414 col415 col416 col417 col418 col419 col420 col421 col422 col423 col424 col425 col426 col427 col428 col429 col430 col431 col432 col433 col434 col435 col436 col437 col438 col439 col440 col441 col442 col443 col444 col445 col446 col447 col448 col449 col450 col451 col452 col453 col454 col455 col456 col457 col458 col459 col460 col461 col462 col463 col464 col465 col466 col467 col468 col469 col470 col471 col472 col473 col474 col475 col476 col477 col478 col479 col480 col481 col482 col483 col484};
create ntrials_months_exit var {col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 col13 col14 col15 col16 col17 col18 col19 col20 col21 col22 col23 col24 col25 col26 col27 col28 col29 col30 col31 col32 col33 col34 col35 col36 col37 col38 col39 col40 col41 col42 col43 col44 col45 col46 col47 col48 col49 col50 col51 col52 col53 col54 col55 col56 col57 col58 col59 col60 col61 col62 col63 col64 col65 col66 col67 col68 col69 col70 col71 col72 col73 col74 col75 col76 col77 col78 col79 col80 col81 col82 col83 col84 col85 col86 col87 col88 col89 col90 col91 col92 col93 col94 col95 col96 col97 col98 col99 col100 col101 col102 col103 col104 col105 col106 col107 col108 col109 col110 col111 col112 col113 col114 col115 col116 col117 col118 col119 col120 col121 col122 col123 col124 col125 col126 col127 col128 col129 col130 col131 col132 col133 col134 col135 col136 col137 col138 col139 col140 col141 col142 col143 col144 col145 col146 col147 col148 col149 col150 col151 col152 col153 col154 col155 col156 col157 col158 col159 col160 col161 col162 col163 col164 col165 col166 col167 col168 col169 col170 col171 col172 col173 col174 col175 col176 col177 col178 col179 col180 col181 col182 col183 col184 col185 col186 col187 col188 col189 col190 col191 col192 col193 col194 col195 col196 col197 col198 col199 col200 col201 col202 col203 col204 col205 col206 col207 col208 col209 col210 col211 col212 col213 col214 col215 col216 col217 col218 col219 col220 col221 col222 col223 col224 col225 col226 col227 col228 col229 col230 col231 col232 col233 col234 col235 col236 col237 col238 col239 col240 col241 col242 col243 col244 col245 col246 col247 col248 col249 col250 col251 col252 col253 col254 col255 col256 col257 col258 col259 col260 col261 col262 col263 col264 col265 col266 col267 col268 col269 col270 col271 col272 col273 col274 col275 col276 col277 col278 col279 col280 col281 col282 col283 col284 col285 col286 col287 col288 col289 col290 col291 col292 col293 col294 col295 col296 col297 col298 col299 col300 col301 col302 col303 col304 col305 col306 col307 col308 col309 col310 col311 col312 col313 col314 col315 col316 col317 col318 col319 col320 col321 col322 col323 col324 col325 col326 col327 col328 col329 col330 col331 col332 col333 col334 col335 col336 col337 col338 col339 col340 col341 col342 col343 col344 col345 col346 col347 col348 col349 col350 col351 col352 col353 col354 col355 col356 col357 col358 col359 col360 col361 col362 col363 col364 col365 col366 col367 col368 col369 col370 col371 col372 col373 col374 col375 col376 col377 col378 col379 col380 col381 col382 col383 col384 col385 col386 col387 col388 col389 col390 col391 col392 col393 col394 col395 col396 col397 col398 col399 col400 col401 col402 col403 col404 col405 col406 col407 col408 col409 col410 col411 col412 col413 col414 col415 col416 col417 col418 col419 col420 col421 col422 col423 col424 col425 col426 col427 col428 col429 col430 col431 col432 col433 col434 col435 col436 col437 col438 col439 col440 col441 col442 col443 col444 col445 col446 col447 col448 col449 col450 col451 col452 col453 col454 col455 col456 col457 col458 col459 col460 col461 col462 col463 col464 col465 col466 col467 col468 col469 col470 col471 col472 col473 col474 col475 col476 col477 col478 col479 col480 col481 col482 col483 col484};

%do simulation_ID=1 %to 100;
%iterate(&output,&avc,&cutoff_stay,&cutoff_exit,&prices,&spring,&winter,&fall);
proc append base=ntrials_stay_rule data=temp_stay_rule;run;
proc append base=ntrials_exit_rule data=temp_exit_rule;run;
proc append base=ntrials_beta data=temp_beta;run;
proc append base=ntrials_months_produced data=months_produced_row;run;
proc append base=ntrials_months_exit data=months_exit_row;run;
%end;




proc iml;
use ntrials_stay_rule;
read all var {stay_fence stay_prob} into stay_rule;
samplesize=nrow(stay_rule);
var2=ncol(stay_rule);
use ntrials_exit_rule;
read all var {exit_fence exit_prob} into exit_rule;
samplesize=nrow(exit_rule);
var2=ncol(exit_rule);
use ntrials_beta;
read all var {col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 col13 col14 col15 col16} into beta;
samplesize=nrow(beta);
var16=ncol(beta);
use ntrials_months_produced;
read all var {col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 col13 col14 col15 col16 col17 col18 col19 col20 col21 col22 col23 col24 col25 col26 col27 col28 col29 col30 col31 col32 col33 col34 col35 col36 col37 col38 col39 col40 col41 col42 col43 col44 col45 col46 col47 col48 col49 col50 col51 col52 col53 col54 col55 col56 col57 col58 col59 col60 col61 col62 col63 col64 col65 col66 col67 col68 col69 col70 col71 col72 col73 col74 col75 col76 col77 col78 col79 col80 col81 col82 col83 col84 col85 col86 col87 col88 col89 col90 col91 col92 col93 col94 col95 col96 col97 col98 col99 col100 col101 col102 col103 col104 col105 col106 col107 col108 col109 col110 col111 col112 col113 col114 col115 col116 col117 col118 col119 col120 col121 col122 col123 col124 col125 col126 col127 col128 col129 col130 col131 col132 col133 col134 col135 col136 col137 col138 col139 col140 col141 col142 col143 col144 col145 col146 col147 col148 col149 col150 col151 col152 col153 col154 col155 col156 col157 col158 col159 col160 col161 col162 col163 col164 col165 col166 col167 col168 col169 col170 col171 col172 col173 col174 col175 col176 col177 col178 col179 col180 col181 col182 col183 col184 col185 col186 col187 col188 col189 col190 col191 col192 col193 col194 col195 col196 col197 col198 col199 col200 col201 col202 col203 col204 col205 col206 col207 col208 col209 col210 col211 col212 col213 col214 col215 col216 col217 col218 col219 col220 col221 col222 col223 col224 col225 col226 col227 col228 col229 col230 col231 col232 col233 col234 col235 col236 col237 col238 col239 col240 col241 col242 col243 col244 col245 col246 col247 col248 col249 col250 col251 col252 col253 col254 col255 col256 col257 col258 col259 col260 col261 col262 col263 col264 col265 col266 col267 col268 col269 col270 col271 col272 col273 col274 col275 col276 col277 col278 col279 col280 col281 col282 col283 col284 col285 col286 col287 col288 col289 col290 col291 col292 col293 col294 col295 col296 col297 col298 col299 col300 col301 col302 col303 col304 col305 col306 col307 col308 col309 col310 col311 col312 col313 col314 col315 col316 col317 col318 col319 col320 col321 col322 col323 col324 col325 col326 col327 col328 col329 col330 col331 col332 col333 col334 col335 col336 col337 col338 col339 col340 col341 col342 col343 col344 col345 col346 col347 col348 col349 col350 col351 col352 col353 col354 col355 col356 col357 col358 col359 col360 col361 col362 col363 col364 col365 col366 col367 col368 col369 col370 col371 col372 col373 col374 col375 col376 col377 col378 col379 col380 col381 col382 col383 col384 col385 col386 col387 col388 col389 col390 col391 col392 col393 col394 col395 col396 col397 col398 col399 col400 col401 col402 col403 col404 col405 col406 col407 col408 col409 col410 col411 col412 col413 col414 col415 col416 col417 col418 col419 col420 col421 col422 col423 col424 col425 col426 col427 col428 col429 col430 col431 col432 col433 col434 col435 col436 col437 col438 col439 col440 col441 col442 col443 col444 col445 col446 col447 col448 col449 col450 col451 col452 col453 col454 col455 col456 col457 col458 col459 col460 col461 col462 col463 col464 col465 col466 col467 col468 col469 col470 col471 col472 col473 col474 col475 col476 col477 col478 col479 col480 col481 col482 col483 col484}into months_produce;
samplesize=nrow(months_produce);
n_farms=ncol(months_produce);
use ntrials_months_exit;
read all var {col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 col13 col14 col15 col16 col17 col18 col19 col20 col21 col22 col23 col24 col25 col26 col27 col28 col29 col30 col31 col32 col33 col34 col35 col36 col37 col38 col39 col40 col41 col42 col43 col44 col45 col46 col47 col48 col49 col50 col51 col52 col53 col54 col55 col56 col57 col58 col59 col60 col61 col62 col63 col64 col65 col66 col67 col68 col69 col70 col71 col72 col73 col74 col75 col76 col77 col78 col79 col80 col81 col82 col83 col84 col85 col86 col87 col88 col89 col90 col91 col92 col93 col94 col95 col96 col97 col98 col99 col100 col101 col102 col103 col104 col105 col106 col107 col108 col109 col110 col111 col112 col113 col114 col115 col116 col117 col118 col119 col120 col121 col122 col123 col124 col125 col126 col127 col128 col129 col130 col131 col132 col133 col134 col135 col136 col137 col138 col139 col140 col141 col142 col143 col144 col145 col146 col147 col148 col149 col150 col151 col152 col153 col154 col155 col156 col157 col158 col159 col160 col161 col162 col163 col164 col165 col166 col167 col168 col169 col170 col171 col172 col173 col174 col175 col176 col177 col178 col179 col180 col181 col182 col183 col184 col185 col186 col187 col188 col189 col190 col191 col192 col193 col194 col195 col196 col197 col198 col199 col200 col201 col202 col203 col204 col205 col206 col207 col208 col209 col210 col211 col212 col213 col214 col215 col216 col217 col218 col219 col220 col221 col222 col223 col224 col225 col226 col227 col228 col229 col230 col231 col232 col233 col234 col235 col236 col237 col238 col239 col240 col241 col242 col243 col244 col245 col246 col247 col248 col249 col250 col251 col252 col253 col254 col255 col256 col257 col258 col259 col260 col261 col262 col263 col264 col265 col266 col267 col268 col269 col270 col271 col272 col273 col274 col275 col276 col277 col278 col279 col280 col281 col282 col283 col284 col285 col286 col287 col288 col289 col290 col291 col292 col293 col294 col295 col296 col297 col298 col299 col300 col301 col302 col303 col304 col305 col306 col307 col308 col309 col310 col311 col312 col313 col314 col315 col316 col317 col318 col319 col320 col321 col322 col323 col324 col325 col326 col327 col328 col329 col330 col331 col332 col333 col334 col335 col336 col337 col338 col339 col340 col341 col342 col343 col344 col345 col346 col347 col348 col349 col350 col351 col352 col353 col354 col355 col356 col357 col358 col359 col360 col361 col362 col363 col364 col365 col366 col367 col368 col369 col370 col371 col372 col373 col374 col375 col376 col377 col378 col379 col380 col381 col382 col383 col384 col385 col386 col387 col388 col389 col390 col391 col392 col393 col394 col395 col396 col397 col398 col399 col400 col401 col402 col403 col404 col405 col406 col407 col408 col409 col410 col411 col412 col413 col414 col415 col416 col417 col418 col419 col420 col421 col422 col423 col424 col425 col426 col427 col428 col429 col430 col431 col432 col433 col434 col435 col436 col437 col438 col439 col440 col441 col442 col443 col444 col445 col446 col447 col448 col449 col450 col451 col452 col453 col454 col455 col456 col457 col458 col459 col460 col461 col462 col463 col464 col465 col466 col467 col468 col469 col470 col471 col472 col473 col474 col475 col476 col477 col478 col479 col480 col481 col482 col483 col484} into months_exit;
samplesize=nrow(months_exit);
n_farms=ncol(months_exit);

avg_stay_rule=stay_rule[:, ];
create avg_stay_rule from avg_stay_rule;
append from avg_stay_rule;

avg_exit_rule=exit_rule[:, ];
create avg_exit_rule from avg_exit_rule;
append from avg_exit_rule;

avg_beta=beta[:, ];
create avg_beta from avg_beta;
append from avg_beta;

avg_months_exit=months_exit[:, ];
create avg_months_exit from avg_months_exit;
append from avg_months_exit;

avg_months_produce=months_produce[:, ];
create avg_months_produce from avg_months_produce;
append from avg_months_produce;

quit;

%mend;
%simulate(output,avc,cutoff_stay,cutoff_exit,prices_notier,spring,winter,fall);

proc print data=ntrials_months_produced;run;
proc print data=ntrials_months_exit;run;
proc print data=ntrials_exit_rule;run;
proc print data=ntrials_stay_rule;run;
proc print data=ntrials_beta;run;
proc iml;
use avg_beta;
read all var {col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 col13 col14 col15 col16} into beta;
row1=nrow(beta);
var16=ncol(beta);
use avg_exit_rule;
read all var {col1 col2} into exit_rule;
row1=nrow(exit_rule);
col2=ncol(exit_rule);
use avg_stay_rule;
read all var {col1 col2} into stay_rule;
row1=nrow(stay_rule);
col2=ncol(stay_rule);
use avg_months_produce;
read all var {col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 col13 col14 col15 col16 col17 col18 col19 col20 col21 col22 col23 col24 col25 col26 col27 col28 col29 col30 col31 col32 col33 col34 col35 col36 col37 col38 col39 col40 col41 col42 col43 col44 col45 col46 col47 col48 col49 col50 col51 col52 col53 col54 col55 col56 col57 col58 col59 col60 col61 col62 col63 col64 col65 col66 col67 col68 col69 col70 col71 col72 col73 col74 col75 col76 col77 col78 col79 col80 col81 col82 col83 col84 col85 col86 col87 col88 col89 col90 col91 col92 col93 col94 col95 col96 col97 col98 col99 col100 col101 col102 col103 col104 col105 col106 col107 col108 col109 col110 col111 col112 col113 col114 col115 col116 col117 col118 col119 col120 col121 col122 col123 col124 col125 col126 col127 col128 col129 col130 col131 col132 col133 col134 col135 col136 col137 col138 col139 col140 col141 col142 col143 col144 col145 col146 col147 col148 col149 col150 col151 col152 col153 col154 col155 col156 col157 col158 col159 col160 col161 col162 col163 col164 col165 col166 col167 col168 col169 col170 col171 col172 col173 col174 col175 col176 col177 col178 col179 col180 col181 col182 col183 col184 col185 col186 col187 col188 col189 col190 col191 col192 col193 col194 col195 col196 col197 col198 col199 col200 col201 col202 col203 col204 col205 col206 col207 col208 col209 col210 col211 col212 col213 col214 col215 col216 col217 col218 col219 col220 col221 col222 col223 col224 col225 col226 col227 col228 col229 col230 col231 col232 col233 col234 col235 col236 col237 col238 col239 col240 col241 col242 col243 col244 col245 col246 col247 col248 col249 col250 col251 col252 col253 col254 col255 col256 col257 col258 col259 col260 col261 col262 col263 col264 col265 col266 col267 col268 col269 col270 col271 col272 col273 col274 col275 col276 col277 col278 col279 col280 col281 col282 col283 col284 col285 col286 col287 col288 col289 col290 col291 col292 col293 col294 col295 col296 col297 col298 col299 col300 col301 col302 col303 col304 col305 col306 col307 col308 col309 col310 col311 col312 col313 col314 col315 col316 col317 col318 col319 col320 col321 col322 col323 col324 col325 col326 col327 col328 col329 col330 col331 col332 col333 col334 col335 col336 col337 col338 col339 col340 col341 col342 col343 col344 col345 col346 col347 col348 col349 col350 col351 col352 col353 col354 col355 col356 col357 col358 col359 col360 col361 col362 col363 col364 col365 col366 col367 col368 col369 col370 col371 col372 col373 col374 col375 col376 col377 col378 col379 col380 col381 col382 col383 col384 col385 col386 col387 col388 col389 col390 col391 col392 col393 col394 col395 col396 col397 col398 col399 col400 col401 col402 col403 col404 col405 col406 col407 col408 col409 col410 col411 col412 col413 col414 col415 col416 col417 col418 col419 col420 col421 col422 col423 col424 col425 col426 col427 col428 col429 col430 col431 col432 col433 col434 col435 col436 col437 col438 col439 col440 col441 col442 col443 col444 col445 col446 col447 col448 col449 col450 col451 col452 col453 col454 col455 col456 col457 col458 col459 col460 col461 col462 col463 col464 col465 col466 col467 col468 col469 col470 col471 col472 col473 col474 col475 col476 col477 col478 col479 col480 col481 col482 col483 col484}into months_produce;
row1=nrow(months_produce);
n_farms=ncol(months_produce);
use avg_months_exit;
read all var {col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 col13 col14 col15 col16 col17 col18 col19 col20 col21 col22 col23 col24 col25 col26 col27 col28 col29 col30 col31 col32 col33 col34 col35 col36 col37 col38 col39 col40 col41 col42 col43 col44 col45 col46 col47 col48 col49 col50 col51 col52 col53 col54 col55 col56 col57 col58 col59 col60 col61 col62 col63 col64 col65 col66 col67 col68 col69 col70 col71 col72 col73 col74 col75 col76 col77 col78 col79 col80 col81 col82 col83 col84 col85 col86 col87 col88 col89 col90 col91 col92 col93 col94 col95 col96 col97 col98 col99 col100 col101 col102 col103 col104 col105 col106 col107 col108 col109 col110 col111 col112 col113 col114 col115 col116 col117 col118 col119 col120 col121 col122 col123 col124 col125 col126 col127 col128 col129 col130 col131 col132 col133 col134 col135 col136 col137 col138 col139 col140 col141 col142 col143 col144 col145 col146 col147 col148 col149 col150 col151 col152 col153 col154 col155 col156 col157 col158 col159 col160 col161 col162 col163 col164 col165 col166 col167 col168 col169 col170 col171 col172 col173 col174 col175 col176 col177 col178 col179 col180 col181 col182 col183 col184 col185 col186 col187 col188 col189 col190 col191 col192 col193 col194 col195 col196 col197 col198 col199 col200 col201 col202 col203 col204 col205 col206 col207 col208 col209 col210 col211 col212 col213 col214 col215 col216 col217 col218 col219 col220 col221 col222 col223 col224 col225 col226 col227 col228 col229 col230 col231 col232 col233 col234 col235 col236 col237 col238 col239 col240 col241 col242 col243 col244 col245 col246 col247 col248 col249 col250 col251 col252 col253 col254 col255 col256 col257 col258 col259 col260 col261 col262 col263 col264 col265 col266 col267 col268 col269 col270 col271 col272 col273 col274 col275 col276 col277 col278 col279 col280 col281 col282 col283 col284 col285 col286 col287 col288 col289 col290 col291 col292 col293 col294 col295 col296 col297 col298 col299 col300 col301 col302 col303 col304 col305 col306 col307 col308 col309 col310 col311 col312 col313 col314 col315 col316 col317 col318 col319 col320 col321 col322 col323 col324 col325 col326 col327 col328 col329 col330 col331 col332 col333 col334 col335 col336 col337 col338 col339 col340 col341 col342 col343 col344 col345 col346 col347 col348 col349 col350 col351 col352 col353 col354 col355 col356 col357 col358 col359 col360 col361 col362 col363 col364 col365 col366 col367 col368 col369 col370 col371 col372 col373 col374 col375 col376 col377 col378 col379 col380 col381 col382 col383 col384 col385 col386 col387 col388 col389 col390 col391 col392 col393 col394 col395 col396 col397 col398 col399 col400 col401 col402 col403 col404 col405 col406 col407 col408 col409 col410 col411 col412 col413 col414 col415 col416 col417 col418 col419 col420 col421 col422 col423 col424 col425 col426 col427 col428 col429 col430 col431 col432 col433 col434 col435 col436 col437 col438 col439 col440 col441 col442 col443 col444 col445 col446 col447 col448 col449 col450 col451 col452 col453 col454 col455 col456 col457 col458 col459 col460 col461 col462 col463 col464 col465 col466 col467 col468 col469 col470 col471 col472 col473 col474 col475 col476 col477 col478 col479 col480 col481 col482 col483 col484} into months_exit;
row1=nrow(months_exit);
n_farms=ncol(months_exit);

print months_produce;
print months_exit;
print beta;
print exit_rule;
print stay_rule;

quit;

SAS Super FREQ
Posts: 3,618

Re: Long simulation code generates "Error: An exception has been encountered"

Unfortunately, the error message you describe does not provide any useful information. Are there any other notes, warnings, or errors in the SAS log?

 

Trying to understand and debug hundreds of lines of macro code is a daunting task. The best I can offer is a few general suggestions about how to reorganize your code:

1. Try to replace your macros with calls to SAS/IML functions.It looks like you only have a few arguments, mostly data set names and cutoff values.

2. You don't need to use a macro variable for the data set name. The language supports expressions for specifying data set names at run time., such as USE (dsname);

3. You can store the long lists of variable names in a character matrix, such as

varNames = {jun04 jul04 ... apr15 may15};

read all var varNames into month_lb;

4. Similarly, use

outNames = "col1":"col484";

create ntrials_months_produced var outNames;

5. The reason your program is "running for many hours" is because

   A. You have not vectorized the IML portion of the program, and

   B.  you are using a macro loops like this:

%do simulation_ID=1 %to 100;
   %iterate(&output,&avc,&cutoff_stay,&cutoff_exit,&prices,&spring,&winter,&fall);
   proc append base=ntrials_stay_rule data=temp_stay_rule;run;
   ...
%end;

See the article "Simulation in SAS: The slow way or the BY way" to understand why the macro loops are inefficient.  As you've discovered, it is also difficult to debug macro code, which is why you should try to rewrite the whole program as a single IML program that calls functions.

Ask a Question
Discussion stats
  • 1 reply
  • 287 views
  • 6 likes
  • 2 in conversation