<?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: a suggestion and a question on Macro1 m104p05 about local and global macro concepts in Advanced Programming</title>
    <link>https://communities.sas.com/t5/Advanced-Programming/a-suggestion-and-a-question-on-Macro1-m104p05-about-local-and/m-p/988731#M388</link>
    <description>&lt;P&gt;There are a few other wrinkles you have not explored with your examples.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you create parameters for a macro they are LOCAL macro variables to that macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you reference a macro variable SAS will look for that macro starting with the innermost scope and working its way out to the GLOBAL "scope" (note that GLOBAL and AUTOMATIC macro variables are in the same symbol table and so in the same scope even though SAS prints them with different "scope" values when using %PUT keywords or looking at DICTIONARY.MACROS).&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is even true when you CREATE the macro variable.&amp;nbsp; So if you write this line inside a macro:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let i=ivalue;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And the macro variable I already exists in some outer scope (such as GLOBAL) then the value of that macro variable is modified.&amp;nbsp; To avoid this use the %LOCAL statement to create a new macro variable in the local scope before assigning it a value.&amp;nbsp; Or in a DATA step you can skip the %LOCAL statement by using the 'L' modifier when using the CALL SYMPUTX() method to assign the value to a macro variable.&amp;nbsp; That will force the macro variable into the local symbol table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An even stranger wrinkle is that SAS will not create the local symbol table for a macro until is it runs a statement that the SAS macro compiler thinks should cause it to need a local symbol table.&amp;nbsp; This means that any new macros that are created (by using some method that the macro compiler does not notice) will be made in the scope of the calling environment, even if that calling environment is another macro execution and not open code.&amp;nbsp; Try this example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro outer;
	%let myvar=OUTER ;
	%put &amp;amp;=sysmacroname BEFORE;
	%put _local_;
	%inner;
	%put &amp;amp;=sysmacroname AFTER;
	%put _local_;
%mend outer;
%macro inner;
	%put &amp;amp;=sysmacroname BEFORE;
	%put _local_;
	data _null_;
	  call symput('myvar2','inner');
	run;
	%put &amp;amp;=sysmacroname AFTER;
	%put _local_;
%mend inner;
%outer;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Where is the macro variable MYVAR2 created?&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt; SYSMACRONAME=OUTER BEFORE
 OUTER MYVAR OUTER
 SYSMACRONAME=INNER BEFORE
 
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       cpu time            0.00 seconds
       
 
 SYSMACRONAME=INNER AFTER
 SYSMACRONAME=OUTER AFTER
 OUTER MYVAR OUTER
 OUTER MYVAR2 inner&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;Now see what happens if you add a %LET statement to the inner macro.&amp;nbsp; Try first adding it AFTER the DATA step.&amp;nbsp; Where is MYVAR2 created?&lt;/P&gt;
&lt;P&gt;Then try adding it BEFORE the DATA step.&amp;nbsp; Where is MYVAR2 created?&lt;/P&gt;</description>
    <pubDate>Wed, 27 May 2026 15:15:45 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2026-05-27T15:15:45Z</dc:date>
    <item>
      <title>a suggestion and a question on Macro1 m104p05 about local and global macro concepts</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/a-suggestion-and-a-question-on-Macro1-m104p05-about-local-and/m-p/988657#M379</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;SPAN&gt;may I suggest that for Macro1 lesson practice m104p05, the example of local macro, the name of the macro, &lt;/SPAN&gt;&lt;FONT style="font-family: inherit;" color="#FF0000"&gt;scope&lt;/FONT&gt;&lt;SPAN&gt;, is very confusing (and it is good to use other name for the macro), I spent about 1 hour on it and finally figured out what was the issue that confused me. The code is as follows:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro scope;
%let stormtype1=Some damage;
%let stormtype2=Extensive damage;
%let stormtype3=Devastating damage;
%let stormtype4=Catastrophic damage;
%let stormtype5=Widespread catastrophic damage;
%put _user_;
%mend scope;
%scope;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;After run the code, the log is as follows:&lt;/P&gt;
&lt;PRE&gt;69         %macro scope;
 70         %let stormtype1=Some damage;
 71         %let stormtype2=Extensive damage;
 72         %let stormtype3=Devastating damage;
 73         %let stormtype4=Catastrophic damage;
 74         %let stormtype5=Widespread catastrophic damage;
 75         %put _user_;
 76         %mend scope;
 77         %scope;
 SCOPE STORMTYPE1 Some damage
 SCOPE STORMTYPE2 Extensive damage
 SCOPE STORMTYPE3 Devastating damage
 SCOPE STORMTYPE4 Catastrophic damage
 SCOPE STORMTYPE5 Widespread catastrophic damage
 
 GLOBAL GRAPHINIT GOPTIONS RESET=ALL GSFNAME=_GSFNAME;
 GLOBAL GRAPHTERM GOPTIONS NOACCESSIBLE;
&lt;/PRE&gt;
&lt;P&gt;Seeing the log I had an wrong&amp;nbsp; impression that global macro is explicitly displayed in the log as &lt;STRONG&gt;GLOBAL&lt;/STRONG&gt; ones and &lt;FONT color="#808080"&gt;local macro is explicitly displayed in the log as SCOPE ones&lt;/FONT&gt;, and I even tried at least 6 to 10 times to use &lt;EM&gt;&lt;FONT color="#808080"&gt;&lt;FONT color="#000000"&gt;%put&lt;/FONT&gt; _scope_;&amp;nbsp;&lt;/FONT&gt;&lt;/EM&gt;and&amp;nbsp;&lt;EM&gt;&lt;FONT color="#808080"&gt;&lt;FONT color="#000000"&gt;%put&lt;/FONT&gt; _local_;&lt;/FONT&gt;&lt;/EM&gt; to display my local macros in the log but failed, until I somehow luckily tried the follows:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test;
%global stormtype1;
%let stormtype1=Some damage;
%let stormtype2=Extensive damage;
%let stormtype3=Devastating damage;
%let stormtype4=Catastrophic damage;
%let stormtype5=Widespread catastrophic damage;
%put _user_;
%mend test;
%test;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And get this log as follows, and then I had the correct concepts, that is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(a) unlike global macros displayed in the log as &lt;FONT color="#3366FF"&gt;GLOBAL &lt;FONT color="#000000"&gt;ones&lt;/FONT&gt;&lt;/FONT&gt;, local macro is &lt;FONT color="#FF0000"&gt;NOT&lt;/FONT&gt; explicitly displayed in the log as something like &lt;FONT color="#999999"&gt;LOCAL&lt;/FONT&gt;&amp;nbsp;ones or &lt;FONT color="#999999"&gt;SCOPE&lt;FONT color="#000000"&gt; ones&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#000000"&gt;,&lt;/FONT&gt; it is the macro name &lt;FONT color="#0000FF"&gt;TEST&lt;FONT color="#000000"&gt;,&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(b) if I want to display local macros in the log, I have to use &lt;STRONG&gt;&lt;EM&gt;%put _user_;&lt;/EM&gt;&lt;/STRONG&gt;&amp;nbsp;inside the macro, if I use &lt;STRONG&gt;&lt;EM&gt;%put _user_;&lt;/EM&gt;&lt;/STRONG&gt; or &lt;STRONG&gt;&lt;EM&gt;%put _all_;&lt;/EM&gt;&lt;/STRONG&gt; outside this macro, then only global and automatic macros are displayed, local macros are &lt;FONT color="#FF0000"&gt;not&lt;/FONT&gt; displayed, and&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(c) so there is a question, is there any statement (aside from, for example, like &lt;STRONG&gt;&lt;EM&gt;%put &amp;amp;stormtype1;&lt;/EM&gt;&lt;/STRONG&gt;) I can use here, outside the &lt;STRONG&gt;&lt;EM&gt;%macro test&lt;/EM&gt;&lt;/STRONG&gt; macro, to display all of the local macro only (and do not display the global and automatic ones)?&lt;/P&gt;
&lt;PRE&gt;69         %macro test;
 70         %global stormtype1;
 71         %let stormtype1=Some damage;
 72         %let stormtype2=Extensive damage;
 73         %let stormtype3=Devastating damage;
 74         %let stormtype4=Catastrophic damage;
 75         %let stormtype5=Widespread catastrophic damage;
 76         %put _user_;
 77         %mend test;
 78         %test;
 TEST STORMTYPE2 Extensive damage
 TEST STORMTYPE3 Devastating damage
 TEST STORMTYPE4 Catastrophic damage
 TEST STORMTYPE5 Widespread catastrophic damage

 GLOBAL GRAPHINIT GOPTIONS RESET=ALL GSFNAME=_GSFNAME;
 GLOBAL GRAPHTERM GOPTIONS NOACCESSIBLE;

 GLOBAL STORMTYPE1 Some damage&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;Another thought is: use &lt;EM&gt;%global&lt;/EM&gt; statement very carefully because it is very easily to get macros confused created in the session.&lt;/P&gt;</description>
      <pubDate>Mon, 25 May 2026 17:24:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/a-suggestion-and-a-question-on-Macro1-m104p05-about-local-and/m-p/988657#M379</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2026-05-25T17:24:48Z</dc:date>
    </item>
    <item>
      <title>Re: a suggestion and a question on Macro1 m104p05 about local and global macro concepts</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/a-suggestion-and-a-question-on-Macro1-m104p05-about-local-and/m-p/988660#M380</link>
      <description>&lt;P&gt;I think you are looking for the&amp;nbsp; _LOCAL_ keyword in the %PUT statement.&amp;nbsp; That will exclude the GLOBAL (including the "automatic"ly global) macro variables.&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I agree that the name of the macro is confusing.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SCOPE is already a little overloaded with meanings:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;It has a general meaning in computer programming:&lt;/LI&gt;
&lt;/UL&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Scope defines where variables can be accessed or modified in your program. It determines the visibility and lifetime of variables - in other words, which parts of your program can "see" and use a particular variable.&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;A style="font-family: inherit; background-color: #ffffff;" href="https://www.w3schools.com/programming/prog_scope.php" target="_blank" rel="noopener"&gt;https://www.w3schools.com/programming/prog_scope.php&lt;/A&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;UL&gt;
&lt;LI&gt;It is also used as the name of the variable in SASHELP.VMACRO dictionary view that let's you examine the values of macro variable as a dataset.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;It would be better if they used something like MYMACRO as the macro's name instead.&lt;/P&gt;</description>
      <pubDate>Mon, 25 May 2026 18:39:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/a-suggestion-and-a-question-on-Macro1-m104p05-about-local-and/m-p/988660#M380</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-05-25T18:39:45Z</dc:date>
    </item>
    <item>
      <title>Re: a suggestion and a question on Macro1 m104p05 about local and global macro concepts</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/a-suggestion-and-a-question-on-Macro1-m104p05-about-local-and/m-p/988661#M381</link>
      <description>&lt;P&gt;With _LOCAL_ you only see the macro variables that exist in the innermost symbol table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With _USER_ you see the macro variables that live in all of the symbol tables (except those that SAS has deemed to be "automatic").&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Consider this example of nested macro calls.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro outer ;
%local mvar;
%let mvar=outer;
%put %str( );
%put &amp;amp;=sysmacroname _local_;
%put _local_;
%put &amp;amp;=sysmacroname _user_;
%put _user_;
%inner;
%mend outer;
%macro inner;
%local mvar;
%let mvar=inner;
%put %str( );
%put &amp;amp;=sysmacroname _local_;
%put _local_;
%put &amp;amp;=sysmacroname _user_;
%put _user_;
%mend inner;
%outer;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt; 
 SYSMACRONAME=OUTER _local_
 OUTER MVAR outer
 SYSMACRONAME=OUTER _user_
 OUTER MVAR outer
 GLOBAL CLIENTMACHINE  ...
 ...
  
 SYSMACRONAME=INNER _local_
 INNER MVAR inner
 SYSMACRONAME=INNER _user_
 INNER MVAR inner
 OUTER MVAR outer
 GLOBAL CLIENTMACHINE  ...
 ...
&lt;/PRE&gt;
&lt;P&gt;You can see that the call to %PUT _USER_ inside of the macro %INNER shows the local macro variables for both the %INNER and %OUTER macros.&lt;/P&gt;</description>
      <pubDate>Mon, 25 May 2026 18:49:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/a-suggestion-and-a-question-on-Macro1-m104p05-about-local-and/m-p/988661#M381</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-05-25T18:49:45Z</dc:date>
    </item>
    <item>
      <title>Re: a suggestion and a question on Macro1 m104p05 about local and global macro concepts</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/a-suggestion-and-a-question-on-Macro1-m104p05-about-local-and/m-p/988669#M382</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/466238"&gt;@dxiao2017&lt;/a&gt;&amp;nbsp;wrote in part:&lt;BR /&gt;
&lt;P&gt;(b) if I want to display local macros in the log, I have to use &lt;STRONG&gt;&lt;EM&gt;%put _user_;&lt;/EM&gt;&lt;/STRONG&gt;&amp;nbsp;inside the macro, if I use &lt;STRONG&gt;&lt;EM&gt;%put _user_;&lt;/EM&gt;&lt;/STRONG&gt; or &lt;STRONG&gt;&lt;EM&gt;%put _all_;&lt;/EM&gt;&lt;/STRONG&gt; outside this macro, then only global and automatic macros are displayed, local macros are &lt;FONT color="#FF0000"&gt;not&lt;/FONT&gt; displayed, and&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(c) so there is a question, is there any statement (aside from, for example, like &lt;STRONG&gt;&lt;EM&gt;%put &amp;amp;stormtype1;&lt;/EM&gt;&lt;/STRONG&gt;) I can use here, outside the &lt;STRONG&gt;&lt;EM&gt;%macro test&lt;/EM&gt;&lt;/STRONG&gt; macro, to display all of the local macro only (and do not display the global and automatic ones)?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;A key point about (b) and (c) is that local macro variables only exist while a macro is executing.&amp;nbsp; So if you have a local macro variable &amp;amp;x defined in your macro %test, that macro variable does not exist outside of %test.&amp;nbsp; Thus there is no statement that can be run outside of the macro %test that will display its macro variables. As Tom has shown, inside the macro %test both %put _user_ ; and %put _local_ ; are useful for displaying macro variables.&lt;/P&gt;</description>
      <pubDate>Tue, 26 May 2026 13:09:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/a-suggestion-and-a-question-on-Macro1-m104p05-about-local-and/m-p/988669#M382</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2026-05-26T13:09:41Z</dc:date>
    </item>
    <item>
      <title>Re: a suggestion and a question on Macro1 m104p05 about local and global macro concepts</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/a-suggestion-and-a-question-on-Macro1-m104p05-about-local-and/m-p/988671#M383</link>
      <description>&lt;P&gt;If you want to see the values of all current LOCAL macro variables you could query DICTIONARY.MACROS (or SASHELP.VMACRO).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; 73         proc sql;
 74         describe view sashelp.vmacro;
 NOTE: SQL view SASHELP.VMACRO is defined as:
 
         select *
           from DICTIONARY.MACROS;
 
 75         describe table dictionary.macros;
 NOTE: SQL table DICTIONARY.MACROS was created like:
 
 create table DICTIONARY.MACROS
   (
    scope char(32) label='Macro Scope',
    name char(32) label='Macro Variable Name',
    offset num label='Offset into Macro Variable',
    value char(200) label='Macro Variable Value'
   );
 
 76         quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For an example of using that check out this&amp;nbsp;&lt;A href="https://github.com/sasutils/macros/blob/master/symget.sas" target="_self"&gt;%symget()&lt;/A&gt;&amp;nbsp; macro which lets you retrieve the value of a macro variable that you could not normally access because it is "hidden" by the existence of another macro variable with the same name in a more local scope.&lt;/P&gt;</description>
      <pubDate>Tue, 26 May 2026 13:49:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/a-suggestion-and-a-question-on-Macro1-m104p05-about-local-and/m-p/988671#M383</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-05-26T13:49:21Z</dc:date>
    </item>
    <item>
      <title>Re: a suggestion and a question on Macro1 m104p05 about local and global macro concepts</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/a-suggestion-and-a-question-on-Macro1-m104p05-about-local-and/m-p/988694#M384</link>
      <description>&lt;P&gt;Thanks a lot Tom, for the clarification of the word SCOPE, I now understand the concept: tells me where I can see, access, and modify a macro. I will practice a little bit on &lt;EM&gt;sashelp.vmacro&lt;/EM&gt; dictionary later on. And you are right, I was trying to use &lt;EM&gt;%put _local_&lt;/EM&gt; while posting the question but nothing was displayed in the log then I thought I did not remember the statement correctly.&lt;/P&gt;</description>
      <pubDate>Wed, 27 May 2026 11:07:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/a-suggestion-and-a-question-on-Macro1-m104p05-about-local-and/m-p/988694#M384</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2026-05-27T11:07:04Z</dc:date>
    </item>
    <item>
      <title>Re: a suggestion and a question on Macro1 m104p05 about local and global macro concepts</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/a-suggestion-and-a-question-on-Macro1-m104p05-about-local-and/m-p/988695#M385</link>
      <description>&lt;P&gt;Thanks a lot&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;for the further explanations. So &lt;EM&gt;%put _local_&lt;/EM&gt; displays the &lt;EM&gt;local&lt;/EM&gt; macros, &lt;EM&gt;%put _user_&lt;/EM&gt; displays both &lt;EM&gt;global&lt;/EM&gt; and &lt;EM&gt;local&lt;/EM&gt; ones (that are user-defined), and &lt;EM&gt;%put _all_&lt;/EM&gt; displays &lt;EM&gt;global&lt;/EM&gt; and &lt;EM&gt;automatic&lt;/EM&gt; ones (yet does not display &lt;EM&gt;local&lt;/EM&gt; ones because &lt;EM&gt;local&lt;/EM&gt; ones only exist in the innermost symbol table).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think your example is good, yet it is a little bit complicated, I tried an easier example practiced a bit, as follows:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test1;
%global a;
%let a=avalue;
%let b=bvalue;
%put _global_;
%put _local_;
%mend test1;
%test1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt; 69         %macro test1;
 70         %global a;
 71         %let a=avalue;
 72         %let b=bvalue;
 73         %put _global_;
 74         %put _local_;
 75         %mend test1;
 76         %test1;
 GLOBAL A avalue

 TEST1 B bvalue&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 May 2026 11:26:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/a-suggestion-and-a-question-on-Macro1-m104p05-about-local-and/m-p/988695#M385</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2026-05-27T11:26:02Z</dc:date>
    </item>
    <item>
      <title>Re: a suggestion and a question on Macro1 m104p05 about local and global macro concepts</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/a-suggestion-and-a-question-on-Macro1-m104p05-about-local-and/m-p/988698#M386</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;I think it is a brilliant point you answered my question that the local macros only exist when it is executing. That is why I use &lt;EM&gt;%put _local_&lt;/EM&gt; and &lt;EM&gt;%put _user_&lt;/EM&gt; outside the macro &lt;EM&gt;%test&lt;/EM&gt; and got nothing in the log message (and that's the part previously I did not understand). While reply&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;'s answer I practiced use &lt;EM&gt;_local_&lt;/EM&gt; inside the macro to display the local ones, now I tried put &lt;EM&gt;_local_&lt;/EM&gt; outside the macro (and the local one does not shows in the log), as follows:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test2;
%global c;
%let c=cvalue;
%let d=dvalue;
%mend test2;
%test2;
%put _global_;
%put _local_;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt; 69         %macro test2;
 70         %global c;
 71         %let c=cvalue;
 72         %let d=dvalue;
 73         %mend test2;
 74         %test2;
 75         %put _global_;
 
 GLOBAL C cvalue

 76         %put _local_;
 77         &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 May 2026 11:42:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/a-suggestion-and-a-question-on-Macro1-m104p05-about-local-and/m-p/988698#M386</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2026-05-27T11:42:33Z</dc:date>
    </item>
    <item>
      <title>Re: a suggestion and a question on Macro1 m104p05 about local and global macro concepts</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/a-suggestion-and-a-question-on-Macro1-m104p05-about-local-and/m-p/988703#M387</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;thanks a lot for your further answer which remind me that I can use &lt;EM&gt;dictionary.macros&lt;/EM&gt; to see the macro variables. I checked out the link of &lt;EM&gt;%symget()&lt;/EM&gt; macro, I think it is good yet a bit too advanced for me at the moment so I would not further explore it, hope I have chances to use these macros in future.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I practiced a bit (of creating macro variables in different ways and use &lt;EM&gt;%put _global_; _local_;&lt;/EM&gt; and &lt;EM&gt;dictionary.macros&lt;/EM&gt; to display the macros) as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(1) create and display global macros (no local macro was created or displayed according to my log and result)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test3;
   input id1 $ id2 $;
   datalines;
e evalue
f fvalue
;
run;
data test4;
   input name $ value $;
   datalines;
g gvalue
h hvalue
;
run;
%let i=ivalue;
data _null_;
   set test3;
   call symputx(id1,id2);
run;
proc sql noprint;
select name,value
   into :name1-,:value1-
   from test4;
quit;
%put _global_;
%put _local_;
proc sql;
select * 
   from dictionary.macros
   where name in ('E','F','G','H','I',
                  'NAME1','NAME2',
                  'VALUE1','VALUE2');
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;93         %put _global_;

 GLOBAL E evalue
 GLOBAL F fvalue

 GLOBAL I ivalue
 GLOBAL NAME1 g
 GLOBAL NAME2 h

 GLOBAL VALUE1 gvalue
 GLOBAL VALUE2 hvalue

94         %put _local_;
&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_0-1779886397044.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/115330i3A675D768D5EA28C/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_0-1779886397044.png" alt="dxiao2017_0-1779886397044.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(2) create and display local macros (no global macro was created or displayed according to my log and result)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test5;
   input id1 $ id2 $;
   datalines;
j jvalue
k kvalue
;
run;
data test6;
   input name $ value $;
   datalines;
l lvalue
m mvalue
;
run;
%macro testmcr;
%let n=nvalue;
data _null_;
   set test5;
   call symputx(id1,id2);
run;
proc sql;
select name,value
   into :mname1-,:mvalue1-
   from test6;
quit;
%put _global_;
%put _local_;
proc sql;
select * from dictionary.macros;
quit;
%mend testmcr;
%testmcr;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt; 99         %mend testmcr;
 100        %testmcr;
 
 NOTE: There were 2 observations read from the data set WORK.TEST5.

 TESTMCR J jvalue
 TESTMCR K kvalue
 TESTMCR MNAME1 l
 TESTMCR MNAME2 m
 TESTMCR MVALUE1 lvalue
 TESTMCR MVALUE2 mvalue
 TESTMCR N nvalue&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_1-1779887730073.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/115331i61DB1CFE87CB69B8/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_1-1779887730073.png" alt="dxiao2017_1-1779887730073.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have not try these (the above) out on positional or keyword macro parameters, or on more complicate ones, for example, the nested macros with both global and local macro variables and parameters, too complicated. Will try them later on.&lt;/P&gt;</description>
      <pubDate>Wed, 27 May 2026 13:24:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/a-suggestion-and-a-question-on-Macro1-m104p05-about-local-and/m-p/988703#M387</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2026-05-27T13:24:17Z</dc:date>
    </item>
    <item>
      <title>Re: a suggestion and a question on Macro1 m104p05 about local and global macro concepts</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/a-suggestion-and-a-question-on-Macro1-m104p05-about-local-and/m-p/988731#M388</link>
      <description>&lt;P&gt;There are a few other wrinkles you have not explored with your examples.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you create parameters for a macro they are LOCAL macro variables to that macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you reference a macro variable SAS will look for that macro starting with the innermost scope and working its way out to the GLOBAL "scope" (note that GLOBAL and AUTOMATIC macro variables are in the same symbol table and so in the same scope even though SAS prints them with different "scope" values when using %PUT keywords or looking at DICTIONARY.MACROS).&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is even true when you CREATE the macro variable.&amp;nbsp; So if you write this line inside a macro:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let i=ivalue;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And the macro variable I already exists in some outer scope (such as GLOBAL) then the value of that macro variable is modified.&amp;nbsp; To avoid this use the %LOCAL statement to create a new macro variable in the local scope before assigning it a value.&amp;nbsp; Or in a DATA step you can skip the %LOCAL statement by using the 'L' modifier when using the CALL SYMPUTX() method to assign the value to a macro variable.&amp;nbsp; That will force the macro variable into the local symbol table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An even stranger wrinkle is that SAS will not create the local symbol table for a macro until is it runs a statement that the SAS macro compiler thinks should cause it to need a local symbol table.&amp;nbsp; This means that any new macros that are created (by using some method that the macro compiler does not notice) will be made in the scope of the calling environment, even if that calling environment is another macro execution and not open code.&amp;nbsp; Try this example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro outer;
	%let myvar=OUTER ;
	%put &amp;amp;=sysmacroname BEFORE;
	%put _local_;
	%inner;
	%put &amp;amp;=sysmacroname AFTER;
	%put _local_;
%mend outer;
%macro inner;
	%put &amp;amp;=sysmacroname BEFORE;
	%put _local_;
	data _null_;
	  call symput('myvar2','inner');
	run;
	%put &amp;amp;=sysmacroname AFTER;
	%put _local_;
%mend inner;
%outer;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Where is the macro variable MYVAR2 created?&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt; SYSMACRONAME=OUTER BEFORE
 OUTER MYVAR OUTER
 SYSMACRONAME=INNER BEFORE
 
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       cpu time            0.00 seconds
       
 
 SYSMACRONAME=INNER AFTER
 SYSMACRONAME=OUTER AFTER
 OUTER MYVAR OUTER
 OUTER MYVAR2 inner&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;Now see what happens if you add a %LET statement to the inner macro.&amp;nbsp; Try first adding it AFTER the DATA step.&amp;nbsp; Where is MYVAR2 created?&lt;/P&gt;
&lt;P&gt;Then try adding it BEFORE the DATA step.&amp;nbsp; Where is MYVAR2 created?&lt;/P&gt;</description>
      <pubDate>Wed, 27 May 2026 15:15:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/a-suggestion-and-a-question-on-Macro1-m104p05-about-local-and/m-p/988731#M388</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-05-27T15:15:45Z</dc:date>
    </item>
  </channel>
</rss>

