<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Recursive modules in SAS/IML? in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Recursive-modules-in-SAS-IML/m-p/159531#M1468</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&amp;gt;&amp;nbsp; I am pretty sure there may be more elegant alternatives to write recursive functions within IML/IMLPlus.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp; Both factorial and Fibomacci are implemented efficiently in IML by using iteration. (I am not familiar with the Ackermann function.)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 28 Jun 2014 21:04:14 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2014-06-28T21:04:14Z</dc:date>
    <item>
      <title>Recursive modules in SAS/IML?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Recursive-modules-in-SAS-IML/m-p/159530#M1467</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi IML enthusiasts!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10pt; font-family: 'Courier New'; color: #339966;"&gt;*Factorial using recursive IML module;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;SPAN style="color: #3366ff;"&gt;start&lt;/SPAN&gt; fact(n);&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;SPAN style="color: #3366ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if&lt;/SPAN&gt; n=1 &lt;SPAN style="color: #3366ff;"&gt;then return&lt;/SPAN&gt; 1;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;SPAN style="color: #3366ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else return&lt;/SPAN&gt; n*fact(n-1);&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;SPAN style="color: #3366ff;"&gt;finish&lt;/SPAN&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;x=fact(3);&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;You will get this error message:&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;STRONG&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New'; color: #c00000;"&gt;»ERROR: Module FACT called again before exit from prior call.&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;ERROR: An error occurred while executing module "fact". &lt;/SPAN&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;(25, 7)&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;An error in the program occurred on the SAS server. (+3, 20)&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;I tried the following recursive implementation using macros. It is not very user-friendly, but it works:&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10pt; font-family: 'Courier New'; color: #339966;"&gt;*1) Defining a recursive factorial using macros;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;SPAN style="color: #3366ff;"&gt;submit&lt;/SPAN&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;SPAN style="color: #3366ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %macro&lt;/SPAN&gt; fact(n);&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;SPAN style="color: #3366ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %if&lt;/SPAN&gt; &amp;amp;n=1 &lt;SPAN style="color: #3366ff;"&gt;%then&lt;/SPAN&gt; 1;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;SPAN style="color: #3366ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %else %eval&lt;/SPAN&gt;(&amp;amp;n*%fact(&lt;SPAN style="color: #3366ff;"&gt;%eval&lt;/SPAN&gt;(&amp;amp;n-1)));&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;SPAN style="color: #3366ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %mend&lt;/SPAN&gt; fact;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;SPAN style="color: #3366ff;"&gt;endsubmit&lt;/SPAN&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10pt; font-family: 'Courier New'; color: #339966;"&gt;*2) Calling macro and retrieving results using SYMPUT, SYMGET...;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;results = J(10,2,'@@@@@@@@');&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;SPAN style="color: #3366ff;"&gt;mattrib&lt;/SPAN&gt; results &lt;SPAN style="color: #3366ff;"&gt;colname&lt;/SPAN&gt;={"n" "factorial(n)"};&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;SPAN style="color: #3366ff;"&gt;do&lt;/SPAN&gt; n=1 &lt;SPAN style="color: #3366ff;"&gt;to&lt;/SPAN&gt; 10;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;SPAN style="color: #3366ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; call&lt;/SPAN&gt; symput("n",&lt;SPAN style="color: #3366ff;"&gt;char&lt;/SPAN&gt;(n));&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @%let fact_n = %fact(&amp;amp;n);&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; results[n,1] = char(n,2);&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; results[n,2] = symget("fact_n");&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;SPAN style="color: #3366ff;"&gt;end&lt;/SPAN&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;SPAN style="color: #3366ff;"&gt;print&lt;/SPAN&gt; results;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN lang="EN-US" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;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?&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;Best regards,&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 28 Jun 2014 16:46:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Recursive-modules-in-SAS-IML/m-p/159530#M1467</guid>
      <dc:creator>HectorRodriguez_Deniz</dc:creator>
      <dc:date>2014-06-28T16:46:53Z</dc:date>
    </item>
    <item>
      <title>Re: Recursive modules in SAS/IML?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Recursive-modules-in-SAS-IML/m-p/159531#M1468</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&amp;gt;&amp;nbsp; I am pretty sure there may be more elegant alternatives to write recursive functions within IML/IMLPlus.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp; Both factorial and Fibomacci are implemented efficiently in IML by using iteration. (I am not familiar with the Ackermann function.)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 28 Jun 2014 21:04:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Recursive-modules-in-SAS-IML/m-p/159531#M1468</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2014-06-28T21:04:14Z</dc:date>
    </item>
  </channel>
</rss>

