<?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: Using global variable in a loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-global-variable-in-a-loop/m-p/614397#M179587</link>
    <description>That is great and super fast! Thanks a lot!!!</description>
    <pubDate>Mon, 30 Dec 2019 14:36:04 GMT</pubDate>
    <dc:creator>sas_user_null</dc:creator>
    <dc:date>2019-12-30T14:36:04Z</dc:date>
    <item>
      <title>Using global variable in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-global-variable-in-a-loop/m-p/614389#M179580</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a question for using sas variables dynamically. I created a static list prompt. When selected, prompt outputs following variables:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%LET Selected_Scenarios_count = 3;&lt;BR /&gt;%LET Selected_Scenarios = Base;&lt;BR /&gt;%LET Selected_Scenarios0 = 3;&lt;BR /&gt;%LET Selected_Scenarios3 = Down;&lt;BR /&gt;%LET Selected_Scenarios1 = Base;&lt;BR /&gt;%LET Selected_Scenarios2 = Up;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my variables are working perfectly but I wish to call them dynamically. I wrote the code below but it is not functioning. My question: How can I use the variables within a dynamic loop?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%do i=1 %to &amp;amp;Selected_Scenarios_count.;&lt;/P&gt;&lt;P&gt;Name_&amp;amp;selected_scenarios&amp;amp;i.=value; /*Trying to name the column with respect to variable. eg:'Down'*/&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Dec 2019 14:15:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-global-variable-in-a-loop/m-p/614389#M179580</guid>
      <dc:creator>sas_user_null</dc:creator>
      <dc:date>2019-12-30T14:15:05Z</dc:date>
    </item>
    <item>
      <title>Re: Using global variable in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-global-variable-in-a-loop/m-p/614391#M179581</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/305117"&gt;@sas_user_null&lt;/a&gt;&amp;nbsp; Assuming your question is why your loop isn't functioning, the reason is you need an extra ampersand &amp;amp; to indirectly reference the macro variable resolution within the loop&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So &amp;amp;&amp;amp; here should solve the problem&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Name_&amp;amp;&amp;amp;selected_scenarios&amp;amp;i.=value;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can basically test with a %PUT statement to see the generated statements(text) in the log. For example&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET Selected_Scenarios_count = 3;
%LET Selected_Scenarios = Base;
%LET Selected_Scenarios0 = 3;
%LET Selected_Scenarios3 = Down;
%LET Selected_Scenarios1 = Base;
%LET Selected_Scenarios2 = Up;

%macro t;
%do i=1 %to &amp;amp;Selected_Scenarios_count.;
 %put Name_&amp;amp;&amp;amp;selected_scenarios&amp;amp;i.=value; /*Trying to name the column with respect to variable. eg:'Down'*/
%end;
%mend t;

%t&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Dec 2019 14:27:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-global-variable-in-a-loop/m-p/614391#M179581</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-12-30T14:27:35Z</dc:date>
    </item>
    <item>
      <title>Re: Using global variable in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-global-variable-in-a-loop/m-p/614395#M179585</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/305117"&gt;@sas_user_null&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In my understanding, you want to create a dataset (let's say dataset 'have') with a number of columns equal to 3 (cf. your macro variable &amp;nbsp;Selected_scenarios_count) and named according to your macrovariables (-&amp;gt; Name_down etc.)&lt;/P&gt;
&lt;P&gt;Is that right?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro var_name();
	data have;
		array Name_Selected_Scenarios(&amp;amp;Selected_Scenarios_count.);

		%do i=1 %to &amp;amp;Selected_Scenarios_count.;
			rename Name_selected_scenarios&amp;amp;i.=Name_&amp;amp;&amp;amp;selected_scenarios&amp;amp;i.;
		%end;
	run;

%mend;

%var_name;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 30 Dec 2019 14:32:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-global-variable-in-a-loop/m-p/614395#M179585</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-12-30T14:32:00Z</dc:date>
    </item>
    <item>
      <title>Re: Using global variable in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-global-variable-in-a-loop/m-p/614397#M179587</link>
      <description>That is great and super fast! Thanks a lot!!!</description>
      <pubDate>Mon, 30 Dec 2019 14:36:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-global-variable-in-a-loop/m-p/614397#M179587</guid>
      <dc:creator>sas_user_null</dc:creator>
      <dc:date>2019-12-30T14:36:04Z</dc:date>
    </item>
  </channel>
</rss>

