Is there a SAS/IML equivalent to the DATA step vname() function?
For the DATA step, I can write
data a ;
a = 1 ; b = 2 ; c = 3 ; output ;
run ;
data names ;
set a ;
name_1 = vname( a ) ; name_2 = vname( b ) ; name_3 = vname( c ) ; output ;
run ;
and the dataset names contains the names of the variables.
Now, for SAS/IML, suppose that I have only IML matrices whose names I want to use as arguments to other modules.. I want to create a module called parm_names that will extract the names of the variables passed as parameters:
proc iml ;
start parm_names( p1, p2, p3 ) ;
/* what I want is the equivalent of vname() here */
name_1 = IML_equivalent_to_vname( p1 ) ;
name_2 = IML_equivalent_to_vname( p2 ) ;
name_3 = IML_equivalent_to_vname( p3 ) ;
return ( name_1 || name_2 || name_3 ) ;
finish ;
parm_name_vector = parm_names( a, b, c ) ;
/* additional IML code to use the variable names */
quit ;
SAS Tech Support suggested:
proc iml;
use a;
read all into a[colname=cname];
print cname a;
quit;
but that solution presumes that the names are already available in the program data buffer of a SAS dataset, while in my IML module, I am supplying the variables as IML module parameters.
Does a SAS/IML equivalent to the DATA step function vnames() exist?
Thanks for your help.
If your goal is to obtain the name of the variable that is sent into a module, you can use the PARENTNAME function:
proc iml ;
start parm_names( p1, p2, p3 ) ;
name_1 = parentname( "p1" ) ;
name_2 = parentname( "p2" ) ;
name_3 = parentname( "p3" ) ;
return ( name_1 || name_2 || name_3 ) ;
finish ;
parm_name_vector = parm_names( a, b, c ) ;
print parm_name_vector;
But be aware that this is NOT the equivalent of the VNAME function. The VNAME function gets the name of the variable that corresponds to the i_th element of a variable array. I suppose the equivalent of that is to obtain the name of the i_th variable for a matrix, which is presumably why Tech Support sent you that snippet.
If your goal is to obtain the name of the variable that is sent into a module, you can use the PARENTNAME function:
proc iml ;
start parm_names( p1, p2, p3 ) ;
name_1 = parentname( "p1" ) ;
name_2 = parentname( "p2" ) ;
name_3 = parentname( "p3" ) ;
return ( name_1 || name_2 || name_3 ) ;
finish ;
parm_name_vector = parm_names( a, b, c ) ;
print parm_name_vector;
But be aware that this is NOT the equivalent of the VNAME function. The VNAME function gets the name of the variable that corresponds to the i_th element of a variable array. I suppose the equivalent of that is to obtain the name of the i_th variable for a matrix, which is presumably why Tech Support sent you that snippet.
I was not aware of the parentname() function. It does exactly what I want! Thanks, Rick.
See also The name of a parameter in the parent environment - The DO Loop (sas.com)
And all SAS IML programmers should read Everything you wanted to know about writing SAS/IML modules - The DO Loop
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.