Contributor
Posts: 20

# Matrix with arbitrary (open) dimension

I might create a matrix through a process, but I don't know its dimension beforehand. As far as I know, you can't define a matrix with some temporarily open size, right?

If so, what are some ideas to adjust for this issue?

One way is to define a matrix with sufficiently big size. Then afterwards trim by missing values or reduce the size based on some counter. For example, if you work out a matrix from other one, you allow the size based on the original matrix.

SAS Super FREQ
Posts: 4,242

## Re: Matrix with arbitrary (open) dimension

Right. An example of this situation is in this article:  Efficient acceptance-rejection simulation - The DO Loop

Some people append to a matrix within a loop (see Pre-allocate arrays to improve efficiency - The DO Loop), but that is inefficient so I try to allocate the matrix as large as it might be, and then fill only the rows that are actually used. As you say, trim the rest, like this:

x = j(100, 1, .); /* use missing values to mark unused rows */

do i = 1 to 10;

/* do something that fills 85% of the rows */

end;

x = remove(x, loc(x=.));  /* remove and missing values */

Contributor
Posts: 20

## Re: Matrix with arbitrary (open) dimension

About your program mr. Wicklin, how can you remove rows based on the missing values occurring in a column?

The program you showed works for vectors, i.e. matrices of dimension  n x 1.

SAS Super FREQ
Posts: 4,242

## Re: Matrix with arbitrary (open) dimension

See this article on how to remove rows that have a missing values in any variable:

Count missing values in observations - The DO Loop

Discussion stats
• 3 replies
• 401 views
• 6 likes
• 2 in conversation