<?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: How does the Macro handle the macro call and macro statement? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713634#M220188</link>
    <description>Hi Tom,&lt;BR /&gt;Thanks for your reply.&lt;BR /&gt;instead of how to comment the code, I couldn't understand the behave of the code. Or said, why the results are those? How SAS processes those code? What is under the table?</description>
    <pubDate>Sat, 23 Jan 2021 21:56:53 GMT</pubDate>
    <dc:creator>su35</dc:creator>
    <dc:date>2021-01-23T21:56:53Z</dc:date>
    <item>
      <title>How does the Macro handle the macro call and macro statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713622#M220184</link>
      <description>&lt;P&gt;Hi there,&lt;BR /&gt;When I am reading the book "Carpenter's Complete Guide to SAS Macro", I made confuse in Program 14.3.6e which is about comments.&lt;BR /&gt;The Program is as follows:&lt;/P&gt;&lt;PRE&gt;%macro abc;
   *%put in abc;  
%mend abc;

%macro doit;
   *%abc  
   %put here;
   *%let x = %abc;
   %put value of x is &amp;amp;x;
%mend doit;

%doit ;  &lt;/PRE&gt;&lt;P&gt;Log show:&lt;/P&gt;&lt;PRE&gt;215  %doit ;
here
in abc
value of x is *&lt;/PRE&gt;&lt;P&gt;The author explains:&lt;BR /&gt;1. SAS looks for macro statements and generally ignores most of the things that otherwise have meaning in the Base SAS language (for example, asterisks are not seen as tokens and are therefore not interpreted as the keyword that starts a comment). So,&amp;nbsp; *%put in abc and *%let x = %abc statements are executed.&lt;BR /&gt;2. a macro call is not seen as a macro statement and can still be commented on in this manner.&lt;BR /&gt;My questions:&lt;BR /&gt;1. Why the macro call would be commented?&lt;BR /&gt;2. What is the difference when a macro handle a macro call and a macro statement?&lt;BR /&gt;However, when run %put %doit;, log show:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;216  %put %doit;
&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;in abc&lt;/STRONG&gt;&lt;/FONT&gt;
here
in abc
value of x is *
**    *&lt;/PRE&gt;&lt;P&gt;Why the&amp;nbsp; *%abc executed this time?&lt;/P&gt;</description>
      <pubDate>Sat, 23 Jan 2021 20:20:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713622#M220184</guid>
      <dc:creator>su35</dc:creator>
      <dc:date>2021-01-23T20:20:43Z</dc:date>
    </item>
    <item>
      <title>Re: How does the Macro handle the macro call and macro statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713627#M220186</link>
      <description>&lt;P&gt;The macro processor just looks for the trigger characters % and &amp;amp; to start processing the text of your code.&amp;nbsp; There are only a limited number of ways to force it to ignore those triggers and the statement comment in your code is NOT one of them.&amp;nbsp; It will treat a statement comment the same way it treats any other SAS statement inside your macro.&amp;nbsp; To the macro processor the * that starts a statement comment is no different than the * used in multiplication.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want code to be ignored by the macro processor either use a macro comment.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%* This is a macro comment and will ignore %abc and &amp;amp;xyz ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or a block comment&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* This is block comment and will ignore %abc and &amp;amp;xyz&lt;BR /&gt;  and semi-colons (;) 
  and unbalanced quotes.
*/&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 23 Jan 2021 21:23:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713627#M220186</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-01-23T21:23:17Z</dc:date>
    </item>
    <item>
      <title>Re: How does the Macro handle the macro call and macro statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713634#M220188</link>
      <description>Hi Tom,&lt;BR /&gt;Thanks for your reply.&lt;BR /&gt;instead of how to comment the code, I couldn't understand the behave of the code. Or said, why the results are those? How SAS processes those code? What is under the table?</description>
      <pubDate>Sat, 23 Jan 2021 21:56:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713634#M220188</guid>
      <dc:creator>su35</dc:creator>
      <dc:date>2021-01-23T21:56:53Z</dc:date>
    </item>
    <item>
      <title>Re: How does the Macro handle the macro call and macro statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713643#M220193</link>
      <description>&lt;P&gt;Let's look what you coded.&amp;nbsp; Here is the first (inner) macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro abc;
   *%put in abc;  
%mend abc;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So when you run that it will emit an * and the execute the %PUT statement.&amp;nbsp; So it only generates part of a SAS statement.&amp;nbsp; You could use that macro like this if you wanted.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;x_squared = x %abc x ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And it would generate this SAS statment:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;x_squared = x * x ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now let's look at the second (outer) macro defintion.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro doit;
   *%abc  
   %put here;
   *%let x = %abc;
   %put value of x is &amp;amp;x;
%mend doit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So again you are emiting an * and then calling %ABC.&amp;nbsp; So that will emit another *.&amp;nbsp; You then run the %PUT macro statement. You then emit a third * and run the %LET statement. Since you are again calling %ABC the value of X should be an asterisk. And finally you ran another %PUT statement that writes some constant text and the value of X.&lt;/P&gt;
&lt;P&gt;You can turn on the MPRINT MLOGIC and SYMBOLGEN options to see more informaton in the log about how it is being processed.&lt;/P&gt;
&lt;PRE&gt;40    options mprint mlogic symbolgen;
41    %doit ;
MLOGIC(DOIT):  Beginning execution.
MLOGIC(DOIT):  %PUT here
here
MLOGIC(DOIT):  %LET (variable name is X)
MPRINT(DOIT):   *%abc
MLOGIC(ABC):  Beginning execution.
MLOGIC(ABC):  %PUT in abc
in abc
MLOGIC(ABC):  Ending execution.
MLOGIC(DOIT):  %PUT value of x is &amp;amp;x
SYMBOLGEN:  Macro variable X resolves to *
value of x is *
MLOGIC(DOIT):  Ending execution.
MPRINT():   *
&lt;/PRE&gt;
&lt;P&gt;And you can wrap the call to %DOIT inside a third macro to see how the emitted * is returned by %DOIT.&lt;/P&gt;
&lt;PRE&gt;50    options mprint mlogic symbolgen;

51
52    %macro three ;
53    %doit ;
54    %mend three;
55
56    %three;
MLOGIC(THREE):  Beginning execution.
MLOGIC(DOIT):  Beginning execution.
MLOGIC(DOIT):  %PUT here
here
MLOGIC(DOIT):  %LET (variable name is X)
MPRINT(DOIT):   *%abc
MLOGIC(ABC):  Beginning execution.
MLOGIC(ABC):  %PUT in abc
in abc
MLOGIC(ABC):  Ending execution.
MLOGIC(DOIT):  %PUT value of x is &amp;amp;x
SYMBOLGEN:  Macro variable X resolves to *
value of x is *
MLOGIC(DOIT):  Ending execution.
MPRINT(THREE):   * ;
MLOGIC(THREE):  Ending execution.
&lt;/PRE&gt;
&lt;P&gt;For more fun call %DOIT inside of quotes.&lt;/P&gt;
&lt;PRE&gt;125   options nomprint nomlogic nosymbolgen;
126
127   %put When you "%doit" what happens?;
in abc
here
in abc
value of x is *
When you "**    *" what happens?&lt;/PRE&gt;</description>
      <pubDate>Sat, 23 Jan 2021 22:50:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713643#M220193</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-01-23T22:50:23Z</dc:date>
    </item>
    <item>
      <title>Re: How does the Macro handle the macro call and macro statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713653#M220198</link>
      <description>Hi Tom,&lt;BR /&gt;let me split my question one by one.&lt;BR /&gt;As you explain as "So again you are emiting an * and then calling %ABC. So that will emit another *. You then run the %PUT macro statement. You then emit a third * and run the %LET statement. Since you are again calling %ABC the value of X should be an asterisk. And finally you ran another %PUT statement that writes some constant text and the value of X."&lt;BR /&gt;"then calling %ABC". why there is not an "in abc" output in log before the "here"?&lt;BR /&gt;</description>
      <pubDate>Sun, 24 Jan 2021 01:12:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713653#M220198</guid>
      <dc:creator>su35</dc:creator>
      <dc:date>2021-01-24T01:12:14Z</dc:date>
    </item>
    <item>
      <title>Re: How does the Macro handle the macro call and macro statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713656#M220199</link>
      <description>&lt;P&gt;No idea. Perhaps because it is in the middle of the %LET macro statement?&amp;nbsp; Or because of the missing semi-colon?&amp;nbsp; You can cause weird things by leaving the final semi-colon off the end of the last statement in a macro. Essentially the statement ends up be run in the calling environment instead of in the macro.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The second %PUT does show when you call %DOIT inside quotes.&lt;/P&gt;</description>
      <pubDate>Sun, 24 Jan 2021 01:40:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713656#M220199</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-01-24T01:40:29Z</dc:date>
    </item>
    <item>
      <title>Re: How does the Macro handle the macro call and macro statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713657#M220200</link>
      <description>I had added the semi-colon after the *%abc. But no difference. My core question is why the *%abc will not execute when run %doit along, however, when run %put %doit, the *%abc will execute?</description>
      <pubDate>Sun, 24 Jan 2021 02:16:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713657#M220200</guid>
      <dc:creator>su35</dc:creator>
      <dc:date>2021-01-24T02:16:54Z</dc:date>
    </item>
    <item>
      <title>Re: How does the Macro handle the macro call and macro statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713662#M220203</link>
      <description>&lt;P&gt;Like I said in my first response, it is not the macro call but the attempt to embed macro code into the middle of a statement comment. You can replicate the disappearing %PUT without the inner macro call.&lt;/P&gt;
&lt;PRE&gt;139   data _null_;
140     *%put in abc; ;
141   run;

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


142
143   %macro xx ;
144   data _null_;
145     *%put in abc; ;
146   run;
147   %mend xx;
148   %xx;
in abc

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
&lt;/PRE&gt;
&lt;P&gt;Outside of the macro environment this line is a comment:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; *%put in abc; ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But inside the macro environment it is three separate things:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; *
%put in abc;
 ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 24 Jan 2021 06:30:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713662#M220203</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-01-24T06:30:02Z</dc:date>
    </item>
    <item>
      <title>Re: How does the Macro handle the macro call and macro statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713680#M220215</link>
      <description>&lt;P&gt;I might ask the questions in a nuclear way. Let me ask again in another way.&lt;/P&gt;&lt;P&gt;The macro definition&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro abc;
   *%put in abc;  
%mend abc;

%macro doit;
  &lt;FONT color="#FF0000"&gt; *%abc&lt;/FONT&gt; 
  &lt;FONT color="#008000"&gt; %put here;&lt;/FONT&gt;
   &lt;FONT color="#0000FF"&gt;*%let x = %abc;&lt;/FONT&gt; 
   %put value of x is &amp;amp;x;
%mend doit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Using color to match the code to the output.&lt;/P&gt;&lt;P&gt;Now run&lt;/P&gt;&lt;PRE&gt;%doit;&lt;/PRE&gt;&lt;P&gt;the log output&lt;/P&gt;&lt;PRE&gt;249  %doit ;
MLOGIC(DOIT):  Beginning execution.
&lt;FONT color="#008000"&gt;MLOGIC(DOIT):  %PUT here&lt;/FONT&gt;
here
MLOGIC(DOIT):  %LET (variable name is X)
MPRINT(DOIT):  &lt;FONT color="#000000"&gt; *%abc&lt;/FONT&gt;
&lt;FONT color="#0000FF"&gt;MLOGIC(ABC):  Beginning execution.&lt;/FONT&gt;
MLOGIC(ABC):  %PUT in abc
in abc
MLOGIC(ABC):  Ending execution.
MLOGIC(DOIT):  %PUT value of x is &amp;amp;x
SYMBOLGEN:  Macro variable X resolves to *  ;
value of x is *  ;
MLOGIC(DOIT):  Ending execution.
MPRINT(ABC):   *&lt;/PRE&gt;&lt;P&gt;From the output, we can see that the green and blue code had executed.&lt;/P&gt;&lt;P&gt;Why the red code "*%abc" was ignored?&lt;/P&gt;&lt;P&gt;Now let run&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%put %doit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The log output&lt;/P&gt;&lt;PRE&gt;250  %put %doit;
MLOGIC(DOIT):  Beginning execution.
MLOGIC(ABC):  Beginning execution.
&lt;FONT color="#FF0000"&gt;MLOGIC(ABC):  %PUT in abc&lt;/FONT&gt;
in abc
**
MLOGIC(ABC):  Ending execution.
&lt;FONT color="#008000"&gt;MLOGIC(DOIT):  %PUT here&lt;/FONT&gt;
here
MLOGIC(DOIT):  %LET (variable name is X)
MPRINT(DOIT):   *
MLOGIC(ABC):  Beginning execution.
&lt;FONT color="#0000FF"&gt;MLOGIC(ABC):  %PUT in abc&lt;/FONT&gt;
in abc
MLOGIC(ABC):  Ending execution.
MLOGIC(DOIT):  %PUT value of x is &amp;amp;x
SYMBOLGEN:  Macro variable X resolves to *  ;
value of x is *  ;
MLOGIC(DOIT):  Ending execution.&lt;/PRE&gt;&lt;P&gt;All of the red, green, and blue code executed.&lt;/P&gt;&lt;P&gt;Why the red code executed this time?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Jan 2021 14:54:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713680#M220215</guid>
      <dc:creator>su35</dc:creator>
      <dc:date>2021-01-24T14:54:48Z</dc:date>
    </item>
    <item>
      <title>Re: How does the Macro handle the macro call and macro statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713682#M220216</link>
      <description>&lt;P&gt;I had inserted the Macro definition as SAS code and the code has not been colored right. Re-color as follows&lt;/P&gt;&lt;PRE&gt;%macro doit;
   &lt;FONT color="#FF0000"&gt;*%abc&lt;/FONT&gt; 
   &lt;FONT color="#008000"&gt;%put here;&lt;/FONT&gt;
   &lt;FONT color="#0000FF"&gt;*%let x = %abc;&lt;/FONT&gt; 
   %put value of x is &amp;amp;x;
%mend doit;&lt;/PRE&gt;</description>
      <pubDate>Sun, 24 Jan 2021 15:00:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713682#M220216</guid>
      <dc:creator>su35</dc:creator>
      <dc:date>2021-01-24T15:00:19Z</dc:date>
    </item>
    <item>
      <title>Re: How does the Macro handle the macro call and macro statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713690#M220218</link>
      <description>&lt;P&gt;Like I said before I believe it might be related to the fact that you inner macro, %ABC, has incomplete statements. You have a macro statement embedded in the middle of a comment statement. So the single semi-colon ends the %PUT statement and there is no semi-colon to end the comment statement.&amp;nbsp; But you would need to ask SAS to know exactly where in their parsing of this code the fact that running it inside a macro versus outside a macro makes a difference in whether the %PUT output is generated or not.&lt;/P&gt;</description>
      <pubDate>Sun, 24 Jan 2021 15:42:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713690#M220218</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-01-24T15:42:01Z</dc:date>
    </item>
    <item>
      <title>Re: How does the Macro handle the macro call and macro statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713702#M220222</link>
      <description>Hi Tom,&lt;BR /&gt;Thanks for your patient, but I am still confused.&lt;BR /&gt;When the %doit run, the first code would be *%abc. The first character is *, the * would be a meaningless character inside a macro and would be left to pass back to base SAS. Then, the second character is % which is a macro trigger. So, no matter how the %abc has been defined, the %abc should be called. But, from log output, we saw this macro call hasn't happened. Why?&lt;BR /&gt;However, when %put %doit; run, this macro call exactly happened. Why?&lt;BR /&gt;I had tried to add a semi-colon at the end of "*%put in abc;", "*%abc", and both. There is no difference.</description>
      <pubDate>Sun, 24 Jan 2021 17:08:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-the-Macro-handle-the-macro-call-and-macro-statement/m-p/713702#M220222</guid>
      <dc:creator>su35</dc:creator>
      <dc:date>2021-01-24T17:08:54Z</dc:date>
    </item>
  </channel>
</rss>

