<?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 do loop index starts from zero in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-index-starts-from-zero/m-p/349845#M81243</link>
    <description>&lt;P&gt;You can explicitly declare the dimensions of your array to control this.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or you could loop to n+1 and know that it's n-1 for the variable name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data scores;
	input ID p0 p1 p2 p3 p4 p5 p6 p7 p8 p9 p10;
datalines;
1 56.23 56.89 57.01 57.55 58.12 58.56 58.99 59.60 60.25 60.89 61.74
2 48.55 49.03 49.58 50.17 50.60 50.98 51.44 51.91 52.36 53.18 53.87
;

proc print data=scores;
run;

data scores;
	set scores;
	array pct p0-p10;
		do i=1 to 11;
		pct(i)=round(pct(i), .1);
		end;
	drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;OR&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data scores;
	input ID p0 p1 p2 p3 p4 p5 p6 p7 p8 p9 p10;
datalines;
1 56.23 56.89 57.01 57.55 58.12 58.56 58.99 59.60 60.25 60.89 61.74
2 48.55 49.03 49.58 50.17 50.60 50.98 51.44 51.91 52.36 53.18 53.87
;

proc print data=scores;
run;

data scores;
	set scores;
	array pct(0:10) p0-p10;
		do i=0 to 10;
		pct(i)=round(pct(i), .1);
		end;
	drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 13 Apr 2017 19:16:26 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2017-04-13T19:16:26Z</dc:date>
    <item>
      <title>Array do loop index starts from zero</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-index-starts-from-zero/m-p/349798#M81231</link>
      <description>&lt;P&gt;I would like to round variables (p0 - p10) in the sample dataset below to 1 decimal place. How can I set the do loop index to start at 0?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is the block of code I used in an attempt to achieve it. However, I got an error message "ERROR: Array subscript out of range" when I ran the data step with the do loop below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data scores;
	input ID p0 p1 p2 p3 p4 p5 p6 p7 p8 p9 p10;
datalines;
1 56.23 56.89 57.01 57.55 58.12 58.56 58.99 59.60 60.25 60.89 61.74
2 48.55 49.03 49.58 50.17 50.60 50.98 51.44 51.91 52.36 53.18 53.87
;

proc print data=scores;
run;

data scores;
	set scores;
	array pct p0-p10;
		do i=0 to 10;
		pct(i)=round(pct(i), .1);
		end;
	drop i;
run;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Below is the error message after I submitted the data step for rounding the variables:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT size="2" color="#800000"&gt;ERROR: Array subscript out of range at line 1355 column 22.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT size="2" color="#800000"&gt;ID=1 p0=56.23 p1=56.9 p3=57 p4=57.6 p5=58.1 p6=58.6 p7=59 p8=59.6 p9=60.3 p10=60.9 p2=. _I_=. i=0&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT size="2" color="#800000"&gt;_ERROR_=1 _N_=1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT size="2" color="#800000"&gt;NOTE: The SAS System stopped processing this step because of errors.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT size="2" color="#800000"&gt;NOTE: There were 1 observations read from the data set WORK.SCORES.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I then tested using the same array and do loop but this time the index starts at 1. The code ran successfully:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data scores;
	set scores;
	array pct p1-p10;
		do i=1 to 10;
		pct(i)=round(pct(i), .1);
		end;
	drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Then I just need to round p0 in another data step. How can I round p0-p10 in one step?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2017 16:49:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-index-starts-from-zero/m-p/349798#M81231</guid>
      <dc:creator>yaozhang</dc:creator>
      <dc:date>2017-04-13T16:49:18Z</dc:date>
    </item>
    <item>
      <title>Re: Array do loop index starts from zero</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-index-starts-from-zero/m-p/349845#M81243</link>
      <description>&lt;P&gt;You can explicitly declare the dimensions of your array to control this.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or you could loop to n+1 and know that it's n-1 for the variable name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data scores;
	input ID p0 p1 p2 p3 p4 p5 p6 p7 p8 p9 p10;
datalines;
1 56.23 56.89 57.01 57.55 58.12 58.56 58.99 59.60 60.25 60.89 61.74
2 48.55 49.03 49.58 50.17 50.60 50.98 51.44 51.91 52.36 53.18 53.87
;

proc print data=scores;
run;

data scores;
	set scores;
	array pct p0-p10;
		do i=1 to 11;
		pct(i)=round(pct(i), .1);
		end;
	drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;OR&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data scores;
	input ID p0 p1 p2 p3 p4 p5 p6 p7 p8 p9 p10;
datalines;
1 56.23 56.89 57.01 57.55 58.12 58.56 58.99 59.60 60.25 60.89 61.74
2 48.55 49.03 49.58 50.17 50.60 50.98 51.44 51.91 52.36 53.18 53.87
;

proc print data=scores;
run;

data scores;
	set scores;
	array pct(0:10) p0-p10;
		do i=0 to 10;
		pct(i)=round(pct(i), .1);
		end;
	drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Apr 2017 19:16:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-index-starts-from-zero/m-p/349845#M81243</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-04-13T19:16:26Z</dc:date>
    </item>
    <item>
      <title>Re: Array do loop index starts from zero</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-index-starts-from-zero/m-p/350973#M81653</link>
      <description>&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 18 Apr 2017 18:44:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-index-starts-from-zero/m-p/350973#M81653</guid>
      <dc:creator>yaozhang</dc:creator>
      <dc:date>2017-04-18T18:44:31Z</dc:date>
    </item>
  </channel>
</rss>

