Statistical programming, matrix languages, and more

error: subscript out of range

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

error: subscript out of range

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:

 

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
55
56 proc iml;
NOTE: IML Ready
57 use work.prof;
58 read all var {jun04jul04
58 ! aug04sep04oct04nov04dec04jan05feb05mar05apr05may05jun05jul05aug05sep05oct05nov05dec05jan06feb06mar06a
58 ! pr06may06jun06jul06aug06sep06oct06nov06dec06jan07feb07mar07apr07may07jun07jul07aug07sep07oct07nov07de
58 ! c07jan08feb08mar08apr08may08jun08jul08aug08sep08oct08nov08dec08jan09feb09mar09apr09may09jun09jul09aug
58 ! 09sep09oct09nov09dec09jan10feb10mar10apr10may10jun10jul10aug10sep10oct10nov10dec10jan11feb11mar11apr1
58 ! 1may11jun11jul11aug11sep11oct11nov11dec11jan12feb12mar12apr12may12jun12jul12aug12sep12oct12nov12dec12
58 ! jan13feb13mar13apr13may13jun13jul13aug13sep13oct13nov13dec13jan14feb14mar14apr14may14jun14jul14aug14
58 ! sep14oct14nov14dec14jan15feb15mar15apr15may15} into month_prof;
59 n_months=ncol(month_prof);
60 n_farms=nrow(month_prof);
61 use work.outputs;
62 read all var {jun04jul04
62 ! aug04sep04oct04nov04dec04jan05feb05mar05apr05may05jun05jul05aug05sep05oct05nov05dec05jan06feb06mar06a
62 ! pr06may06jun06jul06aug06sep06oct06nov06dec06jan07feb07mar07apr07may07jun07jul07aug07sep07oct07nov07de
62 ! c07jan08feb08mar08apr08may08jun08jul08aug08sep08oct08nov08dec08jan09feb09mar09apr09may09jun09jul09aug
62 ! 09sep09oct09nov09dec09jan10feb10mar10apr10may10jun10jul10aug10sep10oct10nov10dec10jan11feb11mar11apr1
62 ! 1may11jun11jul11aug11sep11oct11nov11dec11jan12feb12mar12apr12may12jun12jul12aug12sep12oct12nov12dec12
62 ! jan13feb13mar13apr13may13jun13jul13aug13sep13oct13nov13dec13jan14feb14mar14apr14may14jun14jul14aug14
62 ! sep14oct14nov14dec14jan15feb15mar15apr15may15} into month_lb;
63 n_months=ncol(month_lb);
64 n_farms=nrow(month_lb);
65
66 month_cwt = j(n_farms,n_months,0);
67 profpercwt = j(n_farms,n_months,.);
68
69 Theory_exit= j(n_farms,n_months,0);
70 real_exit= j(n_farms,n_months,.);
71
72 Theory_stay= j(n_farms,n_months,0);
73 real_stay= j(n_farms,n_months,.);
74
75 exit_accuracy = j(n_farms,n_months,.);
76 stay_accuracy = j(n_farms,n_months,.);
77
78 do i=1 to n_farms;
78 !
79
79 ! do j=2 to n_months;
80
81
81 ! month_cwt[i,j] = month_lb[i,j]/100;
82
83
83 ! if ((month_cwt[i,j]=0)*(month_cwt[i,j-1]^=0))=1 then real_exit[i,j]=1;
83 !
84
84 ! if ((month_cwt[i,j]=0)*(month_cwt[i,j-1]=0))=1 then real_exit[i,j]=.;
85
86
86 ! if (month_prof[i,j-1]<0)=1 then theory_exit[i,j]=1;
87
87 ! if real_exit[i,j]=. then theory_exit[i,j]=.;
88
89
89 ! if month_cwt[i,j]>0 then real_stay[i,j]=1;
90
91
91 ! if (month_prof[i,j-1]<0)=0 then theory_stay[i,j]=1;
92
92 ! if real_stay[i,j]=. then theory_stay[i,j]=.;
93
94
94 ! if real_exit[i,j]^=. then exit_accuracy[i,j]=(real_exit[i,j]=theory_exit[i,j]);
95
95 ! if real_stay[i,j]^=. then stay_accuracy[i,j]=(real_stay[i,j]=theory_stay[i,j]);
96
96 ! end;
97 end;
ERROR: (execution) Invalid subscript or subscript out of range.
 
operation : [ at line 86 column 16
operands : month_prof, i, _TEM1001
month_prof 484 rows 132 cols (numeric)
 
i 1 row 1 col (numeric)
 
485
 
_TEM1001 1 row 1 col (numeric)
 
1
 
statement : IF at line 86 column 2
98 stay=stay_accuracy[:];
99 exit=exit_accuracy[:];
100 print stay exit;
101 quit;
NOTE: Exiting IML.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE IML used (Total process time):
real time 0.50 seconds
cpu time 0.49 seconds
 
102
103 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
115

Accepted Solutions
Solution
‎05-03-2016 01:16 PM
SAS Super FREQ
Posts: 3,630

Re: error: subscript out of range

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.

View solution in original post


All Replies
Solution
‎05-03-2016 01:16 PM
SAS Super FREQ
Posts: 3,630

Re: error: subscript out of range

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.

Occasional Contributor
Posts: 13

Re: error: subscript out of range

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?

 

 

SAS Super FREQ
Posts: 3,630

Re: error: subscript out of range

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.

Occasional Contributor
Posts: 13

Re: error: subscript out of range

Got it! Thanks again, Rick!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 444 views
  • 0 likes
  • 2 in conversation