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!
@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.
Please post the complete log of that step. Use the </> button to post it.
And include a check for your nvars:
%put &=nvars.;
Just before the step that fails, so we can see how the arrays should be defined, and which element the incriminated code line tries to retrieve.
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.
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.
@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.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.