<?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/372590#M89106</link>
    <description>&lt;P&gt;Dataset and code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data prices;&lt;/P&gt;&lt;P&gt;Input pID quarter cost1 cost2;&lt;/P&gt;&lt;P&gt;Datalines;&lt;/P&gt;&lt;P&gt;1 99Q1 2530 6930&lt;/P&gt;&lt;P&gt;1 99Q2 5636 7000&lt;/P&gt;&lt;P&gt;1 99Q3 4123 5899&lt;/P&gt;&lt;P&gt;1 99Q4 5000 1425&lt;/P&gt;&lt;P&gt;1 00Q1 2550 4777&lt;/P&gt;&lt;P&gt;1 00Q2 1999 2300&lt;/P&gt;&lt;P&gt;2 99Q1 2500 6930&lt;/P&gt;&lt;P&gt;2 99Q2 5636&amp;nbsp;6000&lt;/P&gt;&lt;P&gt;2 99Q3 4000 5899&lt;/P&gt;&lt;P&gt;2 99Q4 5000 1600&lt;/P&gt;&lt;P&gt;2 00Q1 2300 4777&lt;/P&gt;&lt;P&gt;2 00Q2 1000 2300 /*this continues for about 18 different products, each with 6 quarters*/&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;Data trp;&lt;/P&gt;&lt;P&gt;Set prices;&lt;/P&gt;&lt;P&gt;By PID;&lt;/P&gt;&lt;P&gt;Retain A1 - A6;&lt;/P&gt;&lt;P&gt;Retain B1 - B6;&lt;/P&gt;&lt;P&gt;Array CostA(6) A1 - A6;&lt;/P&gt;&lt;P&gt;Array CostB(6) B1 - B6;&lt;/P&gt;&lt;P&gt;If first.pid then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; do j = 1 to 6;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; costa(J)= .;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; costb(J)= .;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;else I + 1;&lt;/P&gt;&lt;P&gt;costa(i)= &amp;nbsp;cost1; /*line 420*/&lt;/P&gt;&lt;P&gt;costb()= cost2;&lt;/P&gt;&lt;P&gt;if last.pid then output;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The error I get looks something like this, made up the numbers for a and b 1-6:&lt;/P&gt;&lt;P&gt;Error: Array subscript out of range at line 420 column 1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;PID = 1 Quarter = 99Q1 cost1=15000 cost2=13000 first.pid = 0 last.pid=0 a1=10000 a2=12000 a3=12000 a4=15200 a5=16000 a6=13500 b&lt;SPAN&gt;1=10000 b2=12000 b3=12000 b4=15200 b5=16000 b6=13500 j=. i=7 _ERROR_=1 _N_=7&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What am I doing wrong?&lt;/P&gt;&lt;P&gt;Am trying to get each pid to have one line and all the quarters cost1 and cost 2 on that same wide line.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 02 Jul 2017 17:54:14 GMT</pubDate>
    <dc:creator>buffalol</dc:creator>
    <dc:date>2017-07-02T17:54:14Z</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/372590#M89106</link>
      <description>&lt;P&gt;Dataset and code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data prices;&lt;/P&gt;&lt;P&gt;Input pID quarter cost1 cost2;&lt;/P&gt;&lt;P&gt;Datalines;&lt;/P&gt;&lt;P&gt;1 99Q1 2530 6930&lt;/P&gt;&lt;P&gt;1 99Q2 5636 7000&lt;/P&gt;&lt;P&gt;1 99Q3 4123 5899&lt;/P&gt;&lt;P&gt;1 99Q4 5000 1425&lt;/P&gt;&lt;P&gt;1 00Q1 2550 4777&lt;/P&gt;&lt;P&gt;1 00Q2 1999 2300&lt;/P&gt;&lt;P&gt;2 99Q1 2500 6930&lt;/P&gt;&lt;P&gt;2 99Q2 5636&amp;nbsp;6000&lt;/P&gt;&lt;P&gt;2 99Q3 4000 5899&lt;/P&gt;&lt;P&gt;2 99Q4 5000 1600&lt;/P&gt;&lt;P&gt;2 00Q1 2300 4777&lt;/P&gt;&lt;P&gt;2 00Q2 1000 2300 /*this continues for about 18 different products, each with 6 quarters*/&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;Data trp;&lt;/P&gt;&lt;P&gt;Set prices;&lt;/P&gt;&lt;P&gt;By PID;&lt;/P&gt;&lt;P&gt;Retain A1 - A6;&lt;/P&gt;&lt;P&gt;Retain B1 - B6;&lt;/P&gt;&lt;P&gt;Array CostA(6) A1 - A6;&lt;/P&gt;&lt;P&gt;Array CostB(6) B1 - B6;&lt;/P&gt;&lt;P&gt;If first.pid then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; do j = 1 to 6;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; costa(J)= .;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; costb(J)= .;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;else I + 1;&lt;/P&gt;&lt;P&gt;costa(i)= &amp;nbsp;cost1; /*line 420*/&lt;/P&gt;&lt;P&gt;costb()= cost2;&lt;/P&gt;&lt;P&gt;if last.pid then output;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The error I get looks something like this, made up the numbers for a and b 1-6:&lt;/P&gt;&lt;P&gt;Error: Array subscript out of range at line 420 column 1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;PID = 1 Quarter = 99Q1 cost1=15000 cost2=13000 first.pid = 0 last.pid=0 a1=10000 a2=12000 a3=12000 a4=15200 a5=16000 a6=13500 b&lt;SPAN&gt;1=10000 b2=12000 b3=12000 b4=15200 b5=16000 b6=13500 j=. i=7 _ERROR_=1 _N_=7&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What am I doing wrong?&lt;/P&gt;&lt;P&gt;Am trying to get each pid to have one line and all the quarters cost1 and cost 2 on that same wide line.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Jul 2017 17:54:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/372590#M89106</guid>
      <dc:creator>buffalol</dc:creator>
      <dc:date>2017-07-02T17:54:14Z</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/372592#M89107</link>
      <description>&lt;P&gt;You never set the value for i when it's first.pid. Also, on the next line you refer to an array without indicating which cell. Thus, you should change those lines to:&lt;/P&gt;
&lt;PRE&gt;If first.pid then do;
    do j = 1 to 6;
    costa(J)= .;
    costb(J)= .;
end;
I + 1;
costa(i)=  cost1; /*line 420*/
costb(i)= cost2;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Jul 2017 18:02:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/372592#M89107</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-07-02T18:02:09Z</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/372603#M89114</link>
      <description>&lt;P&gt;I'm still getting the same error. The missing cell number was a typo when I was writing the question.&lt;/P&gt;</description>
      <pubDate>Sun, 02 Jul 2017 18:37:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/372603#M89114</guid>
      <dc:creator>buffalol</dc:creator>
      <dc:date>2017-07-02T18:37:05Z</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/372604#M89115</link>
      <description>&lt;P&gt;There were some other errors in your code. If you actually have no more than 6 entries per id, like in your example, the following should work:&lt;/P&gt;
&lt;PRE&gt;Data prices;
Input pID quarter $ cost1 cost2;
Datalines;
1 99Q1 2530 6930
1 99Q2 5636 7000
1 99Q3 4123 5899
1 99Q4 5000 1425
1 00Q1 2550 4777
1 00Q2 1999 2300
2 99Q1 2500 6930
2 99Q2 5636 6000
2 99Q3 4000 5899
2 99Q4 5000 1600
2 00Q1 2300 4777
2 00Q2 1000 2300
;
run;
 
Data trp;
Set prices;
By PID;
Retain A1 - A6;
Retain B1 - B6;
Array CostA(6) A1 - A6;
Array CostB(6) B1 - B6;
  If first.pid then do;
    i=1;
    do j = 1 to 6;
      costa(J)= .;
      costb(J)= .;
    end;
  end;
  else I + 1;
costa(i)=  cost1; /*line 420*/
costb(i)= cost2;
if last.pid then output;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Jul 2017 18:44:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/372604#M89115</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-07-02T18:44: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/372611#M89119</link>
      <description>Great thanks, this now runs, it doesn't look the way it should yet, but I think I know why. Will report back.</description>
      <pubDate>Sun, 02 Jul 2017 18:56:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/372611#M89119</guid>
      <dc:creator>buffalol</dc:creator>
      <dc:date>2017-07-02T18:56:20Z</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/372614#M89120</link>
      <description>&lt;P&gt;If you happen to be on an operating system that doesn't ignore case, you'll have to change the else statement to:&lt;/P&gt;
&lt;PRE&gt;  else i + 1;&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Sun, 02 Jul 2017 19:21:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/372614#M89120</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-07-02T19:21:20Z</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/372619#M89123</link>
      <description>&lt;P&gt;Your code is not doing anything with the QUARTER variable. &amp;nbsp;Does every PID have exactly 6 observations? Do they all cover '99Q1' to '00Q2'? &amp;nbsp;If not then you might need to somehow map the actual QUARTER value into the index into your array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming that each PID has at most 6 observations and that you want to preserve the actual values of QUARTER here is another way that takes a little less coding because it places the SET statement inside of a DO loop so that each iteration of the DATA step covers one PID value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data trp;
  do _n_=1 by 1 until (last.pid) ;
    set prices;
    by PID;
    array qtr(6) $4 qtr1-qtr6 ;
    array CostA(6) A1 - A6;
    array CostB(6) B1 - B6;
    qtr(_n_)=quarter;
    costa(_n_)= cost1;
    costb(_n_)= cost2;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Jul 2017 19:43:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/372619#M89123</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-07-02T19:43:21Z</dc:date>
    </item>
  </channel>
</rss>

