BookmarkSubscribeRSS Feed
Bogdan
Calcite | Level 5

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.

3 REPLIES 3
Rick_SAS
SAS Super FREQ

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 */

Bogdan
Calcite | Level 5

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.

Rick_SAS
SAS Super FREQ

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

Count missing values in observations - The DO Loop

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 3 replies
  • 1245 views
  • 6 likes
  • 2 in conversation