BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rbettinger
Lapis Lazuli | Level 10

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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 
= 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.


View solution in original post

2 REPLIES 2
Rick_SAS
SAS Super FREQ

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 
= 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.


rbettinger
Lapis Lazuli | Level 10

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 😊

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 2 replies
  • 1033 views
  • 2 likes
  • 2 in conversation