<?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: Updating global macro variable using macros in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Updating-global-macro-variable-using-macros/m-p/363980#M23882</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/120587"&gt;@ayin&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;The code you've posted works as such.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can replicate what you describe when I use values in the SQL where clause which don't return a single row. In such a case the macro variable doesn't get altered (I'd prefer if it would be set to missing but that's not what SAS does).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Check your log. You should see something like:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;NOTE: No rows were selected.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;27         %let macroValue = 3;
28         
29         %macro test();
30           %global macroValue;
31         
32           proc sql noprint;
33             select name into: macroValue separated by ' '
34               from dictionary.columns
35                 where libname = 'WORK' and memname contains 'TEST';
36           quit;
37         
38         %mend test;
39         
40         %test;
NOTE: No rows were selected.
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      

41         %put &amp;amp;=macroValue;
MACROVALUE=3&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The moment your SQL returns rows you'll get the result you'd expect&lt;/P&gt;
&lt;PRE&gt;27         %let macroValue = 3;
28         
29         %macro test();
30           %global macroValue;
31         
32           proc sql noprint;
33             select name into: macroValue separated by ' '
34               from dictionary.columns
35                 where libname = 'SASHELP' and memname contains 'CLA';
36           quit;
37         
38         %mend test;
39         
40         %test;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.02 seconds
      cpu time            0.01 seconds
      

41         %put &amp;amp;=macroValue;
MACROVALUE=Name Sex Age Height Weight Name Sex Age Height Weight predict lowermean uppermean lower upper&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What above log also shows you: Be careful with &lt;EM&gt;contains&lt;/EM&gt;&amp;nbsp;like used in &lt;EM&gt;and memname contains.&lt;/EM&gt; Using contains can match with multiple table names and though return a list of variable names from multiple tables. Is that what you want to happen?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 03 Jun 2017 02:04:48 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2017-06-03T02:04:48Z</dc:date>
    <item>
      <title>Updating global macro variable using macros</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Updating-global-macro-variable-using-macros/m-p/363692#M23851</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let macroValue = 3;&lt;BR /&gt;
%macro test();
%global macroValue;
proc sql noprint;
select name into: macroValue separated by ' '
from dictionary.columns
where libname = 'WORK' and memname contains 'TEST';
quit;
%mend test;&lt;BR /&gt;
%test;
%put _all_;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I'm trying to update the value of the macro variable 'macroValue' using a macro named 'test'&lt;/P&gt;&lt;P&gt;the dataset work.test have columns like 'V1' 'V2',&amp;nbsp;so the updated value for that macro variable should be 'V1 V2', but apparently it's still '3'.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone help fix it up? Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2017 04:20:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Updating-global-macro-variable-using-macros/m-p/363692#M23851</guid>
      <dc:creator>ayin</dc:creator>
      <dc:date>2017-06-02T04:20:36Z</dc:date>
    </item>
    <item>
      <title>Re: Updating global macro variable using macros</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Updating-global-macro-variable-using-macros/m-p/363694#M23852</link>
      <description>&lt;P&gt;I did exactly same thing and it worked for me. below is the code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%let macroValue = 3;
%macro test();
%global macroValue1;
proc sql print;
select name into :macroValue separated by ' '
from dictionary.columns
where upcase(libname) = 'SASHELP'
and upcase(memname) ='CARS';
quit;
%mend test;&lt;BR /&gt;%test&lt;BR /&gt;%put &amp;amp;macrovalue;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;output i get is&amp;nbsp;&lt;/P&gt;
&lt;DIV class="sasSource"&gt;Make Model Type Origin DriveTrain MSRP Invoice EngineSize Cylinders Horsepower MPG_City MPG_Highway Weight Wheelbase&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am not sure whether below mentioned is working or not&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;memname contains &lt;SPAN class="token string"&gt;'TEST'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;CODE&gt;&lt;/CODE&gt;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;&amp;nbsp;&amp;nbsp;&lt;CODE&gt;&lt;/CODE&gt;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2017 04:48:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Updating-global-macro-variable-using-macros/m-p/363694#M23852</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2017-06-02T04:48:00Z</dc:date>
    </item>
    <item>
      <title>Re: Updating global macro variable using macros</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Updating-global-macro-variable-using-macros/m-p/363695#M23853</link>
      <description>&lt;P&gt;Post the log.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2017 05:44:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Updating-global-macro-variable-using-macros/m-p/363695#M23853</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-06-02T05:44:33Z</dc:date>
    </item>
    <item>
      <title>Re: Updating global macro variable using macros</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Updating-global-macro-variable-using-macros/m-p/363714#M23856</link>
      <description>&lt;P&gt;Why? &amp;nbsp;Two reasons, firstly macro language does nothing on its own, hence knowing when to use it is a big part of it. &amp;nbsp;What you have presented here does nothing - i.e. you can put this bit:&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token statement"&gt;select&lt;/SPAN&gt; name &lt;SPAN class="token keyword"&gt;from&lt;/SPAN&gt; dictionary&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;columns&lt;/SPAN&gt;
&lt;SPAN class="token statement"&gt;where&lt;/SPAN&gt; &lt;SPAN class="token statement"&gt;libname&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;'WORK'&lt;/SPAN&gt; and memname contains &lt;SPAN class="token string"&gt;'TEST'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Into a sub-query and use it in normal base SAS code, keeping your code nice and simple and straight forward.&lt;/P&gt;
&lt;P&gt;Secondly, scope is there for a reason. &amp;nbsp;If you start defining global variables within macros, or altering macro variables outside their scope, you can end up with a truly awfull mess to try debug. &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2017 08:26:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Updating-global-macro-variable-using-macros/m-p/363714#M23856</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-06-02T08:26:04Z</dc:date>
    </item>
    <item>
      <title>Re: Updating global macro variable using macros</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Updating-global-macro-variable-using-macros/m-p/363839#M23875</link>
      <description>&lt;P&gt;Since we do not have the contents of your library it is extremely hard to actually determine why your code may not work.&lt;/P&gt;
&lt;P&gt;When I use the SASHELP library I get the explected result from your code though:&lt;/P&gt;
&lt;PRE&gt;%let macroValue = 3;
%macro test();
%global macroValue;
proc sql noprint;
select name into: macroValue separated by ' '
from dictionary.columns
where libname = 'SASHELP' and memname contains 'CLASS';
quit;
%mend test;
%test;

%put &amp;amp;macrovalue;&lt;/PRE&gt;
&lt;P&gt;Yields:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Name Sex Age Height Weight Name Sex Age Height Weight predict lowermean uppermean lower upper&lt;/P&gt;
&lt;P&gt;as expected.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW why is there a RUN statement in your code example? No proc or data step was used. Perhaps you did not share all of the code and the bit you didn't show also modifies Macrovalue?&lt;/P&gt;</description>
      <pubDate>Mon, 05 Jun 2017 16:50:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Updating-global-macro-variable-using-macros/m-p/363839#M23875</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-06-05T16:50:41Z</dc:date>
    </item>
    <item>
      <title>Re: Updating global macro variable using macros</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Updating-global-macro-variable-using-macros/m-p/363980#M23882</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/120587"&gt;@ayin&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;The code you've posted works as such.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can replicate what you describe when I use values in the SQL where clause which don't return a single row. In such a case the macro variable doesn't get altered (I'd prefer if it would be set to missing but that's not what SAS does).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Check your log. You should see something like:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;NOTE: No rows were selected.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;27         %let macroValue = 3;
28         
29         %macro test();
30           %global macroValue;
31         
32           proc sql noprint;
33             select name into: macroValue separated by ' '
34               from dictionary.columns
35                 where libname = 'WORK' and memname contains 'TEST';
36           quit;
37         
38         %mend test;
39         
40         %test;
NOTE: No rows were selected.
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      

41         %put &amp;amp;=macroValue;
MACROVALUE=3&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The moment your SQL returns rows you'll get the result you'd expect&lt;/P&gt;
&lt;PRE&gt;27         %let macroValue = 3;
28         
29         %macro test();
30           %global macroValue;
31         
32           proc sql noprint;
33             select name into: macroValue separated by ' '
34               from dictionary.columns
35                 where libname = 'SASHELP' and memname contains 'CLA';
36           quit;
37         
38         %mend test;
39         
40         %test;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.02 seconds
      cpu time            0.01 seconds
      

41         %put &amp;amp;=macroValue;
MACROVALUE=Name Sex Age Height Weight Name Sex Age Height Weight predict lowermean uppermean lower upper&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What above log also shows you: Be careful with &lt;EM&gt;contains&lt;/EM&gt;&amp;nbsp;like used in &lt;EM&gt;and memname contains.&lt;/EM&gt; Using contains can match with multiple table names and though return a list of variable names from multiple tables. Is that what you want to happen?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 03 Jun 2017 02:04:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Updating-global-macro-variable-using-macros/m-p/363980#M23882</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-06-03T02:04:48Z</dc:date>
    </item>
    <item>
      <title>Re: Updating global macro variable using macros</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Updating-global-macro-variable-using-macros/m-p/364088#M23885</link>
      <description>&lt;P&gt;If you want to update the macro variable when no results match the SQL query you can test the automatic macro variable SQLOBS.&lt;/P&gt;
&lt;P&gt;Let's update your macro to make it a little more robust.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test(memname,libname=work,mvar=namelist);
%local dummy ;
%if not %symexist(&amp;amp;mvar) %then %global &amp;amp;mvar ;
proc sql noprint;
  select varnum, name
    into :dummy,:&amp;amp;mvar separated by ' '
  from dictionary.columns
  where libname = %upcase("&amp;amp;libname")
    and memname = %upcase("&amp;amp;memname")
  order by varnum
  ;
%if 0=&amp;amp;sqlobs %then %let &amp;amp;mvar=;
quit;
%mend test;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now let's test it with an dataset that exists.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  length v1 v2 $8 ;
  array c _character_;
  stop;
run;

%test(test);
%put &amp;amp;=namelist;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So we get the expected result:&lt;/P&gt;
&lt;PRE&gt;5530  %put &amp;amp;=namelist;
NAMELIST=v1 v2
&lt;/PRE&gt;
&lt;P&gt;Now let's try it with a dataset that doesn't exist.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc delete data=test; run;
%test(test);
%put &amp;amp;=namelist;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And you can see that &amp;amp;NAMELIST is now empty.&lt;/P&gt;
&lt;PRE&gt;5534  %put &amp;amp;=namelist;
NAMELIST=
&lt;/PRE&gt;</description>
      <pubDate>Sun, 04 Jun 2017 04:49:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Updating-global-macro-variable-using-macros/m-p/364088#M23885</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-06-04T04:49:33Z</dc:date>
    </item>
    <item>
      <title>Re: Updating global macro variable using macros</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Updating-global-macro-variable-using-macros/m-p/364176#M23886</link>
      <description>The problem has been found. Should've used double marks: "memname contains upcase("&amp;amp;fileName")", rather than "memname contains upcase('&amp;amp;fileName'). Thank you guys!</description>
      <pubDate>Sun, 04 Jun 2017 23:22:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Updating-global-macro-variable-using-macros/m-p/364176#M23886</guid>
      <dc:creator>ayin</dc:creator>
      <dc:date>2017-06-04T23:22:10Z</dc:date>
    </item>
  </channel>
</rss>

