BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ballardw
Super User

@Cruise wrote:

 

Question2:    

Array code gave error:

ERROR: Array subscript out of range at line 223 column 4.

What am I doing wrong here?

 

data have; 
input id bf1 bf2 bf3 bf4 bf5 bf6 bf7 bf8 bf9 bf10 bf11 bf12 ever; 
datalines;
1	1	1	1	1	1	1	1	1	1	1	1	1	1
2	1	1	1	1	0	0	0	0	0	0	0	0	1
3	1	1	0	0	0	0	0	0	0	0	0	0	1
4	1	0	0	0	0	0	0	0	0	0	0	0	1
5	0	0	0	0	0	0	0	0	0	0	0	0	0
6	1	1	1	1	1	1	1	0	0	0	0	0	1
;

 

 


data test; set have; array x (12) BF_1-BF_12; do i=1 to 12; if x(1-12)=1 then bfcat=121; else bfcat=120; if x(1-6)=1 then bfcat=61; else bfcat=60; if x(1-3)=1 then bfcat=31; else bfcat=30; if x(1)=1 then bfcat=11; else bfcat=10; end; if bf_ever=1 then bfcat=1; else bfcat=0; run;

 


 

The array subscript out of bound comes from each of the statements like this:

if x(1-12)=1 then bfcat=121;

The values inside the parentheses are used in a calculation to find the subscript of the array. Since 1-12 = -11 that is invalid for an array the way you defined x. Similarly 1-6 and 1-3 generate negative values and invalid subscripts of -5 and -2.

 

The calculation in the array subscript is common as often we do an offset such as x(i+1) to reference an element of the array but would need to make sure that the calculation resolves to something inside the defined subscripts for an array.

Here is a brief example of an array that allows negative subscripts:

data example;
   array x (-10:-5) ;
   do i= -10 to -5;
      x(i)= i;
   end;
   drop i;
run;

 

Notice the names of the variables created by the array. Arrays defined with a lower: upper bound are useful for things that have known integer behavior but don't conveniently start at 1 such as year values.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 15 replies
  • 5295 views
  • 6 likes
  • 5 in conversation