BookmarkSubscribeRSS Feed
n6
Quartz | Level 8 n6
Quartz | Level 8

I haven't used arrays recently but I would've sworn that data in them didn't retain.  But then I wrote some code using arrays and was getting unexpected answers and I finally figured out it was because the data was being retained when it went to the next observation.  It took me awhile to figure it out because I didn't even consider that a possibility because I was so sure that that wasn't how arrays work in SAS.

 

So the question is, is there a simple way to tell SAS to not retain data when I'm using arrays?  Any info is appreciated.

8 REPLIES 8
pink_poodle
Barite | Level 11
Maybe you could try using a _temporary_ option, for example, array a {3} _temporary_ 1 2 3.
Tom
Super User Tom
Super User

Array statements do no have any impact on whether variables are retained.  But _TEMPORARY_ arrays, which do not use actual variables, are retained.

The other way that variables will be retained is if they coming from an input dataset.

 

So to prevent variables in an array from being retained make sure they are NEW permanent variables.  If you don't want them written to the output dataset then DROP them.

KachiM
Rhodochrosite | Level 12

As said before_temporary_ array values are by defualt retained. But variables as expressed in array are not retained. Array values are refreshed as data step iterates. Here is an example and see the LOG.

data _null_;
	set sashelp.class;
	array k[*] age height weight;
	put k[1] = k[2] = k[3] =;
run;
Tom
Super User Tom
Super User

@KachiM wrote:

As said before_temporary_ array values are by defualt retained. But variables as expressed in array are not retained. Array values are refreshed as data step iterates. Here is an example and see the LOG.

data _null_;
	set sashelp.class;
	array k[*] age height weight;
	put k[1] = k[2] = k[3] =;
run;

So that is an example of an array where the values ARE retained.  But since the data step is so simple it is hard to tell since the SET statement overwrites the retained value with the values from the next observation read.

To see it in action add a PUT AGE= statement BEFORE the SET statement.

KachiM
Rhodochrosite | Level 12

I understood that a 

Retain statement defined by SAS

Causes a variable that is created by an INPUT or assignment statement to retain its value from one iteration of the DATA step to the next.

andreas_lds
Jade | Level 19

Please show the code, so that we can suggest improvements.

FreelanceReinh
Jade | Level 19

Hello @n6,

 

There is one more situation where array elements are retained: if one or more of them are assigned initial values (see documentation). That is, if you define an array of dimension 1000 and supply an initial value for one of these variables, this variable and all the 999 others will be retained.

 

If that is what happened in your code, consider using an explicit RETAIN statement or assignment statements to supply the initial values or reset those variables to missing (e.g., with CALL MISSING) that you don't want to be retained.

ballardw
Super User

Reset the values when done or when you need them "not retained".

 

Without a real concrete example it is hard to provide an optimal method.

 

It is not clear whether you want to set values to missing or to a specific list of values. So example code where you are using a temporary array that needs to be "not retained" with a description of the rules for when "not retained" is desired would help a lot.

 

From the documentation

_TEMPORARY_

creates a list of temporary data elements.

Range Temporary data elements can be numeric or character.
Tips Temporary data elements behave like DATA step variables with these exceptions:
They do not have names. Refer to temporary data elements by the array name and dimension.
They do not appear in the output data set.
You cannot use the special subscript asterisk (*) to refer to all the elements.
Temporary data element values are always automatically retained, rather than being reset to missing at the beginning of the next iteration of the DATA step.
Arrays of temporary elements are useful when the only purpose for creating an array is to perform a calculation. To preserve the result of the calculation, assign it to a variable. You can improve performance time by using temporary data elements.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 8 replies
  • 1059 views
  • 2 likes
  • 7 in conversation