Previous Page | Next Page

SAS Component Language Dictionary

REDIM



Resizes a dynamic array
Category: Array

Syntax
Details
Examples
Example 1: Resize an Array Preserving the Data
Example 2: Resize a Two-dimensional Array Preserving the Data
Example 3: Resize an Array Without Preserving the Data
See Also

Syntax

rc=REDIM(array,dim1<,dim2<,dim3....<,dimN>...>>);

rc

indicates whether the operation was successful.

0

successful

[ne]0

not successful

Type: Numeric

array

is the dynamic array to be resized. A non-dynamic array causes an error condition.

Type: Array

dim1...dimN

is the size of each specified dimension. If you specify negative sizes or an invalid number of dimensions, an error condition occurs.

Type: Numeric


Details

You can use the REDIM function to resize a dynamic array. You cannot change the numbers of dimensions or type of the array, only the bounds. The REDIM function will preserve the data in the array. However, if you resize the array to a smaller size, you will lose the data in the eliminated elements. There is no limit to the number of times that you can resize an array.


Examples


Example 1: Resize an Array Preserving the Data

This example creates a one-dimensional array of 3 elements, populates it with data, and then resizes it using REDIM to 5 elements while preserving the data.

DCL num a(*);
a = makearray(3);
do i=1 to dim(a);
   a[i]=i;
end;
rc = redim(a,5);
put a=;
rc=delarray(a);

The output:

a=
a[1]=1
a[2]=2
a[3]=3
a[4]=.
a[5]=.


Example 2: Resize a Two-dimensional Array Preserving the Data

This example creates a 3x3 array, populates it with data, and then resizes it to a 4x4 array using REDIM, preserving the initial data.

DCL num a(*,*);    
a = makearray(3,3);  
do i=1 to dim(a);  
 do j=1 to dim(a);   
   a[i,j]=i;   
 end;    
end;  
put a=;   
rc = REDIM(a,4,4);  
put a=;   
rc = delarray(a); 

The output:

a=
a[1,1] = 1
a[1,2] = 1
a[1,3] = 1
a[2,1] = 2
a[2,2] = 2
a[2,3] = 2
a[3,1] = 3
a[3,2] = 3
a[3,3] = 3
a=
a[1,1] = 1
a[1,2] = 1
a[1,3] = 1
a[1,4] = .
a[2,1] = 2
a[2,2] = 2
a[2,3] = 2
a[2,4] = .
a[3,1] = 3
a[3,2] = 3
a[3,3] = 3
a[3,4] = .
a[4,1] = .
a[4,2] = .
a[4,3] = .
a[4,4] = .


Example 3: Resize an Array Without Preserving the Data

This example creates a 3x3 array, populates it with data, and then resizes it to a 4x4 array without using REDIM and without preserving the data.

DCL num a(*,*);    
a = makearray(3,3); 
do i=1 to dim(a);  
 do j=1 to dim(a);   
   a[i,j]=i;   
 end;    
end;  
put a=;   
a = makearray(4,4);  
put a=;   
rc = delarray(a); 

The output:

a=
a[1,1] = 1
a[1,2] = 1
a[1,3] = 1
a[2,1] = 2
a[2,2] = 2
a[2,3] = 2
a[3,1] = 3
a[3,2] = 3
a[3,3] = 3
a=
a[1,1] = .
a[1,2] = .
a[1,3] = .
a[1,4] = .
a[2,1] = .
a[2,2] = .
a[2,3] = .
a[2,4] = .
a[3,1] = .
a[3,2] = .
a[3,3] = .
a[3,4] = .
a[4,1] = .
a[4,2] = .
a[4,3] = .
a[4,4] = .


See Also

DELARRAY

MAKEARRAY

SCL Arrays

Previous Page | Next Page | Top of Page