<?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 array subscript out of range in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/array-subscript-out-of-range/m-p/461633#M117462</link>
    <description>&lt;P&gt;Hi users,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using an array to impute the average&amp;nbsp; value of the next two non-missing variables in a 5 column sorted by year dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am getting the error "ERROR: Array subscript out of range at line 107 column 21" .&amp;nbsp; My code looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data oud2;&lt;BR /&gt;set oud1;&lt;/P&gt;&lt;P&gt;Array oud oud2011-oud2015;&lt;BR /&gt;Array imp imp2011-imp2015;&lt;/P&gt;&lt;P&gt;retain oud;&lt;BR /&gt;retain imp;&lt;BR /&gt;&lt;BR /&gt;do i=1 to 5;&lt;BR /&gt;if 0&amp;lt; oud(i)&amp;gt;5 then oud{i}=.;&lt;BR /&gt;if 0&amp;lt; imp(i)&amp;gt;5 then imp(i)=.;&lt;BR /&gt;&lt;BR /&gt;If missing(oud(i))&lt;BR /&gt;and not missing (oud(i+1))&lt;BR /&gt;and not missing(oud(i+2)) then do;&lt;/P&gt;&lt;P&gt;imp(i) =(oud(i+1)+oud(i+2))/2;&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;&lt;BR /&gt;drop i;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am looking for insight into how to streamlilne this process and set the bounds properly - thank you in advance!&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 11 May 2018 17:36:24 GMT</pubDate>
    <dc:creator>eliz1</dc:creator>
    <dc:date>2018-05-11T17:36:24Z</dc:date>
    <item>
      <title>array subscript out of range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/array-subscript-out-of-range/m-p/461633#M117462</link>
      <description>&lt;P&gt;Hi users,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using an array to impute the average&amp;nbsp; value of the next two non-missing variables in a 5 column sorted by year dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am getting the error "ERROR: Array subscript out of range at line 107 column 21" .&amp;nbsp; My code looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data oud2;&lt;BR /&gt;set oud1;&lt;/P&gt;&lt;P&gt;Array oud oud2011-oud2015;&lt;BR /&gt;Array imp imp2011-imp2015;&lt;/P&gt;&lt;P&gt;retain oud;&lt;BR /&gt;retain imp;&lt;BR /&gt;&lt;BR /&gt;do i=1 to 5;&lt;BR /&gt;if 0&amp;lt; oud(i)&amp;gt;5 then oud{i}=.;&lt;BR /&gt;if 0&amp;lt; imp(i)&amp;gt;5 then imp(i)=.;&lt;BR /&gt;&lt;BR /&gt;If missing(oud(i))&lt;BR /&gt;and not missing (oud(i+1))&lt;BR /&gt;and not missing(oud(i+2)) then do;&lt;/P&gt;&lt;P&gt;imp(i) =(oud(i+1)+oud(i+2))/2;&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;&lt;BR /&gt;drop i;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am looking for insight into how to streamlilne this process and set the bounds properly - thank you in advance!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 May 2018 17:36:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/array-subscript-out-of-range/m-p/461633#M117462</guid>
      <dc:creator>eliz1</dc:creator>
      <dc:date>2018-05-11T17:36:24Z</dc:date>
    </item>
    <item>
      <title>Re: array subscript out of range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/array-subscript-out-of-range/m-p/461637#M117463</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;and not missing (oud(i+1))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;When i is 5, this looks for the sixth element of the array, which doesn't exist. so you can't do this.&lt;/P&gt;</description>
      <pubDate>Fri, 11 May 2018 17:43:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/array-subscript-out-of-range/m-p/461637#M117463</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-05-11T17:43:08Z</dc:date>
    </item>
    <item>
      <title>Re: array subscript out of range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/array-subscript-out-of-range/m-p/461643#M117465</link>
      <description>&lt;P&gt;Thanks! intended the if then statement to set the value to missing once i exceeded 5.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 May 2018 17:51:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/array-subscript-out-of-range/m-p/461643#M117465</guid>
      <dc:creator>eliz1</dc:creator>
      <dc:date>2018-05-11T17:51:57Z</dc:date>
    </item>
    <item>
      <title>Re: array subscript out of range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/array-subscript-out-of-range/m-p/461644#M117466</link>
      <description>&lt;P&gt;The crash actually happens here&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;and not missing(oud(i+2)) then do;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;when i is 4 and you're looking for a non-existent 6th element of your array.&lt;/P&gt;
&lt;P&gt;You need to find logic for the end-cases of your algorithm. How should your calculation for imp2014 and imp2015 look like?&lt;/P&gt;</description>
      <pubDate>Fri, 11 May 2018 17:52:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/array-subscript-out-of-range/m-p/461644#M117466</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-05-11T17:52:03Z</dc:date>
    </item>
    <item>
      <title>Re: array subscript out of range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/array-subscript-out-of-range/m-p/461647#M117468</link>
      <description>&lt;P&gt;If you want missings, then the loop can only go 3 (not 5) and then you won't get the error, and imp(i) (for i=1,2,3) will be assigned a value. imp(i) for i=4,5 doesn't exist and will be missing.&lt;/P&gt;</description>
      <pubDate>Fri, 11 May 2018 17:57:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/array-subscript-out-of-range/m-p/461647#M117468</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-05-11T17:57:59Z</dc:date>
    </item>
    <item>
      <title>Re: array subscript out of range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/array-subscript-out-of-range/m-p/461651#M117471</link>
      <description>&lt;P&gt;ok, thanks. Was hoping there was a way to prevent the i from getting to 6 but can set it to 3 and create separate loop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Appreciate your feedback!&lt;/P&gt;</description>
      <pubDate>Fri, 11 May 2018 18:09:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/array-subscript-out-of-range/m-p/461651#M117471</guid>
      <dc:creator>eliz1</dc:creator>
      <dc:date>2018-05-11T18:09:10Z</dc:date>
    </item>
    <item>
      <title>Re: array subscript out of range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/array-subscript-out-of-range/m-p/461755#M117494</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/157224"&gt;@eliz1&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;We can't know how your logic must look like to do what you're after and you haven't really told us. Based on the code posted and if it's simply&amp;nbsp;about avoiding the out of range condition, adding a simple check could do. Something like: &lt;STRONG&gt;If i+2&amp;lt;=dim(oud)&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data oud2;
  set oud1;
  Array oud oud2011-oud2015;
  Array imp imp2011-imp2015;
  retain oud;
  retain imp;

  do i=1 to 5;
    if 0&amp;lt; oud(i)&amp;gt;5 then
      oud{i}=.;

    if 0&amp;lt; imp(i)&amp;gt;5 then
      imp(i)=.;

    If i+2&amp;lt;=dim(oud) 
      and missing(oud(i))
      and not missing (oud(i+1))
      and not missing(oud(i+2)) then
      do;
        imp(i) =(oud(i+1)+oud(i+2))/2;
      end;
  end;

  drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Oh, and this condition in your code looks not right.&lt;/P&gt;
&lt;PRE&gt;if 0&amp;lt; oud(i) &amp;gt;5 the oud{i}=.;&lt;/PRE&gt;
&lt;P&gt;Can you describe in words when oud(I) should get set to missing? Your current logic is the same like: if oud(I)&amp;gt;5;&lt;/P&gt;</description>
      <pubDate>Sat, 12 May 2018 03:55:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/array-subscript-out-of-range/m-p/461755#M117494</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-05-12T03:55:57Z</dc:date>
    </item>
    <item>
      <title>Re: array subscript out of range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/array-subscript-out-of-range/m-p/461756#M117495</link>
      <description>Thanks Patrick,&lt;BR /&gt;&lt;BR /&gt;MY goal is to impute missing values in a table with five columns each&lt;BR /&gt;representing a year - the intent is to substitute either the average of the&lt;BR /&gt;previous and last, or of the next two years. in the case of the first and&lt;BR /&gt;last year the criteria changes to moving only forward or backward.&lt;BR /&gt;&lt;BR /&gt;I made it work using do loops with separate code for the first, middle and&lt;BR /&gt;last years - your stop idea may be the way to avoid this, I'll give it a&lt;BR /&gt;try, thank you!&lt;BR /&gt;</description>
      <pubDate>Sat, 12 May 2018 04:06:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/array-subscript-out-of-range/m-p/461756#M117495</guid>
      <dc:creator>eliz1</dc:creator>
      <dc:date>2018-05-12T04:06:37Z</dc:date>
    </item>
    <item>
      <title>Re: array subscript out of range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/array-subscript-out-of-range/m-p/461758#M117496</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/157224"&gt;@eliz1&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Just as an idea: If you always select two array elements for your calculation and it's only about selecting the right ones based on the value of &lt;EM&gt;I&lt;/EM&gt; then using an informat could make things quite simple.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have(keep=invar_:);
  array invar_ {5} 8.;
  do obs=1 to 3;
    do val=1 to 5;
      invar_[val]=obs*val;
      if ceil(ranuni(1)*4)=1 then call missing(invar_[val]);
    end;
    output;
  end;
run;

proc format;
  invalue cycle
    0=3
    6=3
  ;
run;
data want;
  set have;
  array invars  {*} invar_:;
  array outvars {*} 8. outvar_1 - outvar_5;
  do i=1 to dim(invars);
    outvars[i]=mean(invars[input(i-1,cycle.)],invars[input(i+1,cycle.)]);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 12 May 2018 05:23:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/array-subscript-out-of-range/m-p/461758#M117496</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-05-12T05:23:08Z</dc:date>
    </item>
    <item>
      <title>Re: array subscript out of range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/array-subscript-out-of-range/m-p/461759#M117497</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/157224"&gt;@eliz1&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Or actually: You can use the same variable multiple times in an array definition and though you could just add the variables you need for the first and the last iteration of the do loop to the array. Something like below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have(keep=invar_:);
  array invar_ {5} 8.;
  do obs=1 to 3;
    do val=1 to 5;
      invar_[val]=obs*val;
      if ceil(ranuni(1)*4)=1 then call missing(invar_[val]);
    end;
    output;
  end;
run;


data want;
  set have;
  array invars  {0:6} invar_3 invar_: invar_3;
  array outvars {*} 8. outvar_1 - outvar_5;
  do i=1 to dim(invars)-2;
    outvars[i]=mean(invars[i-1],invars[i+1]);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 12 May 2018 05:34:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/array-subscript-out-of-range/m-p/461759#M117497</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-05-12T05:34:06Z</dc:date>
    </item>
  </channel>
</rss>

