SAS Component Language Dictionary |
Category: | Array |
Syntax | |
Details | |
Examples | |
Example 1: Create a One-Dimensional Array | |
Example 2: Create a Two-Dimensional Array | |
Example 3: Create an Array from a Table | |
See Also |
Syntax |
array=MAKEARRAY(dim1<,...,dimN>); |
is the dynamic array to be created. A non-dynamic array causes an error condition.
Type: Array
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 |
Unlike static arrays, whose bounds must be set at compile time, you can create and resize (change the bounds of) dynamic arrays at run time. The low bound of the dynamic array will always be 1, and the high bound will be determined as given at run time. If you create a one-dimensional dynamic array with 5 elements, then the low bound and high bound will be 1 and 5, respectively. The array must be declared using an asterisk (*) for the array dimensions with no array elements or initial values specified. The syntax is the same as for a reference array. For example, the following lines declare a one-dimensional numeric dynamic array named A and a two-dimensional character dynamic array named B:
DCL num A(*); DCL char B(*,*);
The MAKEARRAY function creates an array of the given size. All elements in the array initialized to missing for numeric values or blank for character values. The number of dimensions must be the same as what was specified in the DECLARE statement.
If you use the MAKEARRAY function to resize a dynamic array, all the data is lost and becomes garbage. If you try to reference an array element without first creating the array, an error occurs.
Dynamic arrays can be used with the other existing array functions (DIM, HBOUND, LBOUND) as long as the array has been created with MAKEARRAY. If you try to use these other functions without first creating the array, a program halt occurs.
Examples |
This example creates a one-dimensional array of 5 elements.
DCL num a(*); a = makearray(5);
This example creates a two-dimensional 5x5 array.
DCL num a(*,*); a = makearray(5,5);
This example uses table work.a , which has only numerical variables. The data from all the table rows is placed into a two-dimensional array. The dimensions and size of the array are determined by the number of rows and columns in the table.
init: /* Open the table and create the array. */ DCL num arr(*,*) rc; dsid = open('work.a'); nlobs = attrn(dsid, 'NLOBS'); nvars = attrn(dsid, 'NVARS'); arr = makearray(nlobs,nvars); /* Move the contents of the table into the array. */ do i = 1 to dim(arr, 1); rc = fetch(dsid); do j = 1 to dim(arr, 2); arr[i,j] = getvarn(dsid, j); end; end; /* Close the table and delete the array. */ call close(dsid); rc = delarray(arr); return;
See Also |
DIM, HBOUND, and LBOUND in SAS Language Reference: Dictionary
Copyright © 2009 by SAS Institute Inc., Cary, NC, USA. All rights reserved.