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;
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.
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.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.