BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mvhoya
Obsidian | Level 7

Hi SAS Communities,

 

This is a follow-up question to a question I posted a few days ago. See here: https://communities.sas.com/t5/SAS-Programming/Calculate-Duration-First-Visit-to-Time-to-Event/m-p/7...

 

The code has been modified to provide me with one duration variable using two separate conditions. One condition pulls in the first instance of an event '1' and calculates the duration and the second condition pulls in the last instance of an event '0' and calculates the duration. 

 

The code below results in an array subscript out of range error:

proc sql noprint;
select count(*) into :n_vars trimmed
from dictionary.columns
where libname='WORK' and memname='HAVE'
and upcase(name) like 'EVENT^_%' escape '^'
;
quit;

data duration;
length duration_in_days 8;
set table1;

array _visit {*} visit_date_:;
array _event {*} recovered_:;
n_event1=whichn(1,of _event[*]);
if n_event1>=1 then
do;
duration_in_days=_visit[n_event1]-_visit[1];
end;

if missing(duration_in_days) then do;
array _r_visit {*} visit_date_&n_vars - visit_date_1;
array _r_event {*} recovered_&n_vars - recovered_1;
n_event_last0=whichn(0 ,of _r_event[*]);
if n_event_last0>=1 then
do;
duration_in_days=_r_visit[n_event_last0]-_r_visit[&n_vars];
end;
end;
run;

The error is specifically associated with this line of code (see log below): duration_in_days=_r_visit[n_event_last0]-_r_visit[&n_vars];

 95         if missing(duration_in_days) then do;
 96         array _r_visit {*} visit_date_&n_vars - visit_date_1;
 97         array _r_event {*} recovered_&n_vars - recovered_1;
 98         n_event_last0=whichn(0 ,of _r_event[*]);
 99         if n_event_last0>=1 then
 100        do;
 101        %put &=n_vars.;
 N_VARS=0
 102        duration_in_days=_r_visit[n_event_last0]-_r_visit[&n_vars];
 103        end;
 104        end;
 105        run;
 
 ERROR: Array subscript out of range at line 102 column 42.
 
 NOTE: The SAS System stopped processing this step because of errors.
 NOTE: There were 1 observations read from the data set WORK.TABLE1.

 

Does anyone know why this error is occurring and how I can fix it? Thank you in advance! 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@mvhoya wrote:

Hi SAS Communities,

 

This is a follow-up question to a question I posted a few days ago. See here: https://communities.sas.com/t5/SAS-Programming/Calculate-Duration-First-Visit-to-Time-to-Event/m-p/7...

 

The code has been modified to provide me with one duration variable using two separate conditions. One condition pulls in the first instance of an event '1' and calculates the duration and the second condition pulls in the last instance of an event '0' and calculates the duration. 

 

The code below results in an array subscript out of range error:

proc sql noprint;
select count(*) into :n_vars trimmed
from dictionary.columns
where libname='WORK' and memname='HAVE'
and upcase(name) like 'EVENT^_%' escape '^'
;
quit;

data duration;
length duration_in_days 8;
set table1;

array _visit {*} visit_date_:;
array _event {*} recovered_:;
n_event1=whichn(1,of _event[*]);
if n_event1>=1 then
do;
duration_in_days=_visit[n_event1]-_visit[1];
end;

if missing(duration_in_days) then do;
array _r_visit {*} visit_date_&n_vars - visit_date_1;
array _r_event {*} recovered_&n_vars - recovered_1;
n_event_last0=whichn(0 ,of _r_event[*]);
if n_event_last0>=1 then
do;
duration_in_days=_r_visit[n_event_last0]-_r_visit[&n_vars];
end;
end;
run;

The error is specifically associated with this line of code (see log below): duration_in_days=_r_visit[n_event_last0]-_r_visit[&n_vars];

 80         
 81         data duration;
 82         length duration_in_days 8;
 83         set table1;
 84         
 85         array _visit {*} visit_date_:;
 86         array _event {*} recovered_:;
 87         n_event1=whichn(1,of _event[*]);
 88         if n_event1>=1 then
 89         do;
 90         duration_in_days=_visit[n_event1]-_visit[1];
 91         end;
 92         
 93         if missing(duration_in_days) then do;
 94         array _r_visit {*} visit_date_&n_vars - visit_date_1;
 95         array _r_event {*} recovered_&n_vars - recovered_1;
 96         n_event_last0=whichn(0 ,of _r_event[*]);
 97         if n_event_last0>=1 then
 98         do;
 99         duration_in_days=_r_visit[n_event_last0]-_r_visit[&n_vars];
 100        end;
 101        end;
 102        run;
 
 ERROR: Array subscript out of range at line 99 column 42.

Does anyone know why this error is occurring and how I can fix it? Thank you in advance! 


I suspect there is a possible misunderstanding of which element of your array _r_visit you are pointing to when you use _r_visit[&nvars].

 

You are getting the macro variable NVARS from a data set named HAVE but then your code uses TABLE1 on the SET statement. Unless HAVE and TABLE are proper relations there could be an issue since it is counting variables that start with EVENT but none of the variables used in the array seem to match that. So there is big chance of a disconnect between what Nvars represents and your Table1 data set. Time to look at variable names in BOTH of those data sets.

View solution in original post

10 REPLIES 10
mvhoya
Obsidian | Level 7
Thank you for the suggestion. I went ahead and updated my post.
mvhoya
Obsidian | Level 7
Where in my code do I include this line?
Kurt_Bremser
Super User

When I make up code that creates the ERROR, the SAS log includes a listing of all the variable values:

 73         data test;
 74         array test{*} test1-test9;
 75         n = 10;
 76         x = test{n};
 77         run;
 
 ERROR: Array subscript out of range at Zeile 76 Spalte 5.
 test1=. test2=. test3=. test4=. test5=. test6=. test7=. test8=. test9=. n=10 x=. _ERROR_=1 _N_=1
 NOTE: The SAS System stopped processing this step because of errors.
 WARNING: The data set WORK.TEST may be incomplete.  When this step was stopped there were 0 observations and 11 variables.
 WARNING: Datei WORK.TEST wurde nicht ersetzt, da da dieser Schritt angehalten wurde.
 NOTE:  Verwendet wurde: DATA statement - (Gesamtverarbeitungszeit):
       real time           0.00 seconds
       cpu time            0.01 seconds

Please show us that listing.

mvhoya
Obsidian | Level 7
That portion of the LOG is quite long, but I went ahead an updated my post to include it.
Kurt_Bremser
Super User

There you have it:

 101        %put &=n_vars.;
 N_VARS=0

your SQL does not find any variable names that fit the pattern. Since SAS defines arrays with a starting index of 1 (unless you direct it otherwise), trying to retrieve the 0th element will fail.

ballardw
Super User

@mvhoya wrote:

Hi SAS Communities,

 

This is a follow-up question to a question I posted a few days ago. See here: https://communities.sas.com/t5/SAS-Programming/Calculate-Duration-First-Visit-to-Time-to-Event/m-p/7...

 

The code has been modified to provide me with one duration variable using two separate conditions. One condition pulls in the first instance of an event '1' and calculates the duration and the second condition pulls in the last instance of an event '0' and calculates the duration. 

 

The code below results in an array subscript out of range error:

proc sql noprint;
select count(*) into :n_vars trimmed
from dictionary.columns
where libname='WORK' and memname='HAVE'
and upcase(name) like 'EVENT^_%' escape '^'
;
quit;

data duration;
length duration_in_days 8;
set table1;

array _visit {*} visit_date_:;
array _event {*} recovered_:;
n_event1=whichn(1,of _event[*]);
if n_event1>=1 then
do;
duration_in_days=_visit[n_event1]-_visit[1];
end;

if missing(duration_in_days) then do;
array _r_visit {*} visit_date_&n_vars - visit_date_1;
array _r_event {*} recovered_&n_vars - recovered_1;
n_event_last0=whichn(0 ,of _r_event[*]);
if n_event_last0>=1 then
do;
duration_in_days=_r_visit[n_event_last0]-_r_visit[&n_vars];
end;
end;
run;

The error is specifically associated with this line of code (see log below): duration_in_days=_r_visit[n_event_last0]-_r_visit[&n_vars];

 80         
 81         data duration;
 82         length duration_in_days 8;
 83         set table1;
 84         
 85         array _visit {*} visit_date_:;
 86         array _event {*} recovered_:;
 87         n_event1=whichn(1,of _event[*]);
 88         if n_event1>=1 then
 89         do;
 90         duration_in_days=_visit[n_event1]-_visit[1];
 91         end;
 92         
 93         if missing(duration_in_days) then do;
 94         array _r_visit {*} visit_date_&n_vars - visit_date_1;
 95         array _r_event {*} recovered_&n_vars - recovered_1;
 96         n_event_last0=whichn(0 ,of _r_event[*]);
 97         if n_event_last0>=1 then
 98         do;
 99         duration_in_days=_r_visit[n_event_last0]-_r_visit[&n_vars];
 100        end;
 101        end;
 102        run;
 
 ERROR: Array subscript out of range at line 99 column 42.

Does anyone know why this error is occurring and how I can fix it? Thank you in advance! 


I suspect there is a possible misunderstanding of which element of your array _r_visit you are pointing to when you use _r_visit[&nvars].

 

You are getting the macro variable NVARS from a data set named HAVE but then your code uses TABLE1 on the SET statement. Unless HAVE and TABLE are proper relations there could be an issue since it is counting variables that start with EVENT but none of the variables used in the array seem to match that. So there is big chance of a disconnect between what Nvars represents and your Table1 data set. Time to look at variable names in BOTH of those data sets.

mvhoya
Obsidian | Level 7
You're correct, that's exactly what was happening. I fixed the dataset names and incorrect variable names and the code ran perfectly. Thank you for your help!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 10 replies
  • 5264 views
  • 6 likes
  • 3 in conversation