<?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 define macro variables in a loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-define-macro-variables-in-a-loop/m-p/574946#M162522</link>
    <description>&lt;P&gt;Hello alltogether,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do have a list of variable names, say: %let test_var=%nrstr(age sex biologica);&lt;/P&gt;&lt;P&gt;Actually this list is much longer.&lt;/P&gt;&lt;P&gt;For each Variable I want to create new variable names like MW_age MW_sex MW_biologica to use them late in my program.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I trie to do this with a macro-loop.&lt;/P&gt;&lt;P&gt;But the problem I do have is that only MW_age is known in the end of the macro and&amp;nbsp; mean_sex and mean_biologica are unknown outside the macro.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What do I have to do, to have all macro-variables available in the end?&lt;/P&gt;&lt;P&gt;Regards sasstats&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my program:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let test_var=%nrstr(age sex biologica);
%macro test_loop;
	%let i=1;
	%let var=%scan(&amp;amp;test_var.,&amp;amp;i.,' '); 

	%do %While("&amp;amp;var." NE "");
		%put &amp;amp;var.;
		%let MW_var_&amp;amp;i=MW_&amp;amp;var.;
		%put &amp;amp;&amp;amp;MW_Var_&amp;amp;i.;

		%let i=%eval(&amp;amp;i.+1);
		%let var=%scan(&amp;amp;test_var.,&amp;amp;i.,' ');
	%end;
/* here all 3 macro-variables are known */
%put &amp;amp;MW_var_1.;
%put &amp;amp;MW_var_2.;
%put &amp;amp;MW_var_3.;
%mend test_loop;
%test_loop;
&lt;BR /&gt;/* here only MW_age is known, the others not */
%put &amp;amp;MW_var_1.;
%put &amp;amp;MW_var_2.;
%put &amp;amp;MW_var_3.;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 19 Jul 2019 13:57:06 GMT</pubDate>
    <dc:creator>sasstats</dc:creator>
    <dc:date>2019-07-19T13:57:06Z</dc:date>
    <item>
      <title>How to define macro variables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-define-macro-variables-in-a-loop/m-p/574946#M162522</link>
      <description>&lt;P&gt;Hello alltogether,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do have a list of variable names, say: %let test_var=%nrstr(age sex biologica);&lt;/P&gt;&lt;P&gt;Actually this list is much longer.&lt;/P&gt;&lt;P&gt;For each Variable I want to create new variable names like MW_age MW_sex MW_biologica to use them late in my program.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I trie to do this with a macro-loop.&lt;/P&gt;&lt;P&gt;But the problem I do have is that only MW_age is known in the end of the macro and&amp;nbsp; mean_sex and mean_biologica are unknown outside the macro.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What do I have to do, to have all macro-variables available in the end?&lt;/P&gt;&lt;P&gt;Regards sasstats&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my program:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let test_var=%nrstr(age sex biologica);
%macro test_loop;
	%let i=1;
	%let var=%scan(&amp;amp;test_var.,&amp;amp;i.,' '); 

	%do %While("&amp;amp;var." NE "");
		%put &amp;amp;var.;
		%let MW_var_&amp;amp;i=MW_&amp;amp;var.;
		%put &amp;amp;&amp;amp;MW_Var_&amp;amp;i.;

		%let i=%eval(&amp;amp;i.+1);
		%let var=%scan(&amp;amp;test_var.,&amp;amp;i.,' ');
	%end;
/* here all 3 macro-variables are known */
%put &amp;amp;MW_var_1.;
%put &amp;amp;MW_var_2.;
%put &amp;amp;MW_var_3.;
%mend test_loop;
%test_loop;
&lt;BR /&gt;/* here only MW_age is known, the others not */
%put &amp;amp;MW_var_1.;
%put &amp;amp;MW_var_2.;
%put &amp;amp;MW_var_3.;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jul 2019 13:57:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-define-macro-variables-in-a-loop/m-p/574946#M162522</guid>
      <dc:creator>sasstats</dc:creator>
      <dc:date>2019-07-19T13:57:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to define macro variables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-define-macro-variables-in-a-loop/m-p/574948#M162523</link>
      <description>&lt;P&gt;The macro variables you create inside the macro TEST_LOOP are only available inside the macro. When you try to use them outside the macro, you can't, and an error results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can overcome this by putting all the code that uses these variables inside the macro TEST_LOOP; or by using the %GLOBAL command inside of TEST_LOOP, which allows the %GLOBAL macro variables to be used outside of the macro.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jul 2019 14:04:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-define-macro-variables-in-a-loop/m-p/574948#M162523</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-07-19T14:04:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to define macro variables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-define-macro-variables-in-a-loop/m-p/575304#M162708</link>
      <description>&lt;P&gt;Hey,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you very much for your information!&lt;/P&gt;&lt;P&gt;Using the further syntax inside my macro works fine!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;sasstats&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2019 08:54:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-define-macro-variables-in-a-loop/m-p/575304#M162708</guid>
      <dc:creator>sasstats</dc:creator>
      <dc:date>2019-07-22T08:54:34Z</dc:date>
    </item>
  </channel>
</rss>

