<?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: Array subscript out of range in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/440194#M282509</link>
    <description>&lt;P&gt;Here you go:&lt;/P&gt;
&lt;PRE&gt;data two;
  input year var1 $ var2 $ var3 $ var4 $;
cards;
2016 john jack sarah susan
2015 ben bill britney anne
2014 chris peter monica robin
2013 jeff matt christie christine
2012 mike david dia diane
;
run;

data want (keep=year gender color team);
  set two;
  length gender color $20;
  array var{4};
  i=1;
  do gender="male","female";
     do color="red","green";
        team=var{i};
        output;
        i=i+1;
     end;
  end;
run;&lt;/PRE&gt;</description>
    <pubDate>Mon, 26 Feb 2018 14:56:00 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2018-02-26T14:56:00Z</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/440175#M282503</link>
      <description>&lt;P&gt;Hi everyone,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I keep getting errors when I run the following program. Dataset two has 5 variables, year, var1-var4 and 5 observations. The dataset one that I am trying to create should have 20 observations with variables year, gender, color and team. I want to split var1-var4 for for the two genders and 2 colors and do the same for each of the five years(2016-2012). But everytime i run this program i get an error that the array subscript is out of range. It feel like I am making a logic mistake somewhere but am not able to figure out where.&amp;nbsp;&lt;/P&gt;&lt;P&gt;data one;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set two;&lt;/P&gt;&lt;P&gt;&amp;nbsp; array tm (*) $ var1 var2 var3 var4;&lt;/P&gt;&lt;P&gt;&amp;nbsp; do year=2016 to 2012 by -1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; do gender="Male","Female";&lt;/P&gt;&lt;P&gt;&amp;nbsp; do color="Red","Green";&lt;/P&gt;&lt;P&gt;&amp;nbsp; team=tm(year);&lt;/P&gt;&lt;P&gt;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want:&lt;/P&gt;&lt;P&gt;dataset one:&lt;/P&gt;&lt;P&gt;year gender color team&lt;/P&gt;&lt;P&gt;2016 male red var1 value&lt;/P&gt;&lt;P&gt;2016 male green var2 value&lt;/P&gt;&lt;P&gt;2016 female red var3 value&lt;/P&gt;&lt;P&gt;2016 female green var4 value&lt;/P&gt;&lt;P&gt;and so on.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any input is much appreciated. Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2018 13:37:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/440175#M282503</guid>
      <dc:creator>vseshad</dc:creator>
      <dc:date>2018-02-26T13:37: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/440176#M282504</link>
      <description>&lt;P&gt;This:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; array tm (*) $ var1 var2 var3 var4;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; do year=2016 to 2012 by -1;&lt;/P&gt;
&lt;P&gt;There are five iterations: 2016, 2015, 2014, 2013, 2012, however there are only four elements var1-var4.&amp;nbsp; Hence you get an out of range.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you can post some sample test data in the form of a datastep, then I can supply some appropriate code to process it.&amp;nbsp; Just from a guess, I would say that having a counter within the year do loop which goes from 1 to 4 would work.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2018 14:03:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/440176#M282504</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-02-26T14:03:02Z</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/440180#M282505</link>
      <description>&lt;P&gt;Besides the number of elements in the array, you have to consider how you will refer to them.&amp;nbsp; You are using this reference:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;tm(year)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When YEAR is 2016, that refers to the 2016th element of the array ... a far cry from your intention.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The easiest syntax for the array definition ... one that would let you refer to tm(year) ... would be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;array tm {2012:2016} $ var1-var5;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now SAS will expect numbers in the range of 2012 to 2016, to refer to the five elements of the array.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2018 14:13:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/440180#M282505</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-02-26T14:13:35Z</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/440189#M282506</link>
      <description>&lt;P&gt;Thanks RW9. So my dataset&amp;nbsp;two looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data two;&lt;/P&gt;&lt;P&gt;&amp;nbsp; input year var1 $ var2 $ var3 $ var4 $;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;2016 john jack sarah susan&lt;/P&gt;&lt;P&gt;2015 ben bill britney anne&lt;/P&gt;&lt;P&gt;2014 chris peter monica robin&lt;/P&gt;&lt;P&gt;2013 jeff matt christie christine&lt;/P&gt;&lt;P&gt;2012 mike david dia diane&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;dataset one&amp;nbsp;should look like this:&lt;/P&gt;&lt;P&gt;year gender color team&lt;/P&gt;&lt;P&gt;2016 male red&amp;nbsp;John&lt;/P&gt;&lt;P&gt;2016 male green&amp;nbsp;Jack&lt;/P&gt;&lt;P&gt;2016 female red&amp;nbsp;Sarah&lt;/P&gt;&lt;P&gt;2016 female green&amp;nbsp;Susan&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please let me know if I should provide more information. Thanks again&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2018 14:43:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/440189#M282506</guid>
      <dc:creator>vseshad</dc:creator>
      <dc:date>2018-02-26T14:43:58Z</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/440190#M282507</link>
      <description>&lt;P&gt;Thanks Astounding. I can see that I need to be more careful when writing the programs.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2018 14:45:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/440190#M282507</guid>
      <dc:creator>vseshad</dc:creator>
      <dc:date>2018-02-26T14:45:00Z</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/440193#M282508</link>
      <description>&lt;P&gt;The reason why you get the message is obvious: the array TM is indexed 1 to 4, not 2012 to 2016. So an array entry like tm(2016) does not exist, and you get the error message.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Apart from that, you have another problem: your array has 4 elements, but you are trying to loop through 5 elements (indexes 2012, 2013, 2014 2015 and 2016).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So you need to remove a year from your loop, or come up with another variable for the last (or first?) year. Apart from that, the simple (but not much used) solution is to use another index for your array, e.g.:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
  set two;
  array tm(2012:2016) var1-var5;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Now you can fetch an array element like tm(2013).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2018 14:52:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/440193#M282508</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-02-26T14:52: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/440194#M282509</link>
      <description>&lt;P&gt;Here you go:&lt;/P&gt;
&lt;PRE&gt;data two;
  input year var1 $ var2 $ var3 $ var4 $;
cards;
2016 john jack sarah susan
2015 ben bill britney anne
2014 chris peter monica robin
2013 jeff matt christie christine
2012 mike david dia diane
;
run;

data want (keep=year gender color team);
  set two;
  length gender color $20;
  array var{4};
  i=1;
  do gender="male","female";
     do color="red","green";
        team=var{i};
        output;
        i=i+1;
     end;
  end;
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 26 Feb 2018 14:56:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/440194#M282509</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-02-26T14:56:00Z</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/440302#M282510</link>
      <description>&lt;P&gt;Thank you so much RW9. But if I could bother you just a little bit more, I don't understand how the same year is repeated 4 times. This is exactly what I want but I am not clear how that is happening. Also, shouldn't I have to specify that the array is character array? I apologize if I am asking too many questions or if my questions are too basic.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2018 18:56:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/440302#M282510</guid>
      <dc:creator>vseshad</dc:creator>
      <dc:date>2018-02-26T18:56:28Z</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/440414#M282511</link>
      <description>&lt;P&gt;No probs, for your two questions:&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The array statement is a reference to variables in the dataset, if they do not exist then SAS needs to create them.&amp;nbsp; In this instance however the variables var1-4 already exist and have their properties, so we only need the reference to them.&amp;nbsp; If they did not exists then you would need to supply values or properties.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The year is not populated 4 times, what actually happens is that the data is written out 4 times due to the output in the two do loops.&amp;nbsp; As we do not change the year value, it is the same at each output statement call - only at the next loop round the data set (i.e. when a set is encountered) does year change.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Feb 2018 09:50:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/440414#M282511</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-02-27T09:50:27Z</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/440659#M282512</link>
      <description>&lt;P&gt;Why not just read it in that way?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  input year @ ;
  length gender color name $20;
  do gender="male","female";
    do color="red","green";
      input name @ ;
      output;
    end;
  end;
cards;
2016 john jack sarah susan
2015 ben bill britney anne
2014 chris peter monica robin
2013 jeff matt christie christine
2012 mike david dia diane
;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Feb 2018 20:44:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/440659#M282512</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-02-27T20:44:17Z</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/440894#M282513</link>
      <description>&lt;P&gt;Thank you so much for taking the time to explain. I really appreciate all your help.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Feb 2018 14:24:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/440894#M282513</guid>
      <dc:creator>vseshad</dc:creator>
      <dc:date>2018-02-28T14:24:25Z</dc:date>
    </item>
  </channel>
</rss>

