BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
rbettinger
Lapis Lazuli | Level 10

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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.

View solution in original post

4 REPLIES 4
Rick_SAS
SAS Super FREQ

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.

rbettinger
Lapis Lazuli | Level 10

I was not aware of the parentname() function. It does exactly what I want! Thanks, Rick.

rbettinger
Lapis Lazuli | Level 10
Very helpful references. Thanks, Rick!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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