<?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: Creating Global Macro Variables within a Macro? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-Global-Macro-Variables-within-a-Macro/m-p/71535#M15483</link>
    <description>&amp;gt; Since your CALL SYMPUT is executed within a macro,&lt;BR /&gt;
&amp;gt; the macro variables created will be local.&lt;BR /&gt;
&lt;BR /&gt;
That is not exactly right.  There are a few "depends on" that affect the action.  Consider the following example.  You can get the exact action you desire with SYMPUTX when you specify the symbol table option.&lt;BR /&gt;
&lt;BR /&gt;
CALL SYMPUTX(macro-variable, value &amp;lt;,symbol-table&amp;gt;); &lt;BR /&gt;
&lt;BR /&gt;
Having said that I don't think GLOBAL macro variables are the best choice here.  I would think about keeping the data in a SAS data set would be better.  I would need to see how these variables are being used to make a recomendation.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
161  options nostimer;&lt;BR /&gt;
162  %symdel a;&lt;BR /&gt;
163&lt;BR /&gt;
164  %macro test(arg);&lt;BR /&gt;
165     data _null_;&lt;BR /&gt;
166        call symput('A','What is my scope');&lt;BR /&gt;
167        run;&lt;BR /&gt;
168     %put _global_;&lt;BR /&gt;
169     %put _local_;&lt;BR /&gt;
170     %mend;&lt;BR /&gt;
171&lt;BR /&gt;
172  %test&lt;BR /&gt;
173&lt;BR /&gt;
174  %symdel a;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
TEST ARG&lt;BR /&gt;
TEST A What is my scope&lt;BR /&gt;
WARNING: Attempt to delete macro variable A failed. Variable not found.&lt;BR /&gt;
175  %macro test;&lt;BR /&gt;
176     data _null_;&lt;BR /&gt;
177        call symput('A','What is my scope');&lt;BR /&gt;
178        run;&lt;BR /&gt;
179     %put _global_;&lt;BR /&gt;
180     %put _local_;&lt;BR /&gt;
181     %mend;&lt;BR /&gt;
182&lt;BR /&gt;
183  %test&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
GLOBAL A What is my scope&lt;BR /&gt;
184&lt;BR /&gt;
185&lt;BR /&gt;
186  %symdel a;&lt;BR /&gt;
187  %global a;&lt;BR /&gt;
188  %macro test(arg);&lt;BR /&gt;
189     data _null_;&lt;BR /&gt;
190        call symput('A','What is my scope');&lt;BR /&gt;
191        run;&lt;BR /&gt;
192     %put _global_;&lt;BR /&gt;
193     %put _local_;&lt;BR /&gt;
194     %mend;&lt;BR /&gt;
195&lt;BR /&gt;
196  %test();&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
GLOBAL A What is my scope&lt;BR /&gt;
TEST ARG&lt;BR /&gt;
[/pre]</description>
    <pubDate>Wed, 28 Jan 2009 15:13:08 GMT</pubDate>
    <dc:creator>data_null__</dc:creator>
    <dc:date>2009-01-28T15:13:08Z</dc:date>
    <item>
      <title>Creating Global Macro Variables within a Macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-Global-Macro-Variables-within-a-Macro/m-p/71532#M15480</link>
      <description>I'm having problems again.&lt;BR /&gt;
&lt;BR /&gt;
I have a piece of code that creates some numbers that I then assign to global macro variables to use later on.  I want to run the piece of code three times, in order to create three versions of the numbers so that I can then use an average of the three numbers rather than the one off.&lt;BR /&gt;
&lt;BR /&gt;
So I turned the code into a macro and it doesn't work.  I tried some much simplified version of the code and that still doesn't work...&lt;BR /&gt;
&lt;BR /&gt;
Here is the simple version:&lt;BR /&gt;
&lt;BR /&gt;
data temp;&lt;BR /&gt;
input xval $ yval;&lt;BR /&gt;
cards;&lt;BR /&gt;
A 1&lt;BR /&gt;
B 2&lt;BR /&gt;
C 3&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
%macro mymacro (loop);&lt;BR /&gt;
&lt;BR /&gt;
data _null_;&lt;BR /&gt;
set temp;&lt;BR /&gt;
macname=compress(xval||&amp;amp;loop);&lt;BR /&gt;
call symput (macname, yval*&amp;amp;loop);&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
%mend;&lt;BR /&gt;
&lt;BR /&gt;
%mymacro (1);&lt;BR /&gt;
%mymacro (2);&lt;BR /&gt;
%mymacro (3);&lt;BR /&gt;
&lt;BR /&gt;
%put _global_;</description>
      <pubDate>Wed, 28 Jan 2009 13:34:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-Global-Macro-Variables-within-a-Macro/m-p/71532#M15480</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-01-28T13:34:00Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Global Macro Variables within a Macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-Global-Macro-Variables-within-a-Macro/m-p/71533#M15481</link>
      <description>Since your CALL SYMPUT is executed within a macro, the macro variables created will be local. Try to insert a CALL EXECUTE after you have assigned your macro variable a name:&lt;BR /&gt;
&lt;BR /&gt;
%macro mymacro (loop);&lt;BR /&gt;
data _null_;&lt;BR /&gt;
set temp;&lt;BR /&gt;
macname=compress(xval||&amp;amp;loop);&lt;BR /&gt;
&lt;B&gt;call execute('%global '||macname||';');&lt;/B&gt;&lt;BR /&gt;
call symput (macname, yval*&amp;amp;loop);&lt;BR /&gt;
run;&lt;BR /&gt;
%mend;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
/Linus</description>
      <pubDate>Wed, 28 Jan 2009 14:24:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-Global-Macro-Variables-within-a-Macro/m-p/71533#M15481</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2009-01-28T14:24:24Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Global Macro Variables within a Macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-Global-Macro-Variables-within-a-Macro/m-p/71534#M15482</link>
      <description>Refer to this SAS support website  &lt;A href="http://support.sas.com/" target="_blank"&gt;http://support.sas.com/&lt;/A&gt;  DOC reference (9.2 but it applies to 9.1) on SYMPUT resolution, local and global.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Scopes of Macro Variables &lt;BR /&gt;
Special Cases of Scope with the CALL SYMPUT Routine&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/59526/HTML/default/tw3514-symput.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/mcrolref/59526/HTML/default/tw3514-symput.htm&lt;/A&gt;</description>
      <pubDate>Wed, 28 Jan 2009 14:34:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-Global-Macro-Variables-within-a-Macro/m-p/71534#M15482</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-01-28T14:34:36Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Global Macro Variables within a Macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-Global-Macro-Variables-within-a-Macro/m-p/71535#M15483</link>
      <description>&amp;gt; Since your CALL SYMPUT is executed within a macro,&lt;BR /&gt;
&amp;gt; the macro variables created will be local.&lt;BR /&gt;
&lt;BR /&gt;
That is not exactly right.  There are a few "depends on" that affect the action.  Consider the following example.  You can get the exact action you desire with SYMPUTX when you specify the symbol table option.&lt;BR /&gt;
&lt;BR /&gt;
CALL SYMPUTX(macro-variable, value &amp;lt;,symbol-table&amp;gt;); &lt;BR /&gt;
&lt;BR /&gt;
Having said that I don't think GLOBAL macro variables are the best choice here.  I would think about keeping the data in a SAS data set would be better.  I would need to see how these variables are being used to make a recomendation.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
161  options nostimer;&lt;BR /&gt;
162  %symdel a;&lt;BR /&gt;
163&lt;BR /&gt;
164  %macro test(arg);&lt;BR /&gt;
165     data _null_;&lt;BR /&gt;
166        call symput('A','What is my scope');&lt;BR /&gt;
167        run;&lt;BR /&gt;
168     %put _global_;&lt;BR /&gt;
169     %put _local_;&lt;BR /&gt;
170     %mend;&lt;BR /&gt;
171&lt;BR /&gt;
172  %test&lt;BR /&gt;
173&lt;BR /&gt;
174  %symdel a;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
TEST ARG&lt;BR /&gt;
TEST A What is my scope&lt;BR /&gt;
WARNING: Attempt to delete macro variable A failed. Variable not found.&lt;BR /&gt;
175  %macro test;&lt;BR /&gt;
176     data _null_;&lt;BR /&gt;
177        call symput('A','What is my scope');&lt;BR /&gt;
178        run;&lt;BR /&gt;
179     %put _global_;&lt;BR /&gt;
180     %put _local_;&lt;BR /&gt;
181     %mend;&lt;BR /&gt;
182&lt;BR /&gt;
183  %test&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
GLOBAL A What is my scope&lt;BR /&gt;
184&lt;BR /&gt;
185&lt;BR /&gt;
186  %symdel a;&lt;BR /&gt;
187  %global a;&lt;BR /&gt;
188  %macro test(arg);&lt;BR /&gt;
189     data _null_;&lt;BR /&gt;
190        call symput('A','What is my scope');&lt;BR /&gt;
191        run;&lt;BR /&gt;
192     %put _global_;&lt;BR /&gt;
193     %put _local_;&lt;BR /&gt;
194     %mend;&lt;BR /&gt;
195&lt;BR /&gt;
196  %test();&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
GLOBAL A What is my scope&lt;BR /&gt;
TEST ARG&lt;BR /&gt;
[/pre]</description>
      <pubDate>Wed, 28 Jan 2009 15:13:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-Global-Macro-Variables-within-a-Macro/m-p/71535#M15483</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2009-01-28T15:13:08Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Global Macro Variables within a Macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-Global-Macro-Variables-within-a-Macro/m-p/71536#M15484</link>
      <description>If you need a SAS global macro variable, simply move the RUN; statement to be outside the SAS macro definition.  The DOC I referenced shows this condition in action.  I tested your code and that works great.  Amazing what the DOC can do for you, when referenced.  And I found it by doing a Google advanced search below:&lt;BR /&gt;
&lt;BR /&gt;
global macro variable data step site:sas.com&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Wed, 28 Jan 2009 18:30:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-Global-Macro-Variables-within-a-Macro/m-p/71536#M15484</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-01-28T18:30:06Z</dc:date>
    </item>
  </channel>
</rss>

