<?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: Proper way to write a Recursive macro function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477410#M122942</link>
    <description>&lt;P&gt;I haven't checked the details, but your request sounds a bit similar to &lt;A href="https://communities.sas.com/t5/General-SAS-Programming/How-do-I-do-merging-with-changing-variables/m-p/476274#M58285" target="_blank"&gt;one I answered yesterday&lt;/A&gt;. Please note that I generalized the solution (to the case of multiple "parents" per "child") after the thread opener had marked my initial solution as accepted.&lt;/P&gt;</description>
    <pubDate>Thu, 12 Jul 2018 12:02:54 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2018-07-12T12:02:54Z</dc:date>
    <item>
      <title>Proper way to write a Recursive macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477397#M122936</link>
      <description>&lt;P&gt;I've two tables that I'm working with.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First has the basic information like RK, ID and name etc&lt;/P&gt;&lt;P&gt;second table creates the hierarchy and have columns as RK and Parent_RK&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to get all the children of a record under his full hierarchy.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My code is&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*A parent whose sub branches are required;&lt;BR /&gt;%let _MO = 10008;
&lt;BR /&gt;*Data set to save all of the sub branches;
proc sql noprint;
create table management(
rk int,
ID varchar(4000),
Name varchar(4000)
);
quit;

%macro recursive(MO);

%put &amp;amp;MO;

proc sql noprint;&lt;BR /&gt;*Insert parent into dataset;
insert into management
select distinct Par.management_org_rk, Mo.management_org_id,Mo.Organization_nm
from Sasoprsk.Management_org_assoc_l par
inner join Sasoprsk.Management_org_l Mo on Mo.management_org_rk = par.management_org_rk
where par.management_org_rk = &amp;amp;MO;
&lt;BR /&gt;*Find all the sub branches of that parent;
select distinct management_org_rk
into: MO_List separated by "|"
from Sasoprsk.Management_org_assoc_l
where Parent_management_org_rk = &amp;amp;MO
and MANAGEMENT_ORG_ASSOC_TYPE_CD = 'DEF';
&lt;BR /&gt;*Count of sub branches of that parent;
select count(distinct management_org_rk)
into: count
from Sasoprsk.Management_org_assoc_l
where Parent_management_org_rk = &amp;amp;MO
and MANAGEMENT_ORG_ASSOC_TYPE_CD = 'DEF';
quit;

%put &amp;amp;MO_List;
%put &amp;amp;count;

%if &amp;amp;count ne 0 %then 
%do i=1 %to &amp;amp;count;
	%let Child=%scan(%bquote(&amp;amp;MO_List), %bquote(&amp;amp;i) ,%str(|));&lt;BR /&gt;        *repeat the process for each sub branch;
	%recursive(&amp;amp;Child);
	%put &amp;amp;i;
%end;

%mend;

%recursive(&amp;amp;_MO);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;Dry Run&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;First Cycle&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Lets assume MO 11382 has 3 kids&lt;/P&gt;&lt;P&gt;MO_List = 11383|11384|&lt;SPAN&gt;11385&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;count = 3&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;child = 11383&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Second Cycle&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;MO 11383 has 2 Kids&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;MO_List = 11386|11387&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;count =2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;child = 11386&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Third Cycle&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Mo 11386 has no kids&lt;/P&gt;&lt;P&gt;MO_List =&lt;SPAN&gt;11386|11387&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;count = 0;&lt;/P&gt;&lt;P&gt;i=1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now system will go back to second cycle&lt;/P&gt;&lt;P&gt;MO= 11387 has no kids&lt;/P&gt;&lt;P&gt;MO_List =&lt;SPAN&gt;11386|11387&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;count = 0;&lt;/P&gt;&lt;P&gt;i=2;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now system will go back to first cycle&lt;/P&gt;&lt;P&gt;since MO_List has been updated it won't be able to proceed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kindly suggest me a better approach to get all of the children of a MO. In server there are ten levels of h&lt;SPAN&gt;ierarchy&lt;/SPAN&gt; so I can't hard-code.&lt;/P&gt;&lt;P&gt;Can we do multi threading at do loop that instead of executing with first child he should first send all of the children to macro then do same with their kids.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hierarchy is like a tree with multiple nodes.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 13:19:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477397#M122936</guid>
      <dc:creator>Azeem112</dc:creator>
      <dc:date>2018-07-12T13:19:08Z</dc:date>
    </item>
    <item>
      <title>Re: Proper way to write a Recursive macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477403#M122939</link>
      <description>&lt;P&gt;Can post test data as data step, so that we have something to play with?&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 11:19:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477403#M122939</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2018-07-12T11:19:30Z</dc:date>
    </item>
    <item>
      <title>Re: Proper way to write a Recursive macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477406#M122940</link>
      <description>I've attached the sample data sets from RACE EGRC server.</description>
      <pubDate>Thu, 12 Jul 2018 11:46:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477406#M122940</guid>
      <dc:creator>Azeem112</dc:creator>
      <dc:date>2018-07-12T11:46:16Z</dc:date>
    </item>
    <item>
      <title>Re: Proper way to write a Recursive macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477410#M122942</link>
      <description>&lt;P&gt;I haven't checked the details, but your request sounds a bit similar to &lt;A href="https://communities.sas.com/t5/General-SAS-Programming/How-do-I-do-merging-with-changing-variables/m-p/476274#M58285" target="_blank"&gt;one I answered yesterday&lt;/A&gt;. Please note that I generalized the solution (to the case of multiple "parents" per "child") after the thread opener had marked my initial solution as accepted.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 12:02:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477410#M122942</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-07-12T12:02:54Z</dc:date>
    </item>
    <item>
      <title>Re: Proper way to write a Recursive macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477417#M122944</link>
      <description>Thanks for your reply. my case is little different with this one, there are many kids of a single parent. I'll look in the solution and try to find my solution.</description>
      <pubDate>Thu, 12 Jul 2018 12:25:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477417#M122944</guid>
      <dc:creator>Azeem112</dc:creator>
      <dc:date>2018-07-12T12:25:18Z</dc:date>
    </item>
    <item>
      <title>Re: Proper way to write a Recursive macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477418#M122945</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I haven't read the detail of your code but make sure that any counter used in your recursive macro is declared as %local.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 12:33:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477418#M122945</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2018-07-12T12:33:57Z</dc:date>
    </item>
    <item>
      <title>Re: Proper way to write a Recursive macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477425#M122948</link>
      <description>I used %local instead of %recursive in my macro loop and it gave me following error.&lt;BR /&gt;&lt;BR /&gt;ERROR: Invalid symbolic variable name&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 12 Jul 2018 12:50:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477425#M122948</guid>
      <dc:creator>Azeem112</dc:creator>
      <dc:date>2018-07-12T12:50:15Z</dc:date>
    </item>
    <item>
      <title>Re: Proper way to write a Recursive macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477426#M122949</link>
      <description>I meant, you have to declare your counter i as local,&lt;BR /&gt;for instance at the beginning of your macro.&lt;BR /&gt;&lt;BR /&gt;%local i;</description>
      <pubDate>Thu, 12 Jul 2018 12:51:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477426#M122949</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2018-07-12T12:51:47Z</dc:date>
    </item>
    <item>
      <title>Re: Proper way to write a Recursive macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477427#M122950</link>
      <description>&lt;P&gt;Please explain what you want OUT of this macro.&amp;nbsp; It is not clear what you are trying to generate.&amp;nbsp; If it is a dataset then post example input and output data (in the form of data steps that readers can copy and run).&amp;nbsp; If it is a macro variable then show example result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Make sure to make you macro variables LOCAL so that nested calls do not overwrite their values.&amp;nbsp; In particular COUNT and I.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 12:57:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477427#M122950</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-07-12T12:57:04Z</dc:date>
    </item>
    <item>
      <title>Re: Proper way to write a Recursive macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477435#M122952</link>
      <description>&lt;P&gt;I've posted a sample data set.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Out of macro i want to save all of the children and grand children in&amp;nbsp;management table of a specific parent.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 13:07:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477435#M122952</guid>
      <dc:creator>Azeem112</dc:creator>
      <dc:date>2018-07-12T13:07:45Z</dc:date>
    </item>
    <item>
      <title>Re: Proper way to write a Recursive macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477479#M122977</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/163076"&gt;@Azeem112&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I've posted a sample data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Out of macro i want to save all of the children and grand children in&amp;nbsp;management table of a specific parent.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Datasets that someone would need to download are not as useful as actual code that can just be copied and pasted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sounds like a classical connected subgraph problem.&amp;nbsp; If you search on this site someone has posted a very nice subgraph macro you could probably use.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to make your macro recursive then you will probably need to have it generate a distinct table for each query to avoid conflicts between the versions that executing at the same time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Otherwise instead of using recursion just make a loop to keep finding and adding new descendants that stops when there are no more to be added.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 13:57:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477479#M122977</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-07-12T13:57:50Z</dc:date>
    </item>
    <item>
      <title>Re: Proper way to write a Recursive macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477488#M122991</link>
      <description>I'v added %local i; %local count; %local MO_List;&lt;BR /&gt;&lt;BR /&gt;and it solved my problem.</description>
      <pubDate>Thu, 12 Jul 2018 14:10:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/477488#M122991</guid>
      <dc:creator>Azeem112</dc:creator>
      <dc:date>2018-07-12T14:10:54Z</dc:date>
    </item>
    <item>
      <title>Re: Proper way to write a Recursive macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/648307#M194169</link>
      <description>&lt;P&gt;A very nice Solution!!!&lt;/P&gt;</description>
      <pubDate>Sat, 16 May 2020 15:22:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proper-way-to-write-a-Recursive-macro-function/m-p/648307#M194169</guid>
      <dc:creator>Ambica071</dc:creator>
      <dc:date>2020-05-16T15:22:43Z</dc:date>
    </item>
  </channel>
</rss>

