<?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/442320#M110655</link>
    <description>&lt;P&gt;Your loop...&lt;/P&gt;
&lt;PRE&gt;do i=&amp;amp;dateoflastrepricing to hbound(_year);&lt;/PRE&gt;
&lt;P&gt;....iterates until&amp;nbsp;the very last element of the array....&lt;/P&gt;
&lt;PRE&gt;array _year(2017:2022) year2017-year2022;
&lt;/PRE&gt;
&lt;P&gt;....and though already in the second last iteration...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;_year(i+2)&lt;/PRE&gt;
&lt;P&gt;...will point to an array element which hasn't been defined. That's why you get the error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 05 Mar 2018 12:12:42 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2018-03-05T12:12:42Z</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/442299#M110649</link>
      <description>&lt;P&gt;I have a dataset as such:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset which looks like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;ID      2017    2018    2019    2020

2017    30      24      20      18
2018    30      24      20      18
2019    30      24      20      18
2020    30      24      20      18&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am looking to create an array based on a few inputs:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;%let FixedorFloating = '1 or 0';
%let Repricingfrequency = n Years;
%let LastRepricingDate = 'Date'n;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;The array criteria is such that if ID= Year +2i then flag = 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;e.g.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID = 2017 then flag =1 for Years 2017 and 2019, 0 otherwise&lt;/P&gt;&lt;P&gt;ID = 2018 then flag = 1 for Years 2018 and 2020, 0 otherwise&lt;/P&gt;&lt;P&gt;ID = 2019 then flag = 1 for Year 2019 , 0 otherwise&lt;/P&gt;&lt;P&gt;ID = 2020 then flag = 1 for year 2020, 0 otherwise&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My code is currently, I'm having issues with year i+2 (highlighted in red) but year(i) works fine.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ReferenceRateContract;
	set refratecontract;

*arrays for years and flags;
	array _year(2017:2022) year2017-year2022;
	array _flag(2017:2022) flag2017-flag2022;

*loop over array;

	if &amp;amp;FixedorFloating=1

		then do i=&amp;amp;dateoflastrepricing to hbound(_year);
    	
    	/*check if year matches year in variable name*/
  
  		if put(ID, 4.) = compress(vname(_year(i)),, 'kd') 
  			then _flag(i)=1;
  
  		else _flag(i)=0;
	
	end;

	else if &amp;amp;fixedorfloating=0

		then do i=&amp;amp;dateoflastrepricing to hbound(_year);
  
  		if put(ID, 4.) = compress(vname(_year(i)),, 'kd') 
  			then _flag(i)=1;
  
  		&lt;FONT color="#ff0000" face="arial black,avant garde"&gt;else if put(ID, 4.) = compress(vname(_year(i+2)),, 'kd') 
  		then _flag(i)=1;&lt;/FONT&gt;
  
  		else _flag(i)=0;
	end;

	drop i;

run;

data referenceratecontract;
set referenceratecontract;
keep flag2017--flag2020;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Looking for a way to flag based on Id= Year + 2i, TIA&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#ff0000" face="Arial Black"&gt;else if put(ID, 4.) = compress(vname(_year(i+2)),, 'kd') then _flag(i)=1;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Mar 2018 10:47:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/442299#M110649</guid>
      <dc:creator>89974114</dc:creator>
      <dc:date>2018-03-05T10:47:45Z</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/442310#M110651</link>
      <description>&lt;P&gt;As with any question like this, the first question I always ask is why the data looks like this in the first place.&amp;nbsp; Bad data modelling results in the rest of the process being sub-optimal.&amp;nbsp; So first quesiton, why is ID a year value?&amp;nbsp; Next, why are years transposed?&amp;nbsp; Normalised data is far simpler, and makes this (as with the hundreds of other questions on here using transposed data) so simple:&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;YEAR&amp;nbsp; VALUE&lt;/P&gt;
&lt;P&gt;2017&amp;nbsp; &amp;nbsp;2017&amp;nbsp; &amp;nbsp; 30&lt;/P&gt;
&lt;P&gt;2017&amp;nbsp; &amp;nbsp;2018&amp;nbsp; &amp;nbsp; 24&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Mar 2018 11:47:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/442310#M110651</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-03-05T11:47:23Z</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/442312#M110652</link>
      <description>&lt;P&gt;ID identifies the vintage of the bond , the columns specify basis points for each year&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;        2017        2018        2019        2020
17    bp1          bp2          bp3          bp4
18    bp1          bp2 ...
...&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 05 Mar 2018 11:51:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/442312#M110652</guid>
      <dc:creator>89974114</dc:creator>
      <dc:date>2018-03-05T11:51: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/442318#M110653</link>
      <description>&lt;P&gt;Turn your dumb data into intelligent data first by transposing, then perform a simple sequential data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID val_2017 val_2018 val_2019 val_2020;
cards;
2017    30      24      20      18
2018    30      24      20      18
2019    30      24      20      18
2020    30      24      20      18
;
run;

proc transpose
  data=have
  out=int (rename=(col1=value))
;
by id;
var val_:;
run;

data have_good;
set int;
year = input(substr(_name_,5),best.);
drop _name_;
run;

data want;
set have_good;
retain firstyear;
by id;
if first.id
then do;
  firstyear = year;
  flag = 1;
end;
else if year = firstyear + 2
then flag = 1;
else flag = 0;
drop firstyear;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You may need to tweak the condition where you set firstyear and start analysing.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Mar 2018 12:07:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/442318#M110653</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-03-05T12:07:23Z</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/442320#M110655</link>
      <description>&lt;P&gt;Your loop...&lt;/P&gt;
&lt;PRE&gt;do i=&amp;amp;dateoflastrepricing to hbound(_year);&lt;/PRE&gt;
&lt;P&gt;....iterates until&amp;nbsp;the very last element of the array....&lt;/P&gt;
&lt;PRE&gt;array _year(2017:2022) year2017-year2022;
&lt;/PRE&gt;
&lt;P&gt;....and though already in the second last iteration...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;_year(i+2)&lt;/PRE&gt;
&lt;P&gt;...will point to an array element which hasn't been defined. That's why you get the error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Mar 2018 12:12:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/442320#M110655</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-03-05T12:12:42Z</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/442333#M110658</link>
      <description>&lt;P&gt;Thanks, I'll have to re-write the dimensions and loop&lt;/P&gt;</description>
      <pubDate>Mon, 05 Mar 2018 12:57:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/442333#M110658</guid>
      <dc:creator>89974114</dc:creator>
      <dc:date>2018-03-05T12:57:33Z</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/442352#M110667</link>
      <description>&lt;P&gt;I changed the dimensions with a macro variable and the problem was that I was putting I+2 instead of I-2, I-2 is what is required for my desired impact.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Mar 2018 13:53:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-subscript-out-of-range/m-p/442352#M110667</guid>
      <dc:creator>89974114</dc:creator>
      <dc:date>2018-03-05T13:53:00Z</dc:date>
    </item>
  </channel>
</rss>

