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!
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;
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;
Thanks!
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: