BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Amy0223
Quartz | Level 8

Hi community,

I'm a SAS beginner and I need help with this question below.  a) shows error "Array subscript out of range at line 77 column 14" but I'm not sure how to correct it. b and c seem to have no problems but please correct me if I'm wrong. Thank you very much!

 

*2. What, if anything, is wrong with the following data step program fragments?

  a);
  data c;
   array x{20};
total=0;
do i=1 to 25 by 2;
   total=sum(x(i), total);
 end;
 
 

*b);
data d;
 array x{20};
    n=0;
    do i=1 to 25 by 2;
       n=n+1;
       total=sum(x(n), total);
     end;

 *c);
 do i=0.07 to 0.08;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

For A and B the array is defined with 20 variables, but you are incrementing the index beyond 20.

B works because you are using N and not I as the index into the array and N is increasing by 1 and not 2. So it will never get larger than 20.

 

For A make the upper bound on the DO loop match the dimension of the Array. One way is to use the DIM() function in the DO loop.

do i=1 to dim(x) by 2;

 

For C you did not specify the amount to increment by so it will use 1.  So the loop will run only once since 1.07 is greater than 0.08.

 

A more interesting problem with trying to do something like C is that decimal fractions are not represented exactly in floating point numbers.  So it might be better to control the loop using integers (like 7 to 😎 and then divide the loop index by some factor (like 100) to make the fractional value you want to use in the loop.

 

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

For A and B the array is defined with 20 variables, but you are incrementing the index beyond 20.

B works because you are using N and not I as the index into the array and N is increasing by 1 and not 2. So it will never get larger than 20.

 

For A make the upper bound on the DO loop match the dimension of the Array. One way is to use the DIM() function in the DO loop.

do i=1 to dim(x) by 2;

 

For C you did not specify the amount to increment by so it will use 1.  So the loop will run only once since 1.07 is greater than 0.08.

 

A more interesting problem with trying to do something like C is that decimal fractions are not represented exactly in floating point numbers.  So it might be better to control the loop using integers (like 7 to 😎 and then divide the loop index by some factor (like 100) to make the fractional value you want to use in the loop.

 

Amy0223
Quartz | Level 8
Thank you so much for taking your precious time to answer my question. It's very helpful information. I really appreciate your kind help!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 644 views
  • 2 likes
  • 2 in conversation