DATA Step, Macro, Functions and more

Array error "ERROR: Array subscript out of range at line/column. "

Reply
Occasional Contributor
Posts: 14

Array error "ERROR: Array subscript out of range at line/column. "

Two code problems that I was hoping to get some further insight:

(1) Trying my hand at arrays and I continue to get the an error code = "Error: Array subscript out of range at line X column Y". I am trying to merge (I) questionnaire and (ii) medication data (both longitudinal, sorted by participant study_id). There are a maximum of 8 interviewdates per participant, but tens of medications possible corresponding to a given interview date.

(2) I also want to "carry forward" a variable (mednaive) but am unsure on how to do this. In this particular variable, a participant can be on a medication at one questionnaire (mednaive=0) and then off at another (mednaive=1). However, I want to capture "ever exposed" to the medication such that, if an individual is not on a medication at a given questionnaire but was in the past, then they will ALWAYS be mednaive=0 (even if they are mednaive=1 at this particular questionnaire).

data qSummary ;

  set dataset ;

  by study_id ; /*sorted by study_id*/

  retain qCount qdate1-qdate11 ; / *variables created for interview dates and number of completed questionnaires*/

  mednaive=. ; /*new variable to identify those naive vs those experienced to a particular medication class*/

     if ((drug_class='X') or (drug_class='Y') or (drug_class='Z')) then mednaive=0 ; /*on medication at current questionnaires*/

     else mednaive=1 ; /*no on medication at current questionnaire*/

     format mednaive mednaive. ; /*NEWVAR needed to address "carry forward" concept noted above -- unsure of how to do this*/

  array qdate {*} qdate1-qdate11  ;

  format qdate1-qdate11 date9. ;

     if first.study_id then do ; /*if first questionnaire per person*/

       qCount=1 ; /*count of questionnaire (max=11 questionnaires)*/

       qdate1=interviewdt ; /*the first questionnaire date is equal to the first interviewdate variable already defined in the dataset*/

       do i=2 to 11 ;

          qdate{i}=. ;

       end ;

     end ;

     else do ; /*if not first questionnaire per person*/

          qcount=sum(qcount,1) ;

          qdate{qcount}=interviewdt; /* <-- ERROR: ARRAY SUBSCRIPT OUT OF RANGE AT (THIS) LINE COLUMN X*/

     end ;

     if last.study_id then output ;

run ;

Super User
Super User
Posts: 7,997

Re: Array error "ERROR: Array subscript out of range at line/column. "

Hi,

Its usually a good idea to post some test data in a datastep and what the output should look like.

For your Error, what this is saying is that the value in {} exceeds the maximum number of elements in the array.  So qdate has 11 elements, perhaps your code loop gets to qdate{12} - this would be outside the bounds of 1 - 11. 

As for the other part, its easier to see with test data/ output.

Super User
Posts: 11,343

Re: Array error "ERROR: Array subscript out of range at line/column. "

If you use a put statement after the statement:

qcount=sum(qcount,1);

put qcount= ;

I bet that you will find that qcount is greater than 11. Cause is hard to determine without actual data.

To get better help you may need to provide some example data of the pertinent variables and the desired outcome.

Occasional Contributor
Posts: 14

Re: Array error "ERROR: Array subscript out of range at line/column. "

I actually found the error in my code seconds after posting this -- by using <put qcount=;> -- funny how that happens (after spending HOURS trying to figure out my error!). I had 13 dates (only for 2 individuals out of 1800...)

As for the example data:

where study_id = the individuals unique identified

onMeds=on medication at the time of the interview (this doesn't exist in my dataset, but it is easier to understand my problem with this variable), 0=yes/1=no

interviewdt=interviewdt

Variables wanted:

*medNaive*=on medication at current interview (medNaive=onMeds), 0=yes/1=no

*medEver*=if EVER on meds, even if not at current interview, 0=never/1=ever 

Study_id      onMeds     interviewdt                    *medNaive*          *medEver*

1                    1               01jan2007                          1                         1             

1                    0              01jan2008                           0                         1

1                    0               01jan2009                          0                         1

2                    0               02feb2008                         0                         0

2                    1               02feb2009                         1                         1

2                    0               02feb2010                         0                         1

3                    0               03mar2008                         0                         0

3                    0               03mar2009                         0                         0

3                    1               03mar2010                         1                         1

Ask a Question
Discussion stats
  • 3 replies
  • 5567 views
  • 3 likes
  • 3 in conversation