In an IML procedure I have a matrix with named columns.
proc iml;
myMatrix = {1 2 3, 1 4 9};
mattrib myMatrix colname={"a", "b", "c"};
print myMatrix;
print (myMatrix[,"a"]);
/* load module = myModule;*/
/* run myModule(myMatrix);*/
run;
I can easily access and print the columns by name. However, when I pass the matrix to a user defined module, the column names inside the module disappear (to run the module uncomment the lines in previous proc iml)
proc iml;
start myModule(MatrixWithHeader);
print MatrixWithHeader;
print (MatrixWithHeader[,"a"]);
finish myModule;
store module=myModule;
run;
I got the following error:
ERROR: (execution) Character argument should be numeric.
How can I access the matrix columns in the module by their names?
Access by column number will make the code inflexible. Possible workaround might be to pass the vector of column names as an argument and run a mattrib inside the module. However, repeat of mattrib is cumbersome and in this case I will need to extract colnames from the myMatrix, since it is defined by a long piece of code (not like in example), and the vector of names is not available.
Thanks in advance,
Alex
SOLVED
Thanks for the solution.
I think the only solution is to use a GLOBAL clause and access the original matrix that has the attributes. Even though SAS/IML passes arguments by reference, it does not preserve the MATTRIB assignments (Like you, I noticed this only recently.) Furthermore, there is no function that you can call to return the matrix attributes (in particular, the column names), so you cannot extract the column names from a matrix and then pass the names into a module as another parameter.