Language Reference
LISTSETITEM Call
CALL LISTSETITEM (list, position, v <, flag> ) ;
This subroutine is supported by the IML procedure and the iml action.
The ListSetItem subroutine sets the value of one or more existing list items. For examples and a general discussion of using lists, see Chapter 9, Lists and Data Structures.
The subroutine takes the following arguments:
- list
specifies an existing list.
- position
specifies a matrix of integer values or character values that specify the positions of the items to be set. You can also specify a list, which enables you to mix names and indices. Any existing items at the specified locations are overwritten.
- v
specifies a SAS/IML variable or literal that will become a new list item. The value of v can be a matrix, table, list, or any other valid SAS/IML type, including an empty matrix.
- flag
-
controls what happens to the v symbol. Valid values are:
- 'c'
copies the data in v into the list. The v symbol is unchanged by the call. This is the default behavior.
- 'm'
moves the data in v into the list. The v symbol is freed after the call.
The following statements use the ListCreate subroutine to allocate a list that has three items. The ListLen function is used in the DO loop to iterate over every item in the list. The ListSetItem subroutine is used to set the nth item to an matrix.
/* create a list of matrices; use ListSetItem to fill */
L = ListCreate(3); /* allocate array of 3 items */
do n = 1 to ListLen(L); /* for each item in array */
A = j(n, n, n-1); /* define n x n matrix */
call ListSetItem(L, n, A); /* assign n_th item of L */
end;
You can also specify a vector for the position argument, in which case the specified value is copied to multiple locations. For example, to set every item of a list to the value 0, use the following statement:
call ListSetItem(L, 1:ListLen(L), 0); /* assign all items of L */
You can use the list item operator ($) to set an item. For example, the following syntax is an alternative way to assign items in a loop:
do n = 1 to ListLen(L); /* for each item in array */
L$n = j(n, n, n-1); /* assign n x n matrix */
end;