<?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: Help with Interpreting the Resolving of Macro variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Interpreting-the-Resolving-of-Macro-variable/m-p/774621#M246211</link>
    <description>&lt;P&gt;Indirect referencing, with the multiple &amp;amp;&amp;amp;&amp;amp;&amp;amp;&amp;amp; type calls is basically the leftmost &amp;amp; (or multiples) hold off resolving until the ones to the right resolve.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The question is why did you use the&lt;/P&gt;
&lt;PRE&gt; into :M_&amp;amp;&amp;amp;analysisvar&lt;/PRE&gt;
&lt;P&gt;in the first place&lt;/P&gt;
&lt;P&gt;When I run your code the log shows:&lt;/P&gt;
&lt;PRE&gt;11   %let analysisvar = MPG_City;
12
13   proc sql ;
14      select mean(&amp;amp;analysisvar) into :M_&amp;amp;&amp;amp;analysisvar
15      from sashelp.cars;
16      quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


WARNING: Apparent symbolic reference M_ not resolved.&lt;BR /&gt;18 %put &amp;amp;M_&amp;amp;ANALYSISVAR; /*This does NOT resolve to the mean.*/&lt;BR /&gt;&amp;amp;M_MPG_City&lt;BR /&gt;
&lt;/PRE&gt;
&lt;P&gt;That particular warning basically means you have attempted to use a macro variable that doesn't exist, which is why it shows the &amp;amp;M_MPG_City in the log. You have implied that you have two macro variables, M_ and Analysisvar.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you use the&lt;/P&gt;
&lt;PRE&gt;put &amp;amp;&amp;amp;M_&amp;amp;ANALYSISVAR;&lt;/PRE&gt;
&lt;P&gt;The first &amp;amp; holds off resolving and generates the equivalent of &amp;amp;M_MPG_CITY, which is the macro variable created.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can see which macro variables you have defined currently in the scope you execute it with and their value by using&lt;/P&gt;
&lt;PRE&gt;%put _user_;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 15 Oct 2021 22:31:20 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2021-10-15T22:31:20Z</dc:date>
    <item>
      <title>Help with Interpreting the Resolving of Macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Interpreting-the-Resolving-of-Macro-variable/m-p/774619#M246210</link>
      <description>&lt;P&gt;I am writing a macro program in which I want to be able to call the grand mean of a variable to use as a reference line. (In my example code here, I'm using a statement that will go into the macro, which will be called by CALL EXECUTE in order to make multiple charts.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%let analysisvar = MPG_City;

proc sql ;
   select mean(&amp;amp;analysisvar) into :M_&amp;amp;&amp;amp;analysisvar
   from sashelp.cars;
   quit;

%put &amp;amp;M_&amp;amp;ANALYSISVAR; /*This does NOT resolve to the mean.*/

%put &amp;amp;&amp;amp;M_&amp;amp;ANALYSISVAR; /*This DOES resolve to the mean but why?*/&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I thought what is happening is that the PROC SQL statement is creating a macro variable &amp;amp;M_&amp;amp;AnalysisVar, but that does not appear to be the case.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would have expected %put &amp;amp;M_&amp;amp;AnalysisVar to resolve to the actual mean, but instead it resolves to &amp;amp;M_MPG_City. I'm trying to wrap my mind around why &amp;amp;&amp;amp;M_AnalysisVar resolves to the numeric value I want. I was wondering if someone knew the steps in which the ampersands are being resolved in PROC SQL in this way of assigning a value to a macro.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Oct 2021 22:05:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Interpreting-the-Resolving-of-Macro-variable/m-p/774619#M246210</guid>
      <dc:creator>svh</dc:creator>
      <dc:date>2021-10-15T22:05:46Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Interpreting the Resolving of Macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Interpreting-the-Resolving-of-Macro-variable/m-p/774621#M246211</link>
      <description>&lt;P&gt;Indirect referencing, with the multiple &amp;amp;&amp;amp;&amp;amp;&amp;amp;&amp;amp; type calls is basically the leftmost &amp;amp; (or multiples) hold off resolving until the ones to the right resolve.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The question is why did you use the&lt;/P&gt;
&lt;PRE&gt; into :M_&amp;amp;&amp;amp;analysisvar&lt;/PRE&gt;
&lt;P&gt;in the first place&lt;/P&gt;
&lt;P&gt;When I run your code the log shows:&lt;/P&gt;
&lt;PRE&gt;11   %let analysisvar = MPG_City;
12
13   proc sql ;
14      select mean(&amp;amp;analysisvar) into :M_&amp;amp;&amp;amp;analysisvar
15      from sashelp.cars;
16      quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


WARNING: Apparent symbolic reference M_ not resolved.&lt;BR /&gt;18 %put &amp;amp;M_&amp;amp;ANALYSISVAR; /*This does NOT resolve to the mean.*/&lt;BR /&gt;&amp;amp;M_MPG_City&lt;BR /&gt;
&lt;/PRE&gt;
&lt;P&gt;That particular warning basically means you have attempted to use a macro variable that doesn't exist, which is why it shows the &amp;amp;M_MPG_City in the log. You have implied that you have two macro variables, M_ and Analysisvar.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you use the&lt;/P&gt;
&lt;PRE&gt;put &amp;amp;&amp;amp;M_&amp;amp;ANALYSISVAR;&lt;/PRE&gt;
&lt;P&gt;The first &amp;amp; holds off resolving and generates the equivalent of &amp;amp;M_MPG_CITY, which is the macro variable created.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can see which macro variables you have defined currently in the scope you execute it with and their value by using&lt;/P&gt;
&lt;PRE&gt;%put _user_;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Oct 2021 22:31:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Interpreting-the-Resolving-of-Macro-variable/m-p/774621#M246211</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-10-15T22:31:20Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Interpreting the Resolving of Macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Interpreting-the-Resolving-of-Macro-variable/m-p/774632#M246215</link>
      <description>&lt;P&gt;Just pretend you are the macro processor.&amp;nbsp; Your whole reason for living is to hunt out % and &amp;amp; and replace text with other text.&amp;nbsp; So let's step through your program.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The macro processors ears perk up right on the first character, %, so it checks out that text and discovers a %LET statement.&amp;nbsp; It stores the text MPG_City into the macro variable analysisvar (creating analysisvar if it did not already exist) but emits NO text from that statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next it sees some text with an &amp;amp;. It emits the text up until the &amp;amp;.&lt;/P&gt;
&lt;PRE&gt;proc sql ;
   select mean(&lt;/PRE&gt;
&lt;P&gt;and checks out want follows the &amp;amp;.&amp;nbsp; It sees a string that looks like the name of macro variable it knows about so it replaces &amp;amp;analysisvar with MPG_City and emits that text to SAS.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;MPG_City&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then it seems more text followed by &amp;amp; so it emits that text.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;) into :M_&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And starts to check out what it sees.&amp;nbsp; I sees two &amp;amp; next to each other so it replaces them with a single &amp;amp; and reminds itself it needs check this string out again when it reaches the end.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then it sees more text followed by a space (or end of line) so it knows it has hit the end of the token which is now &amp;amp;analysisvar which it replaces with MPG_City and emits.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;MPG_City&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The it&amp;nbsp; sees more text until there is a % so it emits that text.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   from sashelp.cars;
   quit;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;At this point SAS has seen the whole SQL step and has run it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The macro processor then recognizes the %PUT statement and so starts checking out what you want it to write to the LOG.&lt;/P&gt;
&lt;P&gt;It then sees &amp;amp; and starts checking if this is a macro variable reference.&amp;nbsp; But instead if just finds the token &amp;amp;M_ so it issues a warning that it doesn't know anything about this M_ macro variable and writes the &amp;amp;M_ to the log.&lt;/P&gt;
&lt;P&gt;Then is sees &amp;amp; and this time recognizes our old friend and writes its value to the log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the second %PUT statement when it sees the double &amp;amp; it again replaces them with&amp;nbsp; a single &amp;amp; and reminds itself to re-review this token when it gets to the end.&amp;nbsp; It then sees the plain text M_ and remembers it.&amp;nbsp; Then it sees the reference to our old friend analysisvar again and replaces it.&amp;nbsp; Then it reaches the ; so it knows it has reached the end the token and rescans what it has which is now &amp;amp;M_MPG_City.&amp;nbsp; This looks like a reference to a macro variable, and it should be found since the SQL code that it just gave to SAS to run created it. So the %PUT statement writes the value of M_MPG_City to the log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Oct 2021 23:18:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Interpreting-the-Resolving-of-Macro-variable/m-p/774632#M246215</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-10-15T23:18:01Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Interpreting the Resolving of Macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Interpreting-the-Resolving-of-Macro-variable/m-p/774634#M246217</link>
      <description>I used&lt;BR /&gt;into :M_&amp;amp;&amp;amp;analysisvar because I'm writing a program in which&lt;BR /&gt;&amp;amp;analysisvar is a macro variable in a larger program, with steps before&lt;BR /&gt;this one. Thus, I'd like to use the same macro variable to make the summary&lt;BR /&gt;statistics I need for charts (rather than try to use another one for this&lt;BR /&gt;step). After some trial and error, I learned I needed :M_&amp;amp;&amp;amp;analysisvar to&lt;BR /&gt;be in the SELECT statement. It's a happy accident, but I sometimes don't&lt;BR /&gt;fully understand how these are resolved. Thanks for your help.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 15 Oct 2021 23:35:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Interpreting-the-Resolving-of-Macro-variable/m-p/774634#M246217</guid>
      <dc:creator>svh</dc:creator>
      <dc:date>2021-10-15T23:35:26Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Interpreting the Resolving of Macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Interpreting-the-Resolving-of-Macro-variable/m-p/774635#M246218</link>
      <description>&lt;P&gt;To reference a macro variable you only need one &amp;amp;.&amp;nbsp; The happy accident is that using two also worked.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using&amp;nbsp; the SYMBOLGEN option can help you see what is happening.&lt;/P&gt;
&lt;PRE&gt;337   %let analysisvar=MPG_City;
338
339   options symbolgen;
340   %put M_&amp;amp;analysisvar ;
SYMBOLGEN:  Macro variable ANALYSISVAR resolves to MPG_City
M_MPG_City
341   %put M_&amp;amp;&amp;amp;analysisvar ;
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
SYMBOLGEN:  Macro variable ANALYSISVAR resolves to MPG_City
M_MPG_City
&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Oct 2021 23:39:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Interpreting-the-Resolving-of-Macro-variable/m-p/774635#M246218</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-10-15T23:39:52Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Interpreting the Resolving of Macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Interpreting-the-Resolving-of-Macro-variable/m-p/774640#M246220</link>
      <description>Hi:&lt;BR /&gt;  Take a look at pages 10-11 and Figure 18 in this paper &lt;A href="https://support.sas.com/resources/papers/proceedings13/120-2013.pdf" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings13/120-2013.pdf&lt;/A&gt; . The Figure is an example of Macro Variable "indirect reference". This may help you understand more about how macro variables can be resolved.&lt;BR /&gt;Cynthia</description>
      <pubDate>Fri, 15 Oct 2021 23:55:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Interpreting-the-Resolving-of-Macro-variable/m-p/774640#M246220</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2021-10-15T23:55:39Z</dc:date>
    </item>
  </channel>
</rss>

