<?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: Syntax Error for macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Syntax-Error-for-macro/m-p/404104#M278978</link>
    <description>&lt;P&gt;Do you know how SAS macro processor works?&amp;nbsp; If you want to use it then you should learn about it.&lt;/P&gt;
&lt;P&gt;Here is a brief description of this macro definition.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro expand(base,n);
%local i;
%do i=1 %to &amp;amp;n; &amp;amp;&amp;amp;&amp;amp;base.&amp;amp;i %end;
%mend expand;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The first line and the last line are what makes this block of code a macro definition.&amp;nbsp; The name of the macro is EXPAND. Including the name on the %MEND statement is not required, but SAS would give me a warning that something had gotten messed up if the two names didn't match.&amp;nbsp;The values BASE and N on the first line are defining the parameter names that the macro uses.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The second line defines the macro variable I as local to macro.&amp;nbsp; I will be used as a loop counter and making local means that running the macro won't change the value of any macro variable named I that already existed when I called the macro.&amp;nbsp; Note that the macro parameters will be local macro variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The third line is the logic of what the macro is doing. It is using a %DO %TO %END loop to iterate the value of I from 1 to the value of the parameter N.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When the SAS macro processor sees two &amp;amp;'s next to each other it replaces them with one and tells itself that it needs to scan that code again to see if there might be more macro variable references to resolve. Macro variable references like '&amp;amp;base' and '&amp;amp;i' will be replaced by their values. Note that the period after '&amp;amp;base' let's the macro processor&amp;nbsp; know that this is the end of the name of the macro variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So '&amp;amp;&amp;amp;&amp;amp;base.&amp;amp;i' will get translated to '&amp;amp;x1' but SAS knows it is not done yet so it will now evaluate this string for any macro variable reference.&amp;nbsp; So '&amp;amp;x1' will get replaced with the value of the first of many macro variables.&amp;nbsp; Note if you have not already defined a macro variable named X1 then the macro processor will complain and then just pass &amp;amp;x1 onto SAS and let SAS see if it makes any sense of those characters as actual SAS code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then the last line&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where geo_id in (%expand(x,100)) ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is an example of using this macro inside your block of SAS code.&amp;nbsp; When you call, or invoke, a macro your prefix the macro's name with an % and provide values for the parameters inside the () that follow.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this case we are telling the macro to use X as the value for &amp;amp;BASE and 100 as the value for &amp;amp;N.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So basically we are asking for it generate the values of macro variables X1 X2 ..... X100 to replace the text of the macro call.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 13 Oct 2017 19:56:27 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2017-10-13T19:56:27Z</dc:date>
    <item>
      <title>Syntax Error for macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Syntax-Error-for-macro/m-p/404057#M278971</link>
      <description>&lt;P&gt;Hello:&lt;/P&gt;
&lt;P&gt;I created a macro codes below.&amp;nbsp; It showed several syntax errors in Log.&amp;nbsp; Please note that, the reason I put "&amp;amp;X1-&amp;amp;X5" is I might have more X coming input in the future, it could be up to 100.&amp;nbsp;&amp;nbsp; That is why I use '-' to save my time to list all of the texts.&amp;nbsp; However, it seems SAS don't recognize this function.&amp;nbsp; Please advice how to fix it.&amp;nbsp; Thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;%Let&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; X1='1400000US06001401300';&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;%Let&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; X2='1400000US06037131400';&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;%Let&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; X3='1400000US06037408627';&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;%Let&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; X4='1400000US06037482201';&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;%Let&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; X5='1400000US06111005306';&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; want;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; have;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;where&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; geo_id in (&amp;amp;X1-&amp;amp;X5)&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;708 data want;&lt;/P&gt;
&lt;P&gt;709 set have;&lt;/P&gt;
&lt;P&gt;710 where geo_id in (&amp;amp;X1-&amp;amp;X5);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 22&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;200&lt;/P&gt;
&lt;P&gt;710 where geo_id in (&amp;amp;X1-&amp;amp;X5);&lt;/P&gt;
&lt;P&gt;-&lt;/P&gt;
&lt;P&gt;22&lt;/P&gt;
&lt;P&gt;ERROR 22-322: Syntax error, expecting one of the following: a quoted string, ), ','.&lt;/P&gt;
&lt;P&gt;ERROR 200-322: The symbol is not recognized and will be ignored.&lt;/P&gt;
&lt;P&gt;710 where geo_id in (&amp;amp;X1-&amp;amp;X5) or GEO_display_label in (&amp;amp;Y1-&amp;amp;Y5);&lt;/P&gt;
&lt;P&gt;-&lt;/P&gt;
&lt;P&gt;200&lt;/P&gt;
&lt;P&gt;ERROR 200-322: The symbol is not recognized and will be ignored.&lt;/P&gt;
&lt;P&gt;ERROR: Syntax error while parsing WHERE clause.&lt;/P&gt;
&lt;P&gt;711 run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Oct 2017 18:06:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Syntax-Error-for-macro/m-p/404057#M278971</guid>
      <dc:creator>ybz12003</dc:creator>
      <dc:date>2017-10-13T18:06:42Z</dc:date>
    </item>
    <item>
      <title>Re: Syntax Error for macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Syntax-Error-for-macro/m-p/404060#M278972</link>
      <description>&lt;P&gt;What code did you want the macro processor to generate?&lt;/P&gt;
&lt;P&gt;This is what you told it to generate.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where geo_id in ('1400000US06001401300'-'1400000US06111005306');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It would probably be easier to just use one macro variable.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%Let Xlist=
 '1400000US06001401300'
 '1400000US06037131400'
 '1400000US06037408627'
 '1400000US06037482201'
 '1400000US06111005306'
;
.....
where geo_id in (&amp;amp;xlist);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Oct 2017 18:14:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Syntax-Error-for-macro/m-p/404060#M278972</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-10-13T18:14:11Z</dc:date>
    </item>
    <item>
      <title>Re: Syntax Error for macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Syntax-Error-for-macro/m-p/404061#M278973</link>
      <description>&lt;P&gt;As you could see, the geo_id is not continuous.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Oct 2017 18:14:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Syntax-Error-for-macro/m-p/404061#M278973</guid>
      <dc:creator>ybz12003</dc:creator>
      <dc:date>2017-10-13T18:14:01Z</dc:date>
    </item>
    <item>
      <title>Re: Syntax Error for macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Syntax-Error-for-macro/m-p/404064#M278974</link>
      <description>&lt;P&gt;The correct one is&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;where&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; geo_id in (&amp;amp;X1, &amp;amp;X2, &amp;amp;X3, &amp;amp;X4, &amp;amp;X5)&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;However if I have 100, do I have to write 100 Xs?&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Oct 2017 18:16:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Syntax-Error-for-macro/m-p/404064#M278974</guid>
      <dc:creator>ybz12003</dc:creator>
      <dc:date>2017-10-13T18:16:17Z</dc:date>
    </item>
    <item>
      <title>Re: Syntax Error for macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Syntax-Error-for-macro/m-p/404066#M278975</link>
      <description>&lt;P&gt;First don't make hundreds of macro variables. Leave data in datasets.&lt;/P&gt;
&lt;P&gt;In PROC SQL code you could write something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where geo_id in (select X from xlist)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And if you do put them into macro variables why use so many?&amp;nbsp; Why not just put the values into a single macro variable?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where geo_id in (&amp;amp;xlist)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you really really need to generate &amp;amp;x1 to &amp;amp;x100 then you will need a macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro expand(base,n);
%local i;
%do i=1 %to &amp;amp;n; &amp;amp;&amp;amp;&amp;amp;base.&amp;amp;i %end;
%mend expand;

....
where geo_id in (%expand(x,100)) ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Oct 2017 18:30:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Syntax-Error-for-macro/m-p/404066#M278975</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-10-13T18:30:29Z</dc:date>
    </item>
    <item>
      <title>Re: Syntax Error for macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Syntax-Error-for-macro/m-p/404081#M278976</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/67134"&gt;@ybz12003&lt;/a&gt;&amp;nbsp;I think Tom alluded to this, but I'll expand on it. When you write a macro the code generated must be valid SAS code. The code being generated is not valid SAS code. So you need to modify it to be valid, as well as make sure it's logically valid.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The best approach to solving these issues and debugging them is to FIRST generate working SAS code and then turn it into a macro. First by using macro variables and then a full macro if desired.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The steps to writing a macro are:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Step 1: Working Base SAS code&lt;/P&gt;
&lt;P&gt;Step 2: Identify parts that need to be dynamic/looped.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Step 3: Add the dynamic parts via macro variables&lt;/P&gt;
&lt;P&gt;Step 4: Convert to a full macro, if required.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Step 5: Debug using the following, if required.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint symbolgen mlogic;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Oct 2017 19:23:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Syntax-Error-for-macro/m-p/404081#M278976</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-10-13T19:23:33Z</dc:date>
    </item>
    <item>
      <title>Re: Syntax Error for macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Syntax-Error-for-macro/m-p/404088#M278977</link>
      <description>&lt;P&gt;Hi Tom:&lt;/P&gt;
&lt;P&gt;I am confused that why sometime is &lt;FONT color="#ff0000"&gt;base&lt;/FONT&gt;, sometime is &lt;FONT color="#ff0000"&gt;x&lt;/FONT&gt;; why put three &lt;FONT color="#ff0000"&gt;&amp;amp;&lt;/FONT&gt; in front of &lt;FONT color="#ff0000"&gt;base&lt;/FONT&gt;.&amp;nbsp;&amp;nbsp; Please see&amp;nbsp;my highlights.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token macrobound"&gt;&lt;STRONG&gt;&lt;FONT color="#000080"&gt;%macro&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;&lt;STRONG&gt;&lt;FONT color="#000080"&gt;expand&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;(&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT color="#ff0000"&gt;base&lt;/FONT&gt;&lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;,&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;&lt;FONT color="#0000ff"&gt;n&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT color="#999999"&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;SPAN class="token macroname"&gt;&lt;STRONG&gt;&lt;EM&gt;&lt;FONT color="#000080"&gt;%local&lt;/FONT&gt;&lt;/EM&gt;&lt;/STRONG&gt;&lt;/SPAN&gt; i&lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="token macrostatement"&gt;&lt;FONT color="#0000ff"&gt;%do&lt;/FONT&gt;&lt;/SPAN&gt; i&lt;SPAN class="token operator"&gt;&lt;FONT color="#a67f59"&gt;=&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;&lt;STRONG&gt;&lt;FONT color="#008080"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN class="token macrostatement"&gt;&lt;FONT color="#0000ff"&gt;%to&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&lt;FONT color="#a67f59"&gt;&amp;amp;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;&lt;FONT color="#0000ff"&gt;n&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;;&lt;/FONT&gt;&lt;/SPAN&gt; &lt;FONT color="#ff0000"&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;base&lt;/FONT&gt;&lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&lt;FONT color="#a67f59"&gt;&amp;amp;&lt;/FONT&gt;&lt;/SPAN&gt;i &lt;SPAN class="token macrostatement"&gt;&lt;FONT color="#0000ff"&gt;%end&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="token macrobound"&gt;&lt;STRONG&gt;&lt;FONT color="#000080"&gt;%mend&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;&lt;STRONG&gt;&lt;FONT color="#000080"&gt;expand&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT color="#999999"&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;SPAN class="token statement"&gt;&lt;FONT color="#0000ff"&gt;where&lt;/FONT&gt;&lt;/SPAN&gt; geo_id &lt;SPAN class="token operator"&gt;&lt;FONT color="#a67f59"&gt;in&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;(&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN class="token macroname"&gt;&lt;STRONG&gt;&lt;EM&gt;&lt;FONT color="#000080"&gt;%expand&lt;/FONT&gt;&lt;/EM&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;(&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT color="#ff0000"&gt;x&lt;/FONT&gt;&lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;,&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;&lt;STRONG&gt;&lt;FONT color="#008080"&gt;100&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;FONT color="#999999"&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;/FONT&gt; &lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Oct 2017 19:31:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Syntax-Error-for-macro/m-p/404088#M278977</guid>
      <dc:creator>ybz12003</dc:creator>
      <dc:date>2017-10-13T19:31:41Z</dc:date>
    </item>
    <item>
      <title>Re: Syntax Error for macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Syntax-Error-for-macro/m-p/404104#M278978</link>
      <description>&lt;P&gt;Do you know how SAS macro processor works?&amp;nbsp; If you want to use it then you should learn about it.&lt;/P&gt;
&lt;P&gt;Here is a brief description of this macro definition.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro expand(base,n);
%local i;
%do i=1 %to &amp;amp;n; &amp;amp;&amp;amp;&amp;amp;base.&amp;amp;i %end;
%mend expand;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The first line and the last line are what makes this block of code a macro definition.&amp;nbsp; The name of the macro is EXPAND. Including the name on the %MEND statement is not required, but SAS would give me a warning that something had gotten messed up if the two names didn't match.&amp;nbsp;The values BASE and N on the first line are defining the parameter names that the macro uses.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The second line defines the macro variable I as local to macro.&amp;nbsp; I will be used as a loop counter and making local means that running the macro won't change the value of any macro variable named I that already existed when I called the macro.&amp;nbsp; Note that the macro parameters will be local macro variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The third line is the logic of what the macro is doing. It is using a %DO %TO %END loop to iterate the value of I from 1 to the value of the parameter N.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When the SAS macro processor sees two &amp;amp;'s next to each other it replaces them with one and tells itself that it needs to scan that code again to see if there might be more macro variable references to resolve. Macro variable references like '&amp;amp;base' and '&amp;amp;i' will be replaced by their values. Note that the period after '&amp;amp;base' let's the macro processor&amp;nbsp; know that this is the end of the name of the macro variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So '&amp;amp;&amp;amp;&amp;amp;base.&amp;amp;i' will get translated to '&amp;amp;x1' but SAS knows it is not done yet so it will now evaluate this string for any macro variable reference.&amp;nbsp; So '&amp;amp;x1' will get replaced with the value of the first of many macro variables.&amp;nbsp; Note if you have not already defined a macro variable named X1 then the macro processor will complain and then just pass &amp;amp;x1 onto SAS and let SAS see if it makes any sense of those characters as actual SAS code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then the last line&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where geo_id in (%expand(x,100)) ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is an example of using this macro inside your block of SAS code.&amp;nbsp; When you call, or invoke, a macro your prefix the macro's name with an % and provide values for the parameters inside the () that follow.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this case we are telling the macro to use X as the value for &amp;amp;BASE and 100 as the value for &amp;amp;N.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So basically we are asking for it generate the values of macro variables X1 X2 ..... X100 to replace the text of the macro call.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Oct 2017 19:56:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Syntax-Error-for-macro/m-p/404104#M278978</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-10-13T19:56:27Z</dc:date>
    </item>
    <item>
      <title>Re: Syntax Error for macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Syntax-Error-for-macro/m-p/404111#M278979</link>
      <description>&lt;P&gt;Thank you so much for your thorough explanation, Tom!&amp;nbsp; You are awesome!&lt;/P&gt;</description>
      <pubDate>Fri, 13 Oct 2017 20:16:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Syntax-Error-for-macro/m-p/404111#M278979</guid>
      <dc:creator>ybz12003</dc:creator>
      <dc:date>2017-10-13T20:16:21Z</dc:date>
    </item>
  </channel>
</rss>

