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

I am interested in parsing a module's parameters for parameter type and  name. I get expected results from the type function, but the parentname function demonstrates unexpected results. Here is a test module that I have written to present my findings:

proc iml ;
start test( p1, p2, p3, p4, p5=, p6=, p7= ) ;
name1 = parentname( 'p1' ) ;
name2 = parentname( 'p2' ) ;
name3 = parentname( 'p3' ) ;
name4 = parentname( 'p4' ) ;
name5 = parentname( 'p5' ) ;
name6 = parentname( 'p6' ) ;
name7 = parentname( 'p7' ) ;

if isEmpty( p5 )
then print p1 name1 p2 name2, p3 name3 p4 name4 ;
else print p1 name1 p2 name2, p3 name3 p4 name4, p5 name5 p6 name6, p7 name7 ;
finish ;

ab = 'ab' ;
cd = 'cd' ;
ef = 'ef' ;
gg = { ab ab cd cd ef ef gg } ;

print 'Test case 1: All parameter names reported correctly.' ;
run test( ab, cd, ef, gg ) ;

print 'Test case 2: Second occurrence of duplicated names, e.g., ( ab, ab ) reported as blank',
'e.g., name[2,4] ought to be "ab", "cd" and not blank.' ;
run test( ab, ab, cd, cd ) ;

print 'Test case 3: Attempt to teach IML to report Elizabethan sonnet rhyme scheme is futile',
'e.g., name[2,4,6] are all blank and not "ab", "cd", and "ef".' ;
run test( ab, ab, cd, cd, ef, ef, gg ) ;

quit ;

Here is the output from the program:

Test case 1: All parameter names reported correctly.
p1 	name1 	p2 	name2
ab 	ab 	cd 	cd
p3 	name3 	p4 	  	name4
ef 	ef 	AB 	AB 	CD 	CD 	EF 	EF 	GG 	gg
Test case 2: Second occurrence of duplicated names, e.g., ( ab, ab ) reported as blank
e.g., name[2,4] ought to be "ab", "cd" and not blank.
p1 	name1 	p2 	name2
ab 	ab 	ab 	 
p3 	name3 	p4 	name4
cd 	cd 	cd 	 
Test case 3: Attempt to teach IML to report Elizabethan sonnet rhyme scheme is futile
e.g., name[2,4,6] are all blank and not "ab", "cd", and "ef".
p1 	name1 	p2 	name2
ab 	ab 	ab 	 
p3 	name3 	p4 	name4
cd 	cd 	cd 	 
p5 	name5 	p6 	name6
ef 	ef 	ef 	 
p7 	  	name7
AB 	AB 	CD 	CD 	EF 	EF 	GG 	gg

Can someone explain parentname's perplexing performance?

Thanks in advance,

Ross

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

Arguments to modules are passed by reference, not by value.  This means that the module can change the value of an argument. Because of this, special care is taken if the programmer passes in the same symbol for two different arguments.  For example, consider the following example:

 

start foo(x, y);
   x = 3;
   y = 4;
finish;

A = 1;
run foo(A, A);
print A;

What is the value of A after calling the module? Remember, A was passed to both arguments! Is the value 3 or 4? Should the answer depend on the order of the statements in the body of the module?

 

IML resolves this ambiguity by passing A into the first argument and a COPY of A into the second argument. So the result is similar to if you were to call the module as 

run foo(A, A+0);   /* second arg is a temporary var */

 

This rule is based on "pass by reference," but it affects PARENTNAME. The first argument has a parentname (which is 'A'), and the second argument is a temporary variable, so PARENTNAME returns ' ' [blank].

View solution in original post

2 REPLIES 2
Rick_SAS
SAS Super FREQ

Arguments to modules are passed by reference, not by value.  This means that the module can change the value of an argument. Because of this, special care is taken if the programmer passes in the same symbol for two different arguments.  For example, consider the following example:

 

start foo(x, y);
   x = 3;
   y = 4;
finish;

A = 1;
run foo(A, A);
print A;

What is the value of A after calling the module? Remember, A was passed to both arguments! Is the value 3 or 4? Should the answer depend on the order of the statements in the body of the module?

 

IML resolves this ambiguity by passing A into the first argument and a COPY of A into the second argument. So the result is similar to if you were to call the module as 

run foo(A, A+0);   /* second arg is a temporary var */

 

This rule is based on "pass by reference," but it affects PARENTNAME. The first argument has a parentname (which is 'A'), and the second argument is a temporary variable, so PARENTNAME returns ' ' [blank].

rbettinger
Pyrite | Level 9

Thanks, Rick. You have explained my perplexity perfectly. This topic 1) explains some of the mechanism of IML parameter passing works and 2) would make a great exam question!

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
  • 2 replies
  • 885 views
  • 5 likes
  • 2 in conversation