Chapter Contents |
Previous |
Next |
SAS Component Language: Reference |
There are two ways to copy an array:
Using Assignment Statements |
You can assign values to an array from another array in an assignment statement. For example, the following code copies the values of array A into array B:
declare num a[3] = (1 3 5); declare num b[3]; b = a; put b;These statements produce the following output:
b[1] = 1 b[2] = 3 b[3] = 5
When you use the assignment statement to copy an array, the two arrays must have the same type, dimensions, and size; otherwise, an error condition will occur.
You can use an assignment statement to create and initialize a dynamic array that has been declared but not yet created. If you specify a newly declared dynamic array as the array to which values are to be assigned, then SCL will create the dynamic array and copy the values of the existing array into the new array.
For example, the following statements create dynamic array B to the same size as A and then copies the values of array A into dynamic array B.
declare num a[3] = (1 3 5); declare num b[*]; b = a; put b;These statements produce the following output:
b[1] = 1 b[2] = 3 b[3] = 5
Using The COPYARRAY Function |
You can also use the COPYARRAY function to copy elements from one array to another. By default, the COPYARRAY function produces the same result as the assignment statement and requires that the arrays be of the same type, dimension, and size. For example, the following statements copy the array A into arrays B and C:
declare num a[3] = (1 3 5); declare num b[3] c[3]; rc = COPYARRAY(a,b); put b; c = a; put c;The output for this code would be:
b[1] = 1 b[2] = 3 b[3] = 5 c[1] = 1 c[2] = 3 c[3] = 5
However, with the COPYARRAY function, you can copy an array to an array
of a different size if you set the IGNORESIZE parameter to Y
in the call to COPYARRAY:
rc = COPYARRAY(array1,array2,'Y');The type and dimensions of the arrays must still match. For example, the following statements will copy array A, which has three elements, into array B, which has five elements.
declare num a[3] = (1 3 5); declare num b[5]; rc = COPYARRAY(a,b,'Y'); put b;This code produces the following output:
b[1] = 1 b[2] = 3 b[3] = 5 b[4] = . b[5] = .
The COPYARRAY can also be used to create dynamic arrays, just as you can create them using assignment statements. For example, the following statements create and initialize dynamic array B:
declare num a[3] = (1 3 5); declare num b[*]; rc = COPYARRAY(a,b); put b;The output for this code would be:
b[1] = 1 b[2] = 3 b[3] = 5
Note: When you use the COPYARRAY function to create a new dynamic array,
it is good practice to delete the newly created array using the DELARRAY function.
However, if you do not delete the array with the DELARRAY function, SCL will
delete the array at the end of the routine like all other dynamic arrays.
See Deleting Dynamic Arrays
for more information.
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.