<?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 to use macro variable that was created in another macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-variable-that-was-created-in-another-macro/m-p/945134#M370307</link>
    <description>&lt;P&gt;I've a test_mcr1 where I am creating a macro variable, and I need to use that macro variable in test_mcr2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro test_mcr1 (user, secrete);&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; %let conn_creds=user="&amp;amp;user" password="&amp;amp;secrete";&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;test_mcr1 (user, host);&lt;/P&gt;&lt;P&gt;%macro test_mcr2 ();&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;test_mcr1 (mngr, some_pwd);&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;SAS&lt;/STRONG&gt; &lt;STRONG&gt;code (below two lines in sas code)&lt;/STRONG&gt;:&lt;/P&gt;&lt;P&gt;%test_mcr2();&lt;/P&gt;&lt;P&gt;libname&amp;nbsp; libref odbc &amp;amp;conn_creds. dsn=xyz;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;NOTE:&amp;nbsp;test_mcr1&amp;nbsp; macro run/compiles during autoexec run.&lt;/P&gt;</description>
    <pubDate>Wed, 25 Sep 2024 02:32:39 GMT</pubDate>
    <dc:creator>SRAAM</dc:creator>
    <dc:date>2024-09-25T02:32:39Z</dc:date>
    <item>
      <title>How to use macro variable that was created in another macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-variable-that-was-created-in-another-macro/m-p/945134#M370307</link>
      <description>&lt;P&gt;I've a test_mcr1 where I am creating a macro variable, and I need to use that macro variable in test_mcr2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro test_mcr1 (user, secrete);&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; %let conn_creds=user="&amp;amp;user" password="&amp;amp;secrete";&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;test_mcr1 (user, host);&lt;/P&gt;&lt;P&gt;%macro test_mcr2 ();&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;test_mcr1 (mngr, some_pwd);&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;SAS&lt;/STRONG&gt; &lt;STRONG&gt;code (below two lines in sas code)&lt;/STRONG&gt;:&lt;/P&gt;&lt;P&gt;%test_mcr2();&lt;/P&gt;&lt;P&gt;libname&amp;nbsp; libref odbc &amp;amp;conn_creds. dsn=xyz;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;NOTE:&amp;nbsp;test_mcr1&amp;nbsp; macro run/compiles during autoexec run.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Sep 2024 02:32:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-variable-that-was-created-in-another-macro/m-p/945134#M370307</guid>
      <dc:creator>SRAAM</dc:creator>
      <dc:date>2024-09-25T02:32:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to use macro variable that was created in another macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-variable-that-was-created-in-another-macro/m-p/945137#M370309</link>
      <description>&lt;P&gt;Per default a macro variable created within a macro is local in scope. To use a macro variable outside of the macro where it gets created and populated it must be global.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Add below line to your macro definition:&lt;/P&gt;
&lt;PRE&gt;%global conn_creds;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Sep 2024 04:46:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-variable-that-was-created-in-another-macro/m-p/945137#M370309</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-09-25T04:46:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to use macro variable that was created in another macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-variable-that-was-created-in-another-macro/m-p/945277#M370368</link>
      <description>&lt;P&gt;When you assign a value to a macro variable that does not already exist then one is created.&amp;nbsp; If you do that inside a macro then it is created as LOCAL to the macro.&amp;nbsp; So once the macro finishes running it is gone.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you have two solutions.&lt;/P&gt;
&lt;P&gt;1) Make sure the macro variable already exists before calling the macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test_mcr1 (user, secrete);
    %let conn_creds=user="&amp;amp;user" password="&amp;amp;secrete";
%mend;
%let conn_creds=before macro call;
%test_mcr1(user=FRED,secrete=MYPASS);
%put &amp;amp;=conn_creds;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;2) Make the macro smart enough to make a GLOBAL macro variable if one does not already exist.&amp;nbsp; If you use this method remember to test if there is already a macro variable before trying to make a GLOBAL macro variable.&amp;nbsp; That is because if there already a LOCAL macro variable (perhaps in some other currently running macro that called this one) then trying to make a GLOBAL macro variable will generate an error.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test_mcr1 (user, secrete);
%if not %symexist(conn_creds) %then %global conn_creds;
    %let conn_creds=user="&amp;amp;user" password="&amp;amp;secrete";
%mend;
%test_mcr1(user=FRED,secrete=MYPASS);
%put &amp;amp;=conn_creds;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;But perhaps for your use case you don't really need a macro variable at all.&amp;nbsp; Instead you could make a macro you can call in the middle of the LIBNAME (or PROC SQL CONNECT statement).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro conn_creds;
user="FRED" password="MYPASS"
%mend conn_creds;
libname libref odbc %conn_creds dsn=xyz;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Make sure the macro does not emit any semicolons.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Sep 2024 20:53:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-variable-that-was-created-in-another-macro/m-p/945277#M370368</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-09-25T20:53:13Z</dc:date>
    </item>
  </channel>
</rss>

