SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
yaozhang
Fluorite | Level 6

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?

 

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:

 

 

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;

Below is the error message after I submitted the data step for rounding the variables:

ERROR: Array subscript out of range at line 1355 column 22.
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
_ERROR_=1 _N_=1
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 1 observations read from the data set WORK.SCORES.

 

 

I then tested using the same array and do loop but this time the index starts at 1. The code ran successfully:

data scores;
	set scores;
	array pct p1-p10;
		do i=1 to 10;
		pct(i)=round(pct(i), .1);
		end;
	drop i;
run;

 

 Then I just need to round p0 in another data step. How can I round p0-p10 in one step?

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

You can explicitly declare the dimensions of your array to control this. 

Or you could loop to n+1 and know that it's n-1 for the variable name.

 

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;

OR

 

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;

View solution in original post

2 REPLIES 2
Reeza
Super User

You can explicitly declare the dimensions of your array to control this. 

Or you could loop to n+1 and know that it's n-1 for the variable name.

 

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;

OR

 

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;
yaozhang
Fluorite | Level 6

Thanks!

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 4516 views
  • 4 likes
  • 2 in conversation