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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 455 views
  • 2 likes
  • 2 in conversation