trying to add values to an array through looping without replacing the old values

Reply
Senior User
Posts: 1

trying to add values to an array through looping without replacing the old values

Hello,

 

I'm a new SAS user, so thanks in advance for your help. Smiley Happy

 

I have a loop inside a loop that puts x variable values into an array, and then the outer loop iterates y times. When it iterates the next y time, I want the array to keep the previous values and add the new values to that array instead of replacing the previous values (I assume that if I use retain, it would stop it from replacing but not add the new ones). This is my basic code.

 

1    do y = 1 to 5;

2    if array1[y] ~= . then

3    do x = 1 to 3;

4    if array2[x] = . or array2[x] < array1[y] then array3[x] = .;

5    else array3[x] = array2[x] - array1[y];

6    end;

7    else do x = 1 to 3;

8    array3[x] = .;

9    end;

10   end;

 

I tried adding this before the last end statement (so it is included in the y iteration):

 

Z = 1; (specified outside of loops)

 

11   do j = 1 to 3;

12   array4[Z] = array3[j];

13   Z+1;

14   end;

 

15   Z+1;

 

...but it says there's an array subscript out of range at line 12.

 

I've attached the actual code I used.

 

Ideas???

 

 

 

Attachment
Super User
Posts: 6,935

Re: trying to add values to an array through looping without replacing the old values

Each array has a fixed number of elements.  In that sense, it is not something you can expand or add to.  All you can do is replace some of the current values within the array.

 

There may be a way to accomplish what you need, but you will have to illustrate the "before" and "after" picture using a couple of observations of data.

Ask a Question
Discussion stats
  • 1 reply
  • 71 views
  • 0 likes
  • 2 in conversation