BookmarkSubscribeRSS Feed
toneill
Calcite | Level 5

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 ;

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

ballardw
Super User

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.

toneill
Calcite | Level 5

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

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 3 replies
  • 12674 views
  • 3 likes
  • 3 in conversation