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

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
Pyrite | Level 9

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 😊

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Multiple Linear Regression in SAS

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.

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