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

Hi,

How to grow a sublist of the existing List in proc iml?

 

for eg. 

proc iml;

Variable = ListCreate();
Residual = ListCreate();
Call ListAddItem(Variable,{1 2 34});
call ListAddItem(Variable,{12 4 2 2});
Call ListAddItem(Residual,{1 2 34});
call ListAddItem(Residual,{12 4 2 2});

All = ListCreate({"Variable","Residual"});
call ListSetItem(All,"Variable",Variable);
call ListSetItem(All,"Residual",Residual);
package load ListUtil; /* load ListPrint module */
run ListPrint(All);

 

In the above program, I have a named sublist Variable inside the List All. There is LISTGETSUBITEM function to get the subitem but there is no support to add an item to the sub list or to grow it dynamically. This is useful in cases where all the elements of a sublist are not available right in the beginning and want to maintain a structure like above.

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

OK, then use ListAddItem in my previous example, which will add it to the end of the list.

 

If you need to do this dynamically, you can use the sequence "GetSubItem", "AddItem", "SetSubItem". To make it more efficient, my colleague suggested using the 'm' flag, which does not require making a copy of the memory:

 

tmp = ListGetSubItem(All, "Residual", 'm');    /* move sublist w/out making a copy */
call ListAddItem(tmp, {10 5 100});             /* add new item */
call ListSetSubItem(All,"Residual", tmp, 'm'); /* move memory back to All; tmp is null */

 

 

View solution in original post

3 REPLIES 3
Rick_SAS
SAS Super FREQ

You can use

 

call ListInsertItem(Residual,3, 1:3); /* insert */
call ListSetItem(All,"Residual",Residual); /* replace */

karthick_gopal
Calcite | Level 5
For this to work, I have to know the elements of Residual before hand. What
I am looking for is something like this.

List All contains Residual sublist (initial with 2 items in the sublist)
and let's say I want to add items to Residual sublist in a loop. This is
what i meant by growing the sublist.

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Rick_SAS
SAS Super FREQ

OK, then use ListAddItem in my previous example, which will add it to the end of the list.

 

If you need to do this dynamically, you can use the sequence "GetSubItem", "AddItem", "SetSubItem". To make it more efficient, my colleague suggested using the 'm' flag, which does not require making a copy of the memory:

 

tmp = ListGetSubItem(All, "Residual", 'm');    /* move sublist w/out making a copy */
call ListAddItem(tmp, {10 5 100});             /* add new item */
call ListSetSubItem(All,"Residual", tmp, 'm'); /* move memory back to All; tmp is null */

 

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 3 replies
  • 1059 views
  • 0 likes
  • 2 in conversation