## ListGetItem Command-Type of matrix error

When I try to use the Listgetitem command I get the following error:                                 Note:(subs is the list name)

8807 do j=1 to numSubs;
8808
8809 if IsEmpty(subs[j]) then do;
8809! ps0vector=1;
8809! end;
8810 else do;
8811 d=ListGetItem(subs, j);
8812 print d;
8813 end;
8814 end;
8815 end;
8816 end;
NOTE: Module ISEMPTY loaded from the storage SASHELP.IMLMLIB.
ERROR: Matrix d must be numeric or character.

statement : PRINT at line 8812 column 1
8817 run;
NOTE: Module MAIN is undefined in IML; cannot be RUN.

I'm not sure how to solve this?

4 REPLIES 4

## Re: ListGetItem Command-Type of matrix error

The ISEMPTY function tests whether a matrix has zero rows and zero columns. The error message says that subs[j] is not a matrix, which is true because the sublist operator ([ ]) always returns a list.  I don't know what you are trying to do, but here are two options:

1. You can use the LISTLEN function to test whether a list has zero elements:

IF LISTLEN(subs[j])=0 THEN...

2. You can extract the j_th element from a list by using the list item operator (\$):

IF IsEmpty(subs\$j) THEN...

## Re: ListGetItem Command-Type of matrix error

Hi: The error I was referring to was the one about matrix d being numeric or character.

Here's some more code to describe the problem better and the error message.

subs=listcreate(numNzs);

3405  do j=1 to numSubs;
3406
3407  if listlen(subs[j])=0 then do;
3407  ps0vector=1;
3407 end;
3408 else do;
3409 d=ListGetItem(subs,j);
3410 print d;
3412 end;
3413 end;
ERROR: Matrix d must be numeric or character.

## Re: ListGetItem Command-Type of matrix error

Ahh, sorry. The error is that the PRINT statement in PROC IML (15.1) prints valid matrices. Apparently D is not a matrix for some value of j.  You can use the LISTPRINT modules (or the STRUCT module) to print lists.

If the list contains some elements that are matrices and others that are lists, you can use the TYPE function to determine how to print the item:

``````proc iml;
subs = [{A B C},    /* subs\$1 is a vector */
{1 2, 3 4},  /* subs\$2 is a matrix */
[4, 5:6]];   /* subs\$3 is a list */call ListPrint(subs);  /* print the entire list */
/* or go through item by item and decide what to do */
do j = 1 to ListLen(subs);
d = ListGetItem(subs,j);
print j;
if type(d)='N' | type(d)='C' then
print d;
else if type(d)='L' then
call ListPrint(d);
end;
``````

For more about lists and printing, see p 7-8 of Wicklin (2017), and also the question on p. 19.

## Re: ListGetItem Command-Type of matrix error

Has this issue been resolved? If so, please close it. Otherwise, let us know what other questions you might have.

From The DO Loop