Help using Base SAS procedures

Accepted Solution

Data preparation for a line chart and using array index in different range

Reply
Highlighted
Super User
Posts: 13,066

Re: Data preparation for a line chart and using array index in different range


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.

☑ This topic is solved.

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

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