<?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: Macro variable not refreshing at run-time in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-refreshing-at-run-time/m-p/13102#M1532</link>
    <description>kdp &lt;BR /&gt;
 &lt;BR /&gt;
no, you can develop your solution using sql, unless of course, you are changing the definition of the problem!&lt;BR /&gt;
if the version of your macro that you posted earlier is what you expect to use, only the way you use it would need to change.&lt;BR /&gt;
Your macro places values into 3 macro variables&lt;BR /&gt;
 DRV_CNT, DRVS and DRVS_NAME &lt;BR /&gt;
These you use to specify the size of an array, provide the element names for the array, and provide a list of quoted names.&lt;BR /&gt;
So I see no need for these macro variable names to vary.&lt;BR /&gt;
define a parametrer for your macro&lt;BR /&gt;
%macro iLikeThisMacro( forecast_input_data_set ) ;&lt;BR /&gt;
To use your macro within a call-execute context [pre]&lt;BR /&gt;
   %let DRV_CNT = ;&lt;BR /&gt;
   %let DRVS = ;&lt;BR /&gt;
   %let DRVS_NAME = ;&lt;BR /&gt;
 data _null_ ;&lt;BR /&gt;
  set filelist ; * providing the name of forecast dataset in var DSN;&lt;BR /&gt;
  putlog 'invoke for ' dsn= ;&lt;BR /&gt;
  call execute( '%nrstr(%%iLikeThisMacro)' ); * deferring execution until after;&lt;BR /&gt;
  call execute( '(' !! dsn !! ')' ) ;&lt;BR /&gt;
run ;[/pre]You might want to protect for the condition where your macro extracts no names so DRV_CNT becomes zero.&lt;BR /&gt;
 &lt;BR /&gt;
Many times I have used sql to fill macro variables with lists of values, while running in a macro, so I can assure you that it can be made to work.&lt;BR /&gt;
&lt;BR /&gt;
Scott&lt;BR /&gt;
destroying any pre-existing value in the macro variable seemed important to do, in this situation where the process needs to place a value into these macro variables, and  avoid inheriting any previous iterations' values.&lt;BR /&gt;
 &lt;BR /&gt;
PeterC</description>
    <pubDate>Fri, 06 Nov 2009 22:58:09 GMT</pubDate>
    <dc:creator>Peter_C</dc:creator>
    <dc:date>2009-11-06T22:58:09Z</dc:date>
    <item>
      <title>Macro variable not refreshing at run-time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-refreshing-at-run-time/m-p/13095#M1525</link>
      <description>Hello,&lt;BR /&gt;
&lt;BR /&gt;
I am having a problem with refreshing macro variables using proc sql inside a macro program. I have tried to resolve this 3 different times before and have given up each time, but I need this to work. Here's my code:&lt;BR /&gt;
&lt;BR /&gt;
%macro ILikeThisMacro;&lt;BR /&gt;
       do something;&lt;BR /&gt;
      &lt;BR /&gt;
      proc sql noprint;&lt;BR /&gt;
&lt;BR /&gt;
	select count(distinct(name))&lt;BR /&gt;
	into :drv_cnt&lt;BR /&gt;
	from dictionary.columns&lt;BR /&gt;
	where memname = upcase("&amp;amp;forecast_input_data_set")&lt;BR /&gt;
	and varnum &amp;gt; 11;&lt;BR /&gt;
&lt;BR /&gt;
	select distinct name&lt;BR /&gt;
	into :drvs separated by " "&lt;BR /&gt;
	from dictionary.columns&lt;BR /&gt;
	where memname = upcase("&amp;amp;forecast_input_data_set")&lt;BR /&gt;
	and varnum &amp;gt; 11;&lt;BR /&gt;
&lt;BR /&gt;
	select distinct quote(trim(left(name)))&lt;BR /&gt;
	into :drvs_name separated by ","&lt;BR /&gt;
	from dictionary.columns&lt;BR /&gt;
	where memname = upcase("&amp;amp;forecast_input_data_set")&lt;BR /&gt;
	and varnum &amp;gt; 11;&lt;BR /&gt;
&lt;BR /&gt;
	quit;&lt;BR /&gt;
&lt;BR /&gt;
	data ldp.&amp;amp;forecast_input_data_set (drop= i);&lt;BR /&gt;
	       set ldp.&amp;amp;forecast_input_data_set;&lt;BR /&gt;
		&lt;BR /&gt;
	       array driv_cols(&amp;amp;drv_cnt) &amp;amp;drvs;&lt;BR /&gt;
	       array driv_list(&amp;amp;drv_cnt) $ _temporary_ (&amp;amp;drvs_name);&lt;BR /&gt;
&lt;BR /&gt;
	       do i = 1 to &amp;amp;drv_cnt;&lt;BR /&gt;
		if find(forecast_drivers, driv_list(i)) &amp;lt; 1 then driv_cols(i) = 0;&lt;BR /&gt;
	       end;&lt;BR /&gt;
&lt;BR /&gt;
	run;&lt;BR /&gt;
&lt;BR /&gt;
        do something;&lt;BR /&gt;
&lt;BR /&gt;
%mend ILikeThisMacro;&lt;BR /&gt;
&lt;BR /&gt;
data _null_;&lt;BR /&gt;
      set filelist;&lt;BR /&gt;
      call execute %ILikeThisMacro.....;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Basically, I run the "ILikeThisMacro" for multiple files and each time the proc sql should refresh the following macro variables: &amp;amp;drv_cnt, &amp;amp;drvs, and &amp;amp;drvs_name.&lt;BR /&gt;
However, it seems that the macro variables are being created at compile time and then those same values are being used for every instance of the "ILikeThisMacro".&lt;BR /&gt;
&lt;BR /&gt;
When the proc sql is not enclosed inside a macro program and is run once, it works BEAUTIFULLY!&lt;BR /&gt;
&lt;BR /&gt;
Any suggestions?&lt;BR /&gt;
&lt;BR /&gt;
Thanks,&lt;BR /&gt;
kdp</description>
      <pubDate>Thu, 05 Nov 2009 23:40:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-refreshing-at-run-time/m-p/13095#M1525</guid>
      <dc:creator>kdp</dc:creator>
      <dc:date>2009-11-05T23:40:01Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable not refreshing at run-time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-refreshing-at-run-time/m-p/13096#M1526</link>
      <description>You either must pass your macro variable values at macro invocation time, or you must declare global macro variables in your DATA step execution explicitly.&lt;BR /&gt;
&lt;BR /&gt;
Best that you can see the logic / flow processing by turning on diagnostics with:&lt;BR /&gt;
&lt;BR /&gt;
OPTIONS SOURCE SOURCE2 MACROGEN SYMBOLGEN MLOGIC MPRINT;&lt;BR /&gt;
&lt;BR /&gt;
This additional expanded SAS code will help with debugging your program.&lt;BR /&gt;
&lt;BR /&gt;
The bottom-line is that a CALL EXECUTE happens "after" the DATA step completes so its up to your program to create the macro variable (value) environment with each DATA step iteration by supplying those meaningful variable values you need with each execution of your macro .&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Fri, 06 Nov 2009 00:52:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-refreshing-at-run-time/m-p/13096#M1526</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-11-06T00:52:44Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable not refreshing at run-time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-refreshing-at-run-time/m-p/13097#M1527</link>
      <description>kdp,&lt;BR /&gt;
Scott is pointing at the issue.&lt;BR /&gt;
&lt;BR /&gt;
SQL into :mvar&lt;BR /&gt;
&lt;BR /&gt;
Inside a macro, this is declared by the SQL Macro Language Reference 9.2 as being subject to the same rules as %LET in selecting the symbol table where the value will be stored.. When  referred to for the first time, a macro variable is created in the local (or nearest) symbol table. &lt;BR /&gt;
Scott and I are guessing this is the problem here.  &lt;BR /&gt;
Rather than defining these as GLOBAL I would use %LET to define the macro variables just before you invoke %iLikeThisMacro&lt;BR /&gt;
 &lt;BR /&gt;
good luck&lt;BR /&gt;
PeterC</description>
      <pubDate>Fri, 06 Nov 2009 09:24:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-refreshing-at-run-time/m-p/13097#M1527</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2009-11-06T09:24:07Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable not refreshing at run-time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-refreshing-at-run-time/m-p/13098#M1528</link>
      <description>In essence, when declaring a %LET statement outside a macro, one is defining a SAS global type macro variable - which is what I meant to convey.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Fri, 06 Nov 2009 09:45:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-refreshing-at-run-time/m-p/13098#M1528</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-11-06T09:45:20Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable not refreshing at run-time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-refreshing-at-run-time/m-p/13099#M1529</link>
      <description>Scott&lt;BR /&gt;
 &lt;BR /&gt;
rather than using %GLOBAL, I prefer and recommend using %LET in the calling environment, because often a calling environment is not global. &lt;BR /&gt;
There is added advantage (reducing risk) in avoiding the possibility of error occuring inside a macro when making global a name that is already defined with a local scope.&lt;BR /&gt;
 &lt;BR /&gt;
PeterC</description>
      <pubDate>Fri, 06 Nov 2009 14:52:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-refreshing-at-run-time/m-p/13099#M1529</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2009-11-06T14:52:39Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable not refreshing at run-time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-refreshing-at-run-time/m-p/13100#M1530</link>
      <description>A %LET destroys the macro variable if it already exists - %GLOBAL declaration does not.  The purpose and opportunity for each approach is situation/application specific.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Fri, 06 Nov 2009 16:06:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-refreshing-at-run-time/m-p/13100#M1530</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-11-06T16:06:12Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable not refreshing at run-time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-refreshing-at-run-time/m-p/13101#M1531</link>
      <description>Thanks Scott and Peter!&lt;BR /&gt;
&lt;BR /&gt;
I think the lesson is that I can't use proc sql to generate macro variables inside a macro. I would have to pass them as parameters when I am invoking the macro.&lt;BR /&gt;
&lt;BR /&gt;
However, in my case I don't think either %LET or %GLOBAL would work since I need to pass different values for the macro variables for each invocation of the macro.&lt;BR /&gt;
&lt;BR /&gt;
I have started to write some code that would figure out the exact values to pass to the macro and I think I am almost there!&lt;BR /&gt;
&lt;BR /&gt;
Thanks again!&lt;BR /&gt;
kdp</description>
      <pubDate>Fri, 06 Nov 2009 17:49:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-refreshing-at-run-time/m-p/13101#M1531</guid>
      <dc:creator>kdp</dc:creator>
      <dc:date>2009-11-06T17:49:21Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable not refreshing at run-time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-refreshing-at-run-time/m-p/13102#M1532</link>
      <description>kdp &lt;BR /&gt;
 &lt;BR /&gt;
no, you can develop your solution using sql, unless of course, you are changing the definition of the problem!&lt;BR /&gt;
if the version of your macro that you posted earlier is what you expect to use, only the way you use it would need to change.&lt;BR /&gt;
Your macro places values into 3 macro variables&lt;BR /&gt;
 DRV_CNT, DRVS and DRVS_NAME &lt;BR /&gt;
These you use to specify the size of an array, provide the element names for the array, and provide a list of quoted names.&lt;BR /&gt;
So I see no need for these macro variable names to vary.&lt;BR /&gt;
define a parametrer for your macro&lt;BR /&gt;
%macro iLikeThisMacro( forecast_input_data_set ) ;&lt;BR /&gt;
To use your macro within a call-execute context [pre]&lt;BR /&gt;
   %let DRV_CNT = ;&lt;BR /&gt;
   %let DRVS = ;&lt;BR /&gt;
   %let DRVS_NAME = ;&lt;BR /&gt;
 data _null_ ;&lt;BR /&gt;
  set filelist ; * providing the name of forecast dataset in var DSN;&lt;BR /&gt;
  putlog 'invoke for ' dsn= ;&lt;BR /&gt;
  call execute( '%nrstr(%%iLikeThisMacro)' ); * deferring execution until after;&lt;BR /&gt;
  call execute( '(' !! dsn !! ')' ) ;&lt;BR /&gt;
run ;[/pre]You might want to protect for the condition where your macro extracts no names so DRV_CNT becomes zero.&lt;BR /&gt;
 &lt;BR /&gt;
Many times I have used sql to fill macro variables with lists of values, while running in a macro, so I can assure you that it can be made to work.&lt;BR /&gt;
&lt;BR /&gt;
Scott&lt;BR /&gt;
destroying any pre-existing value in the macro variable seemed important to do, in this situation where the process needs to place a value into these macro variables, and  avoid inheriting any previous iterations' values.&lt;BR /&gt;
 &lt;BR /&gt;
PeterC</description>
      <pubDate>Fri, 06 Nov 2009 22:58:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-refreshing-at-run-time/m-p/13102#M1532</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2009-11-06T22:58:09Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable not refreshing at run-time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-refreshing-at-run-time/m-p/13103#M1533</link>
      <description>Thanks Peter, I finally had a chance to implement your suggestion and it WORKS!!!.&lt;BR /&gt;
&lt;BR /&gt;
I had to consult other SAS experts around me on exactly what your code was doing, but now I understand it.&lt;BR /&gt;
&lt;BR /&gt;
Thanks for your help!&lt;BR /&gt;
Ketan</description>
      <pubDate>Wed, 11 Nov 2009 21:31:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-refreshing-at-run-time/m-p/13103#M1533</guid>
      <dc:creator>kdp</dc:creator>
      <dc:date>2009-11-11T21:31:53Z</dc:date>
    </item>
  </channel>
</rss>

