<?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: %global error while looping in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/global-error-while-looping/m-p/484071#M125607</link>
    <description>&lt;P&gt;The reason you cannot make a variable GLOBAL if it already exists in a local symbol table is because there is no way for you to assign it a value or reference its value. It will be hidden by the existing macro variable with the same name in a closer scope.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The only real purpose in defining a macro variable %GLOBAL inside a macro is if you want to make sure the values you assign to it will live after the macro exits.&amp;nbsp; &amp;nbsp;But if the macro variable already exists when your macro&amp;nbsp;starts running then it will exist after your macro ends.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So just make the macro that it causing the error smart enough NOT to cause the error.&amp;nbsp; Have it test whether the macro variable variable already exists before trying to define it as global.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro check_list;
%if not %symexist(list) %then %global list;
%if &amp;amp;list= %then %do;
%let list=“myname”;

(Other code)

%end;
%mend check_list;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 04 Aug 2018 23:05:28 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2018-08-04T23:05:28Z</dc:date>
    <item>
      <title>%global error while looping</title>
      <link>https://communities.sas.com/t5/SAS-Programming/global-error-while-looping/m-p/484068#M125604</link>
      <description>Hi,&lt;BR /&gt;I had this code which was running fine.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;%macro check_list;&lt;BR /&gt;%global list;&lt;BR /&gt;%if &amp;amp;list= %then %do;&lt;BR /&gt;%let list=“myname”;&lt;BR /&gt;&lt;BR /&gt;(Other code)&lt;BR /&gt;&lt;BR /&gt;%end;&lt;BR /&gt;%mend check_list;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Now I had to loop this process so I updated it to below.&lt;BR /&gt;&lt;BR /&gt;%macro loop;&lt;BR /&gt;%do i = 1 to 4;&lt;BR /&gt;&lt;BR /&gt;%macro check_list;&lt;BR /&gt;%global list&amp;amp;i;&lt;BR /&gt;%if &amp;amp;&amp;amp;list&amp;amp;i= %then %do;&lt;BR /&gt;%let list&amp;amp;i=“myname”;&lt;BR /&gt;&lt;BR /&gt;(Other code)&lt;BR /&gt;%end;&lt;BR /&gt;%mend loop;&lt;BR /&gt;&lt;BR /&gt;%loop;&lt;BR /&gt;&lt;BR /&gt;%end;&lt;BR /&gt;%mend check_list;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;But now I am getting this error.&lt;BR /&gt;&lt;BR /&gt;Error: attempt to %global a name (list2) which exists in a local environment.&lt;BR /&gt;&lt;BR /&gt;How can I get rid of this and run the program successfully.&lt;BR /&gt;&lt;BR /&gt;Any suggestions is appreciated. Thanks in advance.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Sat, 04 Aug 2018 22:28:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/global-error-while-looping/m-p/484068#M125604</guid>
      <dc:creator>nickspencer</dc:creator>
      <dc:date>2018-08-04T22:28:29Z</dc:date>
    </item>
    <item>
      <title>Re: %global error while looping</title>
      <link>https://communities.sas.com/t5/SAS-Programming/global-error-while-looping/m-p/484069#M125605</link>
      <description>&lt;P&gt;First suggestion:&amp;nbsp; untangle the macro definitions so that a human has a prayer of understanding the code.&amp;nbsp; There is hardly ever a reason to define one macro inside the definition of another macro.&amp;nbsp; Don't do it.&amp;nbsp; You can still have a macro call another macro, even if the macro being called is defined later.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second suggestion:&amp;nbsp; check that "other code" section to make sure it doesn't assign a value to &amp;amp;LIST2 when &amp;amp;i=1.&lt;/P&gt;</description>
      <pubDate>Sat, 04 Aug 2018 22:53:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/global-error-while-looping/m-p/484069#M125605</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-08-04T22:53:33Z</dc:date>
    </item>
    <item>
      <title>Re: %global error while looping</title>
      <link>https://communities.sas.com/t5/SAS-Programming/global-error-while-looping/m-p/484071#M125607</link>
      <description>&lt;P&gt;The reason you cannot make a variable GLOBAL if it already exists in a local symbol table is because there is no way for you to assign it a value or reference its value. It will be hidden by the existing macro variable with the same name in a closer scope.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The only real purpose in defining a macro variable %GLOBAL inside a macro is if you want to make sure the values you assign to it will live after the macro exits.&amp;nbsp; &amp;nbsp;But if the macro variable already exists when your macro&amp;nbsp;starts running then it will exist after your macro ends.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So just make the macro that it causing the error smart enough NOT to cause the error.&amp;nbsp; Have it test whether the macro variable variable already exists before trying to define it as global.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro check_list;
%if not %symexist(list) %then %global list;
%if &amp;amp;list= %then %do;
%let list=“myname”;

(Other code)

%end;
%mend check_list;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 04 Aug 2018 23:05:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/global-error-while-looping/m-p/484071#M125607</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-08-04T23:05:28Z</dc:date>
    </item>
  </channel>
</rss>

