I want to build a list containing multiple subitems at the same level so that I have what is equivalent to an array of lists. The code is:
proc iml ; package load listutil ;
L = [] ;
L = L || [ #'1'=[] ] ;
L$1 = [ #'1.1'=[] ] ;
call listprint( L$'1'$'1.1' ) ;
L$1 = L$1 || [ 1, #index=1 ] ;
L$1 = L$1 || [ 2, #index=2 ] ;
L$1 = L$1 || [ 2, #index2=2 ] ;
call struct(L) ;
quit ;
proc iml ; package load listutil ;
L = [] ;
L = L || [ #'1'=[] ] ;
L$1 = [ #'1.1'=[] ] ;
call listprint( L$'1'$'1.1' ) ;
L$1 = L$1 || [ 1, #index=1 ] ;
L$1 = L$1 || [ 2, #index=2 ] ;
L$1 = L$1 || [ 2, #index2=2 ] ;
call struct(L) ;
quit ;
proc iml ; package load listutil ;
L = [] ;
L = L || [ #'1'=[] ] ;
L$1 = [ #'1.1'=[] ] ;
call listprint( L$'1'$'1.1' ) ;
L$1 = L$1 || [ 1, #index=1 ] ;
L$1 = L$1 || [ 2, #index=2 ] ;
L$1 = L$1 || [ 2, #index2=2 ] ;
L$1 = L$1 || [ 2, 3 ] ;
L$1 = L$1 || [ 2, 3 ] ;
call struct(L) ;
quit ;
The log displays an error:
72 L = [] ;
73 L = L || [ #'1'=[] ] ;
74 L$1 = [ #'1.1'=[] ] ;
75 call listprint( L$'1'$'1.1' ) ;
76 L$1 = L$1 || [ 1, #index=1 ] ;
77 L$1 = L$1 || [ 2, #index=2 ] ;
ERROR: Conflicting item names were found in the list concatenation.
operation : || at line 77 column 11
operands : _TEM1001, _TEM1002
_TEM1001 3 items (list)
_TEM1002 2 items (list)
statement : ASSIGN at line 77 column 1
78 L$1 = L$1 || [ 2, #index2=2 ] ;
79 call struct(L) ;
Note that the same operation in line 78 is acceptable even though the first item in the right argument to the concatenation operator, 2, is the same as the first item in the statement on line 77. The named items, 'index' and 'index2', are different. Furthermore, if the example is extended to
proc iml ; package load listutil ;
L = [] ;
L = L || [ #'1'=[] ] ;
L$1 = [ #'1.1'=[] ] ;
call listprint( L$'1'$'1.1' ) ;
L$1 = L$1 || [ 1, #index=1 ] ;
L$1 = L$1 || [ 2, #index=2 ] ;
L$1 = L$1 || [ 2, #index2=2 ] ;
L$1 = L$1 || [ 2, 3 ] ;
L$1 = L$1 || [ 2, 3 ] ;
call struct(L) ;
quit ;
then we will observe that the last two concatenations are accepted even though the items [ 2, 3 ] are identical. Why are identical numeric arguments accepted for list items but named items are required to be distinguished?
Named items are used to emulate associative arrays. An associative array is a collection of (key, value) pairs where each key appears at most once. Notice that the VALUES can be repeated, but each key must be unique.
Why should names be unique? Because when you ask for
x = ListGetItem(L, 'joe');
you want to get ONE item, the one named 'joe'.
Your program is complicated. A simpler example is
L = [#MyKey=1, #MyKey=2];
which gives the error
ERROR: The name MyKey conflicts with an existing name used in the list.
Named items are used to emulate associative arrays. An associative array is a collection of (key, value) pairs where each key appears at most once. Notice that the VALUES can be repeated, but each key must be unique.
Why should names be unique? Because when you ask for
x = ListGetItem(L, 'joe');
you want to get ONE item, the one named 'joe'.
Your program is complicated. A simpler example is
L = [#MyKey=1, #MyKey=2];
which gives the error
ERROR: The name MyKey conflicts with an existing name used in the list.
Thank you, Rick! I didn't expect to get a reply so soon, since it is Sunday morning, but your response came as a pleasant surprise 😊
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.