## Need help with looping through array

Solved
Occasional Contributor
Posts: 8

# Need help with looping through array

Hi,

I have these equations (as an example):

ACVF1=c1+c1*c2+c2*c3+c3*c4;
ACVF2=c2+c1*c3+c2*c4;
ACVF3=c3+c1*c4;
ACVF4=c4;

I need to fulfill them in general to be able to calculate various LAG autocorrelation's for ma(q) process.

Here is what I have accomplished so far:

```%let LAG = 4;
%let cmax = 0.9;
%let cmin = 0.3;
%let cvalue = (&cmax.-&cmin.)/(&LAG.-1);
data tACVF;
array ACVF(&LAG.);
array c(&LAG.);
do i=1 to dim(c);
c(i)=&cmax.-(&cvalue.*(i-1));
ACVF(i)=c(i);
do j=1 to dim(c);
ACVF(j)=ACVF(j)+c(i)*c(j+1);
output;
end;
end;
proc print;
run;```

I'm getting this:

ERROR: Array subscript out of range at line 66 column 22.

I assume this is because of reaching higher index then there is, but I don't know how to solve this issue. Can anyone help ?

Accepted Solutions
Solution
‎05-10-2016 10:25 AM
Occasional Contributor
Posts: 8

## Re: Need help with looping through array

[ Edited ]

That's a bit closer to what I wanted to achieve, but you gave me an idea on how to solve my issue here. Many thanks!

Here's the code:

```%let LAG = 4;
%let cmax = 0.9;
%let cmin = 0.3;
%let cvalue = (&cmax.-&cmin.)/(&LAG.-1);
data tACVF;
array ACVF(&LAG.);
array c(&LAG.);
/* initialize C array*/
do i=1 to dim(c);
c(i)=&cmax.-(&cvalue.*(i-1));
end;
do k=1 to dim(Acvf);
ACVF(k)=c(k);
do j=1 to (dim(c)-k);
s=j+k;
ACVF(k)=sum(ACVF(k),c(j)*c(s));
end;
/* possible output here*/
end;
proc print;
run;```

All Replies
Posts: 5,526

## Re: Need help with looping through array

You say

`do j=1 to dim(c);`

and then refer to c(j+1), That's obviously out of range...

PG
Super User
Posts: 13,523

## Re: Need help with looping through array

Are you looking for something like this:

``````
%let LAG = 4;
%let cmax = 0.9;
%let cmin = 0.3;
%let cvalue = (&cmax.-&cmin.)/(&LAG.-1);
data tACVF;
array ACVF(&LAG.);
array c(&LAG.);
/* initialize C array*/
do i=1 to dim(c);
c(i)=&cmax.-(&cvalue.*(i-1));
end;
do k=1 to dim(Acvf);
ACVF(k)=c(k);
do j=k to (dim(c)-1);
ACVF(j)=sum(ACVF(j),c(j)*c(j+1));
end;
/* possible output here*/
end;
run;``````

That has all the values in one record.

If you are looking for triangle of values for ACVF then use the location indicated for the output. Your original output statement was going to create possibly more records than you really wanted as you had intermediate values of the ACVF. Drop the i, j, k if desired.

Solution
‎05-10-2016 10:25 AM
Occasional Contributor
Posts: 8

## Re: Need help with looping through array

[ Edited ]

That's a bit closer to what I wanted to achieve, but you gave me an idea on how to solve my issue here. Many thanks!

Here's the code:

```%let LAG = 4;
%let cmax = 0.9;
%let cmin = 0.3;
%let cvalue = (&cmax.-&cmin.)/(&LAG.-1);
data tACVF;
array ACVF(&LAG.);
array c(&LAG.);
/* initialize C array*/
do i=1 to dim(c);
c(i)=&cmax.-(&cvalue.*(i-1));
end;
do k=1 to dim(Acvf);
ACVF(k)=c(k);
do j=1 to (dim(c)-k);
s=j+k;
ACVF(k)=sum(ACVF(k),c(j)*c(s));
end;
/* possible output here*/
end;
proc print;
run;```

🔒 This topic is solved and locked.