<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Error: Array Subscript Out of Range in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Error-Array-Subscript-Out-of-Range/m-p/738960#M230576</link>
    <description>&lt;P&gt;There you have it:&lt;/P&gt;
&lt;PRE&gt; 101        %put &amp;amp;=n_vars.;
 N_VARS=0
&lt;/PRE&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
    <pubDate>Tue, 04 May 2021 15:33:57 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2021-05-04T15:33:57Z</dc:date>
    <item>
      <title>Error: Array Subscript Out of Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-Array-Subscript-Out-of-Range/m-p/738920#M230557</link>
      <description>&lt;P&gt;Hi SAS Communities,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a follow-up question to a question I posted a few days ago. See here:&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Calculate-Duration-First-Visit-to-Time-to-Event/m-p/738489#M230369" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Programming/Calculate-Duration-First-Visit-to-Time-to-Event/m-p/738489#M230369&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code below results in an &lt;STRONG&gt;array subscript out of range error:&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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&amp;gt;=1 then
do;
duration_in_days=_visit[n_event1]-_visit[1];
end;

if missing(duration_in_days) then do;
array _r_visit {*} visit_date_&amp;amp;n_vars - visit_date_1;
array _r_event {*} recovered_&amp;amp;n_vars - recovered_1;
n_event_last0=whichn(0 ,of _r_event[*]);
if n_event_last0&amp;gt;=1 then
do;
duration_in_days=_r_visit[n_event_last0]-_r_visit[&amp;amp;n_vars];
end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;The error is specifically associated with this line of code (see log below):&lt;/STRONG&gt;&amp;nbsp;duration_in_days=_r_visit[n_event_last0]-_r_visit[&amp;amp;n_vars];&lt;/P&gt;
&lt;PRE&gt; 95         if missing(duration_in_days) then do;
 96         array _r_visit {*} visit_date_&amp;amp;n_vars - visit_date_1;
 97         array _r_event {*} recovered_&amp;amp;n_vars - recovered_1;
 98         n_event_last0=whichn(0 ,of _r_event[*]);
 99         if n_event_last0&amp;gt;=1 then
 100        do;
 101        %put &amp;amp;=n_vars.;
 N_VARS=0
 102        duration_in_days=_r_visit[n_event_last0]-_r_visit[&amp;amp;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.&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does anyone know why this error is occurring and how I can fix it? Thank you in advance!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 May 2021 15:40:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-Array-Subscript-Out-of-Range/m-p/738920#M230557</guid>
      <dc:creator>mvhoya</dc:creator>
      <dc:date>2021-05-04T15:40:42Z</dc:date>
    </item>
    <item>
      <title>Re: Error: Array Subscript Out of Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-Array-Subscript-Out-of-Range/m-p/738930#M230560</link>
      <description>&lt;P&gt;Please post the complete log of that step. Use the &amp;lt;/&amp;gt; button to post it.&lt;/P&gt;</description>
      <pubDate>Tue, 04 May 2021 15:02:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-Array-Subscript-Out-of-Range/m-p/738930#M230560</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-04T15:02:10Z</dc:date>
    </item>
    <item>
      <title>Re: Error: Array Subscript Out of Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-Array-Subscript-Out-of-Range/m-p/738932#M230561</link>
      <description>Thank you for the suggestion. I went ahead and updated my post.</description>
      <pubDate>Tue, 04 May 2021 15:07:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-Array-Subscript-Out-of-Range/m-p/738932#M230561</guid>
      <dc:creator>mvhoya</dc:creator>
      <dc:date>2021-05-04T15:07:02Z</dc:date>
    </item>
    <item>
      <title>Re: Error: Array Subscript Out of Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-Array-Subscript-Out-of-Range/m-p/738936#M230564</link>
      <description>&lt;P&gt;When I make up code that creates the ERROR, the SAS log includes a listing of all the variable values:&lt;/P&gt;
&lt;PRE&gt; 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
&lt;/PRE&gt;
&lt;P&gt;Please show us that listing.&lt;/P&gt;</description>
      <pubDate>Tue, 04 May 2021 15:10:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-Array-Subscript-Out-of-Range/m-p/738936#M230564</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-04T15:10:29Z</dc:date>
    </item>
    <item>
      <title>Re: Error: Array Subscript Out of Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-Array-Subscript-Out-of-Range/m-p/738941#M230565</link>
      <description>&lt;P&gt;And include a check for your nvars:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%put &amp;amp;=nvars.;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 May 2021 15:14:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-Array-Subscript-Out-of-Range/m-p/738941#M230565</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-04T15:14:22Z</dc:date>
    </item>
    <item>
      <title>Re: Error: Array Subscript Out of Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-Array-Subscript-Out-of-Range/m-p/738949#M230568</link>
      <description>Where in my code do I include this line?</description>
      <pubDate>Tue, 04 May 2021 15:23:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-Array-Subscript-Out-of-Range/m-p/738949#M230568</guid>
      <dc:creator>mvhoya</dc:creator>
      <dc:date>2021-05-04T15:23:22Z</dc:date>
    </item>
    <item>
      <title>Re: Error: Array Subscript Out of Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-Array-Subscript-Out-of-Range/m-p/738952#M230571</link>
      <description>&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Tue, 04 May 2021 15:26:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-Array-Subscript-Out-of-Range/m-p/738952#M230571</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-04T15:26:48Z</dc:date>
    </item>
    <item>
      <title>Re: Error: Array Subscript Out of Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-Array-Subscript-Out-of-Range/m-p/738956#M230574</link>
      <description>That portion of the LOG is quite long, but I went ahead an updated my post to include it.</description>
      <pubDate>Tue, 04 May 2021 15:30:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-Array-Subscript-Out-of-Range/m-p/738956#M230574</guid>
      <dc:creator>mvhoya</dc:creator>
      <dc:date>2021-05-04T15:30:48Z</dc:date>
    </item>
    <item>
      <title>Re: Error: Array Subscript Out of Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-Array-Subscript-Out-of-Range/m-p/738960#M230576</link>
      <description>&lt;P&gt;There you have it:&lt;/P&gt;
&lt;PRE&gt; 101        %put &amp;amp;=n_vars.;
 N_VARS=0
&lt;/PRE&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Tue, 04 May 2021 15:33:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-Array-Subscript-Out-of-Range/m-p/738960#M230576</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-04T15:33:57Z</dc:date>
    </item>
    <item>
      <title>Re: Error: Array Subscript Out of Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-Array-Subscript-Out-of-Range/m-p/738961#M230577</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/344766"&gt;@mvhoya&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi SAS Communities,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a follow-up question to a question I posted a few days ago. See here:&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Calculate-Duration-First-Visit-to-Time-to-Event/m-p/738489#M230369" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Programming/Calculate-Duration-First-Visit-to-Time-to-Event/m-p/738489#M230369&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code below results in an &lt;STRONG&gt;array subscript out of range error:&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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&amp;gt;=1 then
do;
duration_in_days=_visit[n_event1]-_visit[1];
end;

if missing(duration_in_days) then do;
array _r_visit {*} visit_date_&amp;amp;n_vars - visit_date_1;
array _r_event {*} recovered_&amp;amp;n_vars - recovered_1;
n_event_last0=whichn(0 ,of _r_event[*]);
if n_event_last0&amp;gt;=1 then
do;
duration_in_days=_r_visit[n_event_last0]-_r_visit[&amp;amp;n_vars];
end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;The error is specifically associated with this line of code (see log below):&lt;/STRONG&gt;&amp;nbsp;duration_in_days=_r_visit[n_event_last0]-_r_visit[&amp;amp;n_vars];&lt;/P&gt;
&lt;PRE&gt; 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&amp;gt;=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_&amp;amp;n_vars - visit_date_1;
 95         array _r_event {*} recovered_&amp;amp;n_vars - recovered_1;
 96         n_event_last0=whichn(0 ,of _r_event[*]);
 97         if n_event_last0&amp;gt;=1 then
 98         do;
 99         duration_in_days=_r_visit[n_event_last0]-_r_visit[&amp;amp;n_vars];
 100        end;
 101        end;
 102        run;
 
 ERROR: Array subscript out of range at line 99 column 42.&lt;/PRE&gt;
&lt;P&gt;Does anyone know why this error is occurring and how I can fix it? Thank you in advance!&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I suspect there is a possible misunderstanding of which element of your array _r_visit you are pointing to when you use _r_visit[&amp;amp;nvars].&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Tue, 04 May 2021 15:34:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-Array-Subscript-Out-of-Range/m-p/738961#M230577</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-05-04T15:34:30Z</dc:date>
    </item>
    <item>
      <title>Re: Error: Array Subscript Out of Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-Array-Subscript-Out-of-Range/m-p/738971#M230582</link>
      <description>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!</description>
      <pubDate>Tue, 04 May 2021 15:42:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-Array-Subscript-Out-of-Range/m-p/738971#M230582</guid>
      <dc:creator>mvhoya</dc:creator>
      <dc:date>2021-05-04T15:42:15Z</dc:date>
    </item>
  </channel>
</rss>

