<?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: Iterative definition of macro variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Iterative-definition-of-macro-variables/m-p/341103#M272664</link>
    <description>&lt;P&gt;Thank you for the valuable thoughts and the time that you have spent on my problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="short_text"&gt;&lt;SPAN class=""&gt;For the sake of completeness&lt;/SPAN&gt;&lt;/SPAN&gt; here the working code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO a(XVAR1=, CCENT=, GCENT=);
%local xvar1n;
%if %sysevalf(%superq(XVAR1)=,boolean) = 0 %then %do; 
	%let NXVAR1 = %sysfunc(countw(&amp;amp;XVAR1));                             
	%Do N1 = 1 %to &amp;amp;NXVAR1;
		%Let a=%scan(&amp;amp;XVAR1,&amp;amp;N1);
		%Let b=%index(&amp;amp;CCENT,&amp;amp;a);
		%Let c=%index(&amp;amp;GCENT,&amp;amp;a);
		%if &amp;amp;b &amp;gt; 0 %then %do;
			%Let X1&amp;amp;N1 = &amp;amp;a.cc;
		%end;
		%else %if &amp;amp;c &amp;gt; 0 %then %do;
			%Let X1&amp;amp;N1 = &amp;amp;a.gc;
		%end;
		%else %do;
			%Let X1&amp;amp;N1 = &amp;amp;a;
		%end;
		%let XVAR1n = &amp;amp;XVAR1n &amp;amp;&amp;amp;X1&amp;amp;N1;
	%end;
%end;
%else %do;
	%Let XVAR1n = ;
%end;
%put &amp;amp;XVAR1n;
%mend a;

%a(XVAR1=xvar1a xvar1b, CCENT=xvar1b, GCENT=xvar1a);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 15 Mar 2017 08:06:08 GMT</pubDate>
    <dc:creator>Daniel_Paul</dc:creator>
    <dc:date>2017-03-15T08:06:08Z</dc:date>
    <item>
      <title>Iterative definition of macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-definition-of-macro-variables/m-p/340239#M272659</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I try to define macro variables in an iterative loop. An altough the code underneath does what it should (SAS 9.4), we can expect a warning message "apparent symbolic references not resolved". Actually I have no idea how a more elegant solution might look. Can you help me?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO a(XVAR1=, CCENT=, GCENT=);
%MACRO NAMEP(prefix,maxnum);
	%DO i=1 %to &amp;amp;maxnum;
		&amp;amp;prefix&amp;amp;i
	%END;
%MEND NAMEP;


%if %sysevalf(%superq(XVAR1)=,boolean) = 0 %then %do; 
	%let NXVAR1 = 1;                             
	%do %while(%length(%scan(&amp;amp;XVAR1,&amp;amp;NXVAR1)));
		%let NXVAR1 = %EVAL(&amp;amp;NXVAR1 + 1);
	%end;
	%Let NXVAR1 = %eval(&amp;amp;NXVAR1 -1);
	%Do N1 = 1 %to &amp;amp;NXVAR1;
		%Let a=%scan(&amp;amp;XVAR1,&amp;amp;N1);
		%Let b=%index(&amp;amp;CCENT,&amp;amp;a);
		%Let c=%index(&amp;amp;GCENT,&amp;amp;a);
		%if &amp;amp;b &amp;gt; 0 %then %do;
			%Let X1&amp;amp;N1 = &amp;amp;a.cc;
		%end;
		%else %if &amp;amp;c &amp;gt; 0 %then %do;
			%Let X1&amp;amp;N1 = &amp;amp;a.gc;
		%end;
		%else %do;
			%Let X1&amp;amp;N1 = &amp;amp;a;
		%end;
	%end;
	%let XVAR1n = %namep(&amp;amp;X1,&amp;amp;NXVAR1);
%end;
%else %do;
	%Let XVAR1n = ;
%end;
%put &amp;amp;XVAR1n;

%mend a;

%a(XVAR1=xvar1a xvar1b, CCENT=xvar1b, GCENT=xvar1a);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bye, Daniel&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 12 Mar 2017 17:08:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-definition-of-macro-variables/m-p/340239#M272659</guid>
      <dc:creator>Daniel_Paul</dc:creator>
      <dc:date>2017-03-12T17:08:34Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative definition of macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-definition-of-macro-variables/m-p/340261#M272660</link>
      <description>&lt;P&gt;I can't work out &lt;EM&gt;exactly&lt;/EM&gt; what you're trying to do, but is it simply a case of:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	%let XVAR1n = %namep(X1, &amp;amp;NXVAR1);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 12 Mar 2017 20:33:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-definition-of-macro-variables/m-p/340261#M272660</guid>
      <dc:creator>LaurieF</dc:creator>
      <dc:date>2017-03-12T20:33:03Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative definition of macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-definition-of-macro-variables/m-p/340346#M272661</link>
      <description>&lt;P&gt;What exactly is it your trying to do? &amp;nbsp;I can guarentee you thre is a far simpler and easier to maintain way of doing things thatn that, be it re-modelling your data, or using inbuilt SAS functionality. &amp;nbsp;Creating endless macro lists, macros and such like is a recipe for obfuscatd, unmaintable code which will fall over every time you run it.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2017 09:38:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-definition-of-macro-variables/m-p/340346#M272661</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-03-13T09:38:21Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative definition of macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-definition-of-macro-variables/m-p/340433#M272662</link>
      <description>&lt;P&gt;Defining a macro to include another macro, especially one such as your NAMEP that has no dependencies on the parameters of the outmacro is poor practice. As a minimum you waste compute cycles recompiling it everytime the outer macro executes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And it really helps to provide an example of what you expect this macro to produce.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2017 14:45:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-definition-of-macro-variables/m-p/340433#M272662</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-03-13T14:45:52Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative definition of macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-definition-of-macro-variables/m-p/340627#M272663</link>
      <description>&lt;P&gt;First as was mentioned by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; embedded macro definitions are not needed and almost always counter productive.&amp;nbsp; The macro definitions would appear as:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO NAMEP(prefix,maxnum);
	%DO i=1 %to &amp;amp;maxnum;
		&amp;amp;prefix&amp;amp;i
	%END;
%MEND NAMEP;
%MACRO a(XVAR1=, CCENT=, GCENT=);
%local xvar1n;
....&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The better news is that you don't need the %NAMEP macro anyway.&amp;nbsp; Let's look at your other code a bit.&amp;nbsp; This portion counts the number of words in &amp;amp;XVAR1.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	%let NXVAR1 = 1;                             
	%do %while(%length(%scan(&amp;amp;XVAR1,&amp;amp;NXVAR1)));
		%let NXVAR1 = %EVAL(&amp;amp;NXVAR1 + 1);
	%end;
	%Let NXVAR1 = %eval(&amp;amp;NXVAR1 -1);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is simplified with the COUNTW function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	%let NXVAR1 = %sysfunc(countw( &amp;amp;XVAR1));                            
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The call to the %NAMEP is causing an error (unitialized variable because &amp;amp;X1 is not defined).&amp;nbsp; Since you already have a loop. the definition of &amp;amp;XVAR1N can be moved insite the loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;		%else %do;
			%Let X1&amp;amp;N1 = &amp;amp;a;
		%end;
	  %let XVAR1n = &amp;amp;xvar1n &amp;amp;&amp;amp;x1&amp;amp;n1;
	%end;
%end;
%else %do;
	%Let XVAR1n = ;
%end;
%put &amp;amp;XVAR1n;

%mend a;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Notice that &amp;amp;XVAR1N is initialized as a local macro variable just below the macro statement. Other cleanup in the macro is possible, but this should get you started.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Mar 2017 02:44:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-definition-of-macro-variables/m-p/340627#M272663</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2017-03-14T02:44:21Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative definition of macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-definition-of-macro-variables/m-p/341103#M272664</link>
      <description>&lt;P&gt;Thank you for the valuable thoughts and the time that you have spent on my problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="short_text"&gt;&lt;SPAN class=""&gt;For the sake of completeness&lt;/SPAN&gt;&lt;/SPAN&gt; here the working code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO a(XVAR1=, CCENT=, GCENT=);
%local xvar1n;
%if %sysevalf(%superq(XVAR1)=,boolean) = 0 %then %do; 
	%let NXVAR1 = %sysfunc(countw(&amp;amp;XVAR1));                             
	%Do N1 = 1 %to &amp;amp;NXVAR1;
		%Let a=%scan(&amp;amp;XVAR1,&amp;amp;N1);
		%Let b=%index(&amp;amp;CCENT,&amp;amp;a);
		%Let c=%index(&amp;amp;GCENT,&amp;amp;a);
		%if &amp;amp;b &amp;gt; 0 %then %do;
			%Let X1&amp;amp;N1 = &amp;amp;a.cc;
		%end;
		%else %if &amp;amp;c &amp;gt; 0 %then %do;
			%Let X1&amp;amp;N1 = &amp;amp;a.gc;
		%end;
		%else %do;
			%Let X1&amp;amp;N1 = &amp;amp;a;
		%end;
		%let XVAR1n = &amp;amp;XVAR1n &amp;amp;&amp;amp;X1&amp;amp;N1;
	%end;
%end;
%else %do;
	%Let XVAR1n = ;
%end;
%put &amp;amp;XVAR1n;
%mend a;

%a(XVAR1=xvar1a xvar1b, CCENT=xvar1b, GCENT=xvar1a);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Mar 2017 08:06:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-definition-of-macro-variables/m-p/341103#M272664</guid>
      <dc:creator>Daniel_Paul</dc:creator>
      <dc:date>2017-03-15T08:06:08Z</dc:date>
    </item>
  </channel>
</rss>

