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

Hi all,

 

I am struggling to append values from each iteration of a %do loop. When I use a CREATE statement inside the %do loop, it re-creates the dataset in each iteration and overwrites the values from the previous iteration. When I use it outside the loop, the append statement doesn't recognize which dataset it should append to. The append and create statements are bolded in the code below. I appreciate any input!

 

Here is my code:

 

 

%macro debug(output,avc,prices,cutoff);
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());
u=j(n_farms,n_months,.);
mu=0;
sigma= 4.0233929;
call randgen(u,"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(21,2,.);
stay_output=j(21,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]+u[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 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 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[:];
exit=exit_acc[:];
print exit stay "&cutoff";
r=&cutoff+11;

*creates outlier fence for exit;
exit=exit_acc[:];
exit_output[r,1]=exit;
exit_output[r,2]=&cutoff;
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_fence=exit_rule[1,1];
exit_outlier_percent=exit_rule[1,2];

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

if stay_output[r,1]>0 then stay_rule[1,1]=stay_output[r,1];
stay_rule[1,2]=stay_output[r,2];
stay_outlier_fence=stay_rule[1,1];
stay_outlier_percent=stay_rule[1,2];
append base=temp var {stay_outlier_fence stay_outlier_percent};

print exit_output stay_output;
print exit_rule stay_rule;
quit;
%mend;

 


%macro iterate(output,avc,prices,cutoff);
proc iml;
create temp;
%do &cutoff=-10 %to 10;
%debug(&output,&avc,&prices,&cutoff);
%end;
quit;
%mend;

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Your IML syntax is not right. Should look like :

 


proc iml;
create temp var {stay_outlier_fence stay_outlier_percent};

append;

close;

 

View solution in original post

2 REPLIES 2
Astounding
PROC Star

As a macro programmer (not an IML programmer), I can tell you that this statement is highly suspect:

 

%do &cutoff = -10 %to 10;

 

The usual form would be:

 

%do cutoff = -10 %to 10;

 

That's the correct way to say the macro variable &CUTOFF should iterate from -10 to 10.  Your %do loop, with the extra &, says that the value of &CUTOFF contains the name of a macro variable.  That macro variable should iterate from -10 to 10.

Ksharp
Super User

Your IML syntax is not right. Should look like :

 


proc iml;
create temp var {stay_outlier_fence stay_outlier_percent};

append;

close;

 

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!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 2 replies
  • 665 views
  • 1 like
  • 3 in conversation