Your confusion is caused because the brackets ([...]) are used both for subscript operations and to define lists. If you want to extract the 1st and 2nd items in a list, use the subscript operator:
idx = 1:2;
a = e$'cols'[idx]; /* subscripts, not a list */
call listprint ( a ) ;
With regard to your meta-question ("extracting the data is inelegant"), I suggest that you might benefit by rethinking your data structure. Here are a few concrete suggestions:
- Don't make everything in 'e' a list. If a component contains homogeneous elements (such as 'type' and 'name'), make it a vector or matrix.
- Store the elements of 'cols' as column vectors to eliminate having to call COLVEC every time you retrieve a column
- Use the TableAddVar function to construct a table. Your current method will fail if you have both numerical and character data.
- I prefer CALL STRUCT to CALL LISTPRINT. It provides a nice summary of the structure of the list and its items and subitems.
The following program might give you some ideas to play with:
proc iml ;
package load listutil ;
/* if data are homogeneous, use a matrix instead of a list */
e = [ #'nobs' =3
, #'nvars'=3
, #'type' ={'N' 'N' 'N'}
, #'name' ={'var1' 'var2' 'var3'}
, #'cols' =[{ 11, 21, 31 }, { 12, 22, 32 }, {A, B, C}]
] ;
call struct( e ) ;
/* to create a table from names and columns, use TableAddVar */
varIdx = {1 3};
tbl = TableCreate();
do i = 1 to ncol(varIdx);
k = varIdx[i];
call TableAddVar(tbl, e$'name'[k], e$'cols'$k);
end;
call TablePrint( tbl ) ;