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-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!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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