<?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 can I make a global macro var in %macro? in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-can-I-make-a-global-macro-var-in-macro/m-p/593799#M15573</link>
    <description>&lt;P&gt;I used %let to define local macro in %macro statement, but it was gone way outside of the macro. Because I would like to use macro variables generated in %macro outside of it, I want to use global macro. But, replacing %global with %let doesn't work for me. Do they work differently?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Relatedly, can I simply re-define local macro variables to global macro ones?&lt;/P&gt;</description>
    <pubDate>Thu, 03 Oct 2019 16:23:02 GMT</pubDate>
    <dc:creator>braam</dc:creator>
    <dc:date>2019-10-03T16:23:02Z</dc:date>
    <item>
      <title>How can I make a global macro var in %macro?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-can-I-make-a-global-macro-var-in-macro/m-p/593799#M15573</link>
      <description>&lt;P&gt;I used %let to define local macro in %macro statement, but it was gone way outside of the macro. Because I would like to use macro variables generated in %macro outside of it, I want to use global macro. But, replacing %global with %let doesn't work for me. Do they work differently?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Relatedly, can I simply re-define local macro variables to global macro ones?&lt;/P&gt;</description>
      <pubDate>Thu, 03 Oct 2019 16:23:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-can-I-make-a-global-macro-var-in-macro/m-p/593799#M15573</guid>
      <dc:creator>braam</dc:creator>
      <dc:date>2019-10-03T16:23:02Z</dc:date>
    </item>
    <item>
      <title>Re: How can I make a global macro var in %macro?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-can-I-make-a-global-macro-var-in-macro/m-p/593803#M15574</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please show the code you have tried.&amp;nbsp; This will make it easier to explain why it's not working.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's a good idea to always use the %LOCAL or %GLOBAL statement to explicitly declare the scope of macro variables (there are rare exceptions to this rule).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes you can use the %GLOBAL statement in a macro to create a global macro variable, for example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro try() ;
  %global x ;
  %let x=1 ;
%mend try ;

%try()

%put &amp;amp;=x ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;No, if you have a local macro variable, you cannot use the %GLOBAL statement to redefine it as global. The below log shows the error that will be generated if you try:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;615  %macro try() ;
616    %local x ;
617    %let x=1 ;
618    %global x ;
619    %let x=1 ;
620  %mend try ;
NOTE: The macro TRY completed compilation without errors.
      15 instructions 236 bytes.
621
622  %try()
ERROR: Attempt to %GLOBAL a name (X) which exists in a local environment.
&lt;/PRE&gt;
&lt;P&gt;HTH,&lt;/P&gt;
&lt;P&gt;-Q.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Oct 2019 16:39:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-can-I-make-a-global-macro-var-in-macro/m-p/593803#M15574</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2019-10-03T16:39:46Z</dc:date>
    </item>
    <item>
      <title>Re: How can I make a global macro var in %macro?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-can-I-make-a-global-macro-var-in-macro/m-p/593804#M15575</link>
      <description>&lt;P&gt;Note that you use %LET to assign values to macro variables, not to create LOCAL or GLOBAL macro variables.&lt;/P&gt;
&lt;P&gt;I suspect you are just confused because if the macro variable you are assigning a value to does not already exist then it will be created as LOCAL.&lt;/P&gt;
&lt;P&gt;Try this&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mymacro;

%if not %symexist(mymvar) then %global mymvar;
%let mymvar=Hi there;

%mend mymacro;


%mymacro;
%put My Macro Varaible = &amp;amp;mymvar;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Oct 2019 16:41:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-can-I-make-a-global-macro-var-in-macro/m-p/593804#M15575</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-03T16:41:13Z</dc:date>
    </item>
  </channel>
</rss>

