DATA Step, Macro, Functions and more

Array subscript out of range

Accepted Solution Solved
Reply
Super Contributor
Posts: 286
Accepted Solution

Array subscript out of range

My code is below. My approach is clunky at best. I submit it, see if there is an error, adjust the code and resubmit. How can I edit it so that SAS determines the dimensions needed?

 

 

data coregrades;
set core2;
format mark1-mark5 $3. course_name1-course_name5 $8. name1-name5 $30. point1-point5 1. course_number1-course_number5 $8.;
by student_number;
retain name1-name5 course_name1-course_name5 mark1-mark5 point1-point5 course_number1-course_number5;
if first.student_number then do;
name1=' '; name2=' '; name3=' '; name4=' '; name5=' ';
course_name1=' '; course_name2=' '; course_name3=' '; course_name4=' '; course_name5=' ';
mark1=' '; mark2=' '; mark3=' '; mark4=' '; mark5=' ';
point1=.; point2=.; point3=.; point4=.; point5=.;
course_number1=' ';course_number2=' ';course_number3=' ';course_number4=' ';course_number5=' ';
markcount=1;
end;
array names {5} name1-name5;
array course_names {5} course_name1-course_name5;
array marks {5} mark1-mark5;
array points {5} point1-point5;
array course_numbers {5} course_number1-course_number5;
names {markcount} = name;
course_names {markcount} = course_name;
marks {markcount} = mark;
points {markcount} = point;
course_numbers {markcount} = course_number;
markcount+1;
drop name course_name mark point course_number;
if last.student_number;
run;


Accepted Solutions
Solution
‎08-29-2017 09:10 AM
Super User
Posts: 9,924

Re: Array subscript out of range

Determine the needed array size by running:

proc sql;
create table int as
select count(*) as count from core2 group by student_number;
select max(count) from int;
quit;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Regular Contributor
Posts: 153

Re: Array subscript out of range

Hi,

 

you could use the colon wildcard.

data test;
   name1=1;
   name2=2;
   name3=3;
   name4=4;
   name5=5;
   output;
run;
data test;
   set test;
   array names name:;
   do over names;
      names=names+1;
      put names;
   end;
run;
________________________

- Cheers -

Solution
‎08-29-2017 09:10 AM
Super User
Posts: 9,924

Re: Array subscript out of range

Determine the needed array size by running:

proc sql;
create table int as
select count(*) as count from core2 group by student_number;
select max(count) from int;
quit;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 247 views
  • 0 likes
  • 3 in conversation