SAS DIM function – Counting the elements in an array

This post was kindly contributed by AFHood Analytics Group - Blogs » SAS - go there to comment and to read the full post.

The DIM function returns the number of literal elements in an array. It functions against multi-dimensional arrays as well as one-dimensional arrays.

1-dimensional array example

DIM(array_name)

Multi-dimensional array examples

DIM(m_array) -> returns the number of elements in the first dimension of the array

DIM5(m_array) -> returns the number of elements in the 5th dimension of the array

DIM(m_array, 5) -> returns the number of elements in the 5th dimension of the array

The classic use case for the DIM function is to return the number of elements in an array for the upper bound of a do loop process. Example:

array array_name(5) var1 var2 var3 var4 var5;

do i=1 to dim(array_name);

some SAS statements here

end;

This post was kindly contributed by AFHood Analytics Group - Blogs » SAS - go there to comment and to read the full post.