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

Hi IML enthusiasts!

Well this is a question that came to my mind as I was refreshing my IMLPlus knowledge last week. I tried to write a simple recursive module, but I couldn't make it work. To my surprise, I discovered in the IML documentation that it is not permitted to defines modules that call themselves. That is, if you define and execute a module such as:

*Factorial using recursive IML module;

start fact(n);

      if n=1 then return 1;

      else return n*fact(n-1);

finish;

x=fact(3);


You will get this error message:

»ERROR: Module FACT called again before exit from prior call.

ERROR: An error occurred while executing module "fact". (25, 7)

An error in the program occurred on the SAS server. (+3, 20)


I tried the following recursive implementation using macros. It is not very user-friendly, but it works:

*1) Defining a recursive factorial using macros;

submit;

      %macro fact(n);

            %if &n=1 %then 1;

            %else %eval(&n*%fact(%eval(&n-1)));

      %mend fact;

endsubmit;

*2) Calling macro and retrieving results using SYMPUT, SYMGET...;

results = J(10,2,'@@@@@@@@');

mattrib results colname={"n" "factorial(n)"};

do n=1 to 10;

      call symput("n",char(n));

      @%let fact_n = %fact(&n);

      results[n,1] = char(n,2);

      results[n,2] = symget("fact_n");  

end;

print results;


However, the factorial is a toy problem and I don't know if the same procedure would apply to more elaborated functions. I couldn't make the Fibonacci works, not to mention e.g. the Ackermann function. I am pretty sure there may be more elegant alternatives to write recursive functions within IML/IMLPlus. What do you think?

Best regards,

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

>  I am pretty sure there may be more elegant alternatives to write recursive functions within IML/IMLPlus.

You are correct thatthe SAS/IML languagte does not support recursion. When possible, implementing a recursive definition by using iteration results in a more efficient program with less computational overhead.  Both factorial and Fibomacci are implemented efficiently in IML by using iteration. (I am not familiar with the Ackermann function.)

I have never had a recursive funcion that I was not able to implement iteratively, so I have not searched for a way around this limitation. It occurs to me, however, that IMLPlus supports Java classes, and Java supports recursion, so if you really need recursion then read the IMLPlus documentation example that shows how to define and use Java classes.

View solution in original post

1 REPLY 1
Rick_SAS
SAS Super FREQ

>  I am pretty sure there may be more elegant alternatives to write recursive functions within IML/IMLPlus.

You are correct thatthe SAS/IML languagte does not support recursion. When possible, implementing a recursive definition by using iteration results in a more efficient program with less computational overhead.  Both factorial and Fibomacci are implemented efficiently in IML by using iteration. (I am not familiar with the Ackermann function.)

I have never had a recursive funcion that I was not able to implement iteratively, so I have not searched for a way around this limitation. It occurs to me, however, that IMLPlus supports Java classes, and Java supports recursion, so if you really need recursion then read the IMLPlus documentation example that shows how to define and use Java classes.

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
  • 1 reply
  • 1837 views
  • 0 likes
  • 2 in conversation