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

The following little program assigns column names to a row vector mu_vec.  However, if this processing is carried out within a function, then the command "return(mu_vec);" will get rid of the column names.  Is there a way to preserve these column names? 

assetNames={"MSFT" "NORD" "SBUX"};
mu_vec={0.042 0.0015 0.028};
mattrib mu_vec c=assetNames;
print mu_vec;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

I assume that you are trying something like the following:

 

start Func(x);
   assetNames={"MSFT" "NORD" "SBUX"};
   mu_vec={0.042 0.0015 0.028};
   mattrib mu_vec c=assetNames;
   return mu_vec;
finish;

y = Func(x);  /* copies values but not atrtributes */
print y;

This doesn't work the way you hoped because inside the FUNC module, the variable X is a local variable. Any attributes you assign as assigned to the local variable and are not copied to the variable Y, which has a different scope. (See this article to learn more about the scope of variables in the SAS/IML language.)

 

If you want to give Y attributes, you need to assign them in the scope for Y. If the column names are known inside the function, you can return them (as part of a list) from the function. You can then call the MATTRIB statement in the parent environment. like this:

 

start Func2(x);
   assetNames={"MSFT" "NORD" "SBUX"};
   mu_vec={0.042 0.0015 0.028};
   return ( [mu_vec, assetNames] );  /* return a list */
finish;

L = Func2(x);      /* output is a list with two items */
y = L$1;           /* values are the first item in list */
mattrib y c=(L$2); /* column names are the second item */
print y;

View solution in original post

6 REPLIES 6
Rick_SAS
SAS Super FREQ

I assume that you are trying something like the following:

 

start Func(x);
   assetNames={"MSFT" "NORD" "SBUX"};
   mu_vec={0.042 0.0015 0.028};
   mattrib mu_vec c=assetNames;
   return mu_vec;
finish;

y = Func(x);  /* copies values but not atrtributes */
print y;

This doesn't work the way you hoped because inside the FUNC module, the variable X is a local variable. Any attributes you assign as assigned to the local variable and are not copied to the variable Y, which has a different scope. (See this article to learn more about the scope of variables in the SAS/IML language.)

 

If you want to give Y attributes, you need to assign them in the scope for Y. If the column names are known inside the function, you can return them (as part of a list) from the function. You can then call the MATTRIB statement in the parent environment. like this:

 

start Func2(x);
   assetNames={"MSFT" "NORD" "SBUX"};
   mu_vec={0.042 0.0015 0.028};
   return ( [mu_vec, assetNames] );  /* return a list */
finish;

L = Func2(x);      /* output is a list with two items */
y = L$1;           /* values are the first item in list */
mattrib y c=(L$2); /* column names are the second item */
print y;
kgwet
Fluorite | Level 6
I love this solution. I didn't know about the list feature.
Thanks a lot.
Ksharp
Super User

Dr. Rick has said very clear. 

Or could pass local matrix into global matrix ,if your IML is low version.

 

proc iml;

start Func(x) global(mu ,asset );
   assetNames={"MSFT" "NORD" "SBUX"};
   mu_vec={0.042 0.0015 0.028};
   mattrib mu_vec c=assetNames;
   mu=mu_vec ;asset=assetNames;
   return mu_vec;
finish;

y = Func(x);  /* copies values but not atrtributes */
mattrib mu c=asset l='';
print y , mu ;

quit;

Ksharp_0-1624366596428.png

 

kgwet
Fluorite | Level 6
Thanks for your suggestion. Would global passing of a matrix overwrite a matrix of the same name in the parent program?
Ksharp
Super User
Yes. It is global. It would be overwrited if there are same name in parent program.
But you could define a unique name to avoid it .

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 6 replies
  • 809 views
  • 5 likes
  • 3 in conversation