Hello
If I have one if statement then the code works ok however, the result is wrong, If I added the two if statements, then the code goes into indefinite mode and I had to terminate the SAS. My real life data is not big just 24 observations, I also tried to take 5 observations instead of the 24 but still the answer would be wrong.
I would appreciate any suggestions.
proc iml;
X={0.2, 0.3, 0.5, 0.5, 0.5, 0.5, 0.6, 0.6, 0.7, 0.7, 0.7, 0.8, 0.8, 1.0, 1.0, 1.0, 1.0, 1.1, 1.3, 1.5, 1.5,
1.5, 1.5, 2.0, 2.0, 2.2, 2.5, 3.0, 3.0, 3.3, 3.3, 4.0, 4.0, 4.5, 4.7, 5.0, 5.4, 5.4, 7.0, 7.5, 8.8, 9.0,
10.3, 22.0, 24.5};
Y=1/sqrt(X);
Call Sort(Y);
n=nrow(Y);
m=2;
sum=0;
do i=1 to n;
*if (i<=m) then i=m+1; if (i<=m) then Y[i-m]=Y[1];
if i >= n-m then Y[i+m]=Y[n];
*sum = sum + 1/(Y1[i+m]-Y1[i-m]);
XXX = Y1[i+m]-Y1[i-m]; print XXX;
end;
Inside the loop is the statement:
if (i<=m) then Y[i-m]=Y[1];
When i=1 and m=2, the expression i-m= -1. The vectors in IML are one-based arrays, so it is an error to try to set Y[-1]. Thus, the program gives the error
ERROR: (execution) Invalid subscript or subscript out of range.
BTW, later, the program tries to assign XXX = Y1[i+m]-Y1[i-m]; There is no Y1 array, so perhaps you mean to use Y.
thank you for the reply, yes, when i-m= -1 that is why I used the if statement so i=m+1 and eventually the first 3 terms will be Y3-Y1, Y4-Y1, Y5-Y1.
And you are right, I mean Y not Y1. I just copied the old code and forgot to fix it.
To make sure we are talking about the same statements, I want to emphasize that the first comment in the loop applies only up to the first semicolon, NOT the end of the line. So the program that is being run is
do i=1 to n;
if (i<=m) then Y[i-m]=Y[1];
if i >= n-m then Y[i+m]=Y[n];
XXX = Y1[i+m]-Y1[i-m];
print XXX;
end;
The first statement inside the loop is invalid.
I understand, but how would I fix it ?
If you want "the first 3 terms to be Y3-Y1, Y4-Y1, Y5-Y1" then use
Y[1:3] = Y[3:5] - Y[1];
I want the code to work for any data set, when I talked about the first 3 items, is to break the code down so it is easy for me to check the code by hand. I am working on having a general statement. If I can get it work for these items then I can extend it to all items.
I am sorry, but I do not understand the general algorithm that you want to apply. If you want to use the value m to determine the number of elements to copy, then perhaps the following statements will help:
/* subtract Y[1] from elements m+1,m+2,...,2*m+1 and copy those
values into elements 1,2,...,m+1 */
Y1 = y[1];
idx = 1:(1+m);
Y[idx] = Y[m+idx] - Y1;
If that is not what you want, please explain and provide an example.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.