turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Need help with looping through array

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-09-2016 12:58 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ballardw

05-10-2016 10:25 AM - edited 05-10-2016 11:30 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ascasfaegADGSgs

05-09-2016 01:09 PM

You say

do j=1 to dim(c);

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

PG

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ascasfaegADGSgs

05-09-2016 04:12 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ballardw

05-10-2016 10:25 AM - edited 05-10-2016 11:30 AM

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;