## Conditionally Delete Columns or Rows from Matrix

Solved
Occasional Contributor
Posts: 13

# Conditionally Delete Columns or Rows from Matrix

I am looking for a way to delete all observations from a matrix based on conditions.  For example, if I produced a simulation but subsequently wanted to delete all resulting rows that are less than 0 or greater than 1, how would I do that in IML?

This is easy to do in a DATA step, so I can do that, but I was curious regarding an IML approach.  (It is probably discussed in Dr. Wicklin's text "Statistical Programming with SAS/IML Software," which I have and will examine, but I thought I would post the question here as it may be a more efficient way to find the answer.

Thanks

Brian

proc iml;

A = {0.50 0.50 0.50, -1 0.50 0.50, 2 0.50 0.50|;

B = matrix with rows that contain negative entries or entries greater than 1 removed

run;

The resulting matrix B should consist of {0.50 0.50 0.50} only.

Accepted Solutions
Solution
‎01-30-2015 02:16 PM
SAS Super FREQ
Posts: 4,243

## Re: Conditionally Delete Columns or Rows from Matrix

I suggest modifying the technique in the article Removing Observations with Missing Values - The DO Loop

C = (A<0 | A>1);         /* matrix of zeros and ones */

count = c[,+];           /* add across rows */

keepIdx = loc(count=0);  /* rows where all elements are in [0,1] */

if ncol(keepIdx)>0 then

B = A[keepIdx,];

else print "All rows of A are invalid";

All Replies
Solution
‎01-30-2015 02:16 PM
SAS Super FREQ
Posts: 4,243

## Re: Conditionally Delete Columns or Rows from Matrix

I suggest modifying the technique in the article Removing Observations with Missing Values - The DO Loop

C = (A<0 | A>1);         /* matrix of zeros and ones */

count = c[,+];           /* add across rows */

keepIdx = loc(count=0);  /* rows where all elements are in [0,1] */

if ncol(keepIdx)>0 then

B = A[keepIdx,];

else print "All rows of A are invalid";

🔒 This topic is solved and locked.