Here is my code:
proc iml;
use work.prof;
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_prof;
n_months=ncol(month_prof);
n_farms=nrow(month_prof);
use work.outputs;
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);
month_cwt = j(n_farms,n_months,0);
profpercwt = j(n_farms,n_months,.);
Theory_exit= j(n_farms,n_months,0);
real_exit= j(n_farms,n_months,.);
Theory_stay= j(n_farms,n_months,0);
real_stay= j(n_farms,n_months,.);
exit_accuracy = j(n_farms,n_months,.);
stay_accuracy = j(n_farms,n_months,.);
do i=1 to n_farms;
do j=2 to n_months;
month_cwt[i,j] = month_lb[i,j]/100;
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 ((month_prof[i,j-1]<&shutdown_prof))=1 then theory_exit[i,j]=1;
if real_exit[i,j]=. then theory_exit[i,j]=.;
if month_cwt[i,j]>0 then real_stay[i,j]=1;
if ((month_prof[i,j-1]<&shutdown_prof))=0 then theory_stay[i,j]=1;
if real_stay[i,j]=. then theory_stay[i,j]=.;
if real_exit[i,j]^=. then exit_accuracy[i,j]=(real_exit[i,j]=theory_exit[i,j]);
if real_stay[i,j]^=. then stay_accuracy[i,j]=(real_stay[i,j]=theory_stay[i,j]);
end;
end;
stay=stay_accuracy[:];
exit=exit_accuracy[:];
print stay exit;
quit;
Any insight into fixing the error is greatly appreciated. Thanks!
Here is the log:
You read in month_prof from the PROF data set, which apparently has 484 observations. However, the n_farms variable, which is the upper limit of the "DO I" loop, is based on the number of observations in the OUTPUTS data set, which is apparently greater than 484. (I can't tell how many obs because you are using the NONOTES option.)
In short, the program loops DO i = 1 TO n_farms and accesses the i_th row of month_prof. As soon as i exceeds 484, you get the "invalid subscript or subscript out of range" error.
You read in month_prof from the PROF data set, which apparently has 484 observations. However, the n_farms variable, which is the upper limit of the "DO I" loop, is based on the number of observations in the OUTPUTS data set, which is apparently greater than 484. (I can't tell how many obs because you are using the NONOTES option.)
In short, the program loops DO i = 1 TO n_farms and accesses the i_th row of month_prof. As soon as i exceeds 484, you get the "invalid subscript or subscript out of range" error.
Thanks Rick!
That was exactly the problem, I used Proc Print to look more closely at 'outputs' to find there were 5 extra observations for each variable that were all blank. If this is the case, is this error a problem? i.e. will my results be affected?
Yes, errors are always a problem that should be investigated and fixed. The best thing to do is figure out why those five blank observations are there.
If you fail at that task, you could truncate the month_lb matrix so that it is always the same size as month_prof:
month_lb = month_lb[1:nrow(month_prof), ];
However, you'll feel better if you find out why the data sets are not the same size.
Got it! Thanks again, Rick!
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.