<?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 How to use recursive methods in SAS？ in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-to-use-recursive-methods-in-SAS/m-p/605417#M17195</link>
    <description>&lt;P&gt;I am trying to define a macro which is used for replace mutiple transtrn(), that is to say:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
	method = '1,2,3,';
	method2 = transtrn(transtrn(transtrn(method,'1',"test method1"),'2',"test method2"),'3',"test method3");
	method3 = %transtrn(method,'1',"test method1",'2',"test method2",'3',"test method3");;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am expecting &lt;EM&gt;method3&amp;nbsp;&lt;/EM&gt;will has the same value as&amp;nbsp;&lt;EM&gt;method2.&lt;/EM&gt; So I write macro&amp;nbsp;&lt;CODE class=" language-sas"&gt;&lt;STRONG&gt;%transtrn() &lt;/STRONG&gt;like this:&lt;/CODE&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;option nomprint nomlogic;
%macro transtrn()/parmbuff;
*extract parameters;
%global header tail;
%let mstring = %substr(&amp;amp;syspbuff.,2,%length(&amp;amp;syspbuff.)-2);
%if &amp;amp;header eq %str() %then %do;
	%let header = %scan(%superq(mstring),1,%str(,));
%end;

*use prx-function to get substitute strings reliably;
%let pat = %sysfunc(prxparse(%str(/(('.*?')|(".*?")),(('.*?')|(".*?"))/)));
%if %sysfunc(prxmatch(&amp;amp;pat.,%superq(mstring))) %then %do;
	%let sub1 = %sysfunc(prxposn(&amp;amp;pat.,1,%superq(mstring)));
	%let sub2 = %sysfunc(prxposn(&amp;amp;pat.,4,%superq(mstring)));
%end;

*try recursion for multiple recall the prx-function part and combine every results into the final expression;
%let header = transtrn(&amp;amp;header.;
%let tail = &amp;amp;tail.,%str(&amp;amp;sub1.),%str(&amp;amp;sub2.));

%let left = %sysfunc(transtrn(%superq(mstring),%str(,&amp;amp;sub1.),%str()));
%let left = %sysfunc(transtrn(%superq(left),%str(,&amp;amp;sub2.),%str()));
%if %sysfunc(prxmatch(&amp;amp;pat.,%superq(left))) %then %transtrn(&amp;amp;left.);
%else %do;
	%let Rst = &amp;amp;header &amp;amp;tail.;
	&amp;amp;Rst.;
	%symdel header tail; 
%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;I tested my macro like this and everything seems right:&lt;/CODE&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%put %transtrn(method,'1',"test method1",'2',"test method2",'3',"test method3");&lt;BR /&gt;transtrn(method ,'1',"test method1"),'2',"test method2"),'3',"test method3")&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;But when I apply it to my test program, it just wrong:&lt;/CODE&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
	method = '1,2,3,';
	method2 = transtrn(transtrn(transtrn(method,'1',"test method1"),'2',"test method2"),'3',"test method3");
	method3 = %transtrn(method,'1',"test method1",'2',"test method2",'3',"test method3");;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;Can someone help me? thanks a lot.&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 19 Nov 2019 15:21:27 GMT</pubDate>
    <dc:creator>whymath</dc:creator>
    <dc:date>2019-11-19T15:21:27Z</dc:date>
    <item>
      <title>How to use recursive methods in SAS？</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-use-recursive-methods-in-SAS/m-p/605417#M17195</link>
      <description>&lt;P&gt;I am trying to define a macro which is used for replace mutiple transtrn(), that is to say:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
	method = '1,2,3,';
	method2 = transtrn(transtrn(transtrn(method,'1',"test method1"),'2',"test method2"),'3',"test method3");
	method3 = %transtrn(method,'1',"test method1",'2',"test method2",'3',"test method3");;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am expecting &lt;EM&gt;method3&amp;nbsp;&lt;/EM&gt;will has the same value as&amp;nbsp;&lt;EM&gt;method2.&lt;/EM&gt; So I write macro&amp;nbsp;&lt;CODE class=" language-sas"&gt;&lt;STRONG&gt;%transtrn() &lt;/STRONG&gt;like this:&lt;/CODE&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;option nomprint nomlogic;
%macro transtrn()/parmbuff;
*extract parameters;
%global header tail;
%let mstring = %substr(&amp;amp;syspbuff.,2,%length(&amp;amp;syspbuff.)-2);
%if &amp;amp;header eq %str() %then %do;
	%let header = %scan(%superq(mstring),1,%str(,));
%end;

*use prx-function to get substitute strings reliably;
%let pat = %sysfunc(prxparse(%str(/(('.*?')|(".*?")),(('.*?')|(".*?"))/)));
%if %sysfunc(prxmatch(&amp;amp;pat.,%superq(mstring))) %then %do;
	%let sub1 = %sysfunc(prxposn(&amp;amp;pat.,1,%superq(mstring)));
	%let sub2 = %sysfunc(prxposn(&amp;amp;pat.,4,%superq(mstring)));
%end;

*try recursion for multiple recall the prx-function part and combine every results into the final expression;
%let header = transtrn(&amp;amp;header.;
%let tail = &amp;amp;tail.,%str(&amp;amp;sub1.),%str(&amp;amp;sub2.));

%let left = %sysfunc(transtrn(%superq(mstring),%str(,&amp;amp;sub1.),%str()));
%let left = %sysfunc(transtrn(%superq(left),%str(,&amp;amp;sub2.),%str()));
%if %sysfunc(prxmatch(&amp;amp;pat.,%superq(left))) %then %transtrn(&amp;amp;left.);
%else %do;
	%let Rst = &amp;amp;header &amp;amp;tail.;
	&amp;amp;Rst.;
	%symdel header tail; 
%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;I tested my macro like this and everything seems right:&lt;/CODE&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%put %transtrn(method,'1',"test method1",'2',"test method2",'3',"test method3");&lt;BR /&gt;transtrn(method ,'1',"test method1"),'2',"test method2"),'3',"test method3")&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;But when I apply it to my test program, it just wrong:&lt;/CODE&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
	method = '1,2,3,';
	method2 = transtrn(transtrn(transtrn(method,'1',"test method1"),'2',"test method2"),'3',"test method3");
	method3 = %transtrn(method,'1',"test method1",'2',"test method2",'3',"test method3");;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;Can someone help me? thanks a lot.&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Nov 2019 15:21:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-use-recursive-methods-in-SAS/m-p/605417#M17195</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2019-11-19T15:21:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to use recursive methods in SAS？</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-use-recursive-methods-in-SAS/m-p/605565#M17218</link>
      <description>&lt;P&gt;First thing, DO NOT use *text ; style comments. Inside macros either use /*text */ or %* text; The *text; style comments will be part of the resolved macro values&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I removed your commented lines I got what I believe to be your desired output. So change all those to /*&amp;nbsp; */&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Personally I only use /* */ comments so I never have to worry about using the proper form&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There were LOTS of clues in the Log when executing that data step:&lt;/P&gt;
&lt;PRE&gt;488  data test;
489     method = '1,2,3,';
490     method2 = transtrn(transtrn(transtrn(method,'1',"test method1"),'2',"test
490! method2"),'3',"test method3");
491     method3 = %transtrn(method,'1',"test method1",'2',"test method2",'3',"test method3");;
NOTE: Line generated by the invoked macro "TRANSTRN".
1     &lt;FONT size="4"&gt;&lt;STRONG&gt;&lt;FONT color="#ff0000"&gt;*extract parameters;
&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;      -
      386
      200
      76
ERROR: Undeclared array referenced: transtrn.
NOTE 137-205: Line generated by the invoked macro "TRANSTRN".
7        &amp;amp;Rst.;
              -
              22
ERROR 386-185: Expecting an arithmetic expression.

ERROR 200-322: The symbol is not recognized and will be ignored.

ERROR 76-322: Syntax error, statement will be ignored.

ERROR 22-322: Syntax error, expecting one of the following: +, =.

NOTE: Line generated by the invoked macro "TRANSTRN".
7        &amp;amp;Rst.;
              -
              76
ERROR 76-322: Syntax error, statement will be ignored.

492  run;
&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Nov 2019 22:40:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-use-recursive-methods-in-SAS/m-p/605565#M17218</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-11-19T22:40:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to use recursive methods in SAS？</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-use-recursive-methods-in-SAS/m-p/605594#M17220</link>
      <description>&lt;P&gt;You tell the truth.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Nov 2019 01:46:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-use-recursive-methods-in-SAS/m-p/605594#M17220</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2019-11-20T01:46:29Z</dc:date>
    </item>
  </channel>
</rss>

