BookmarkSubscribeRSS Feed
matte109
Calcite | Level 5

Hello,

 

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

 

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???

 

 

 

1 REPLY 1
Astounding
PROC Star

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 1032 views
  • 0 likes
  • 2 in conversation