<?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: SAS Macro errors in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-errors/m-p/902243#M356534</link>
    <description>&lt;P&gt;Just what is this supposed to actually do (in words)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro do_over(values, between, phrase);
%let sql_expr=%do_over(values=inv_at_l drevadj ppe roa, between=%str(+),phrase=a.? * b.?);
%put %nrbquote(&amp;amp;sql_expr);
%mend;&lt;/PRE&gt;
&lt;P&gt;You do not use the parameters values, between or phrase anywhere, if you were the code should show &amp;amp;values &amp;amp;between and &amp;amp;phrase in the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The macro defines itself using itself, meaning it calls itself recursively and has no rule for stopping the recursion so likely over flows the macro space somewhere.&lt;/P&gt;</description>
    <pubDate>Thu, 09 Nov 2023 03:12:21 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-11-09T03:12:21Z</dc:date>
    <item>
      <title>SAS Macro errors</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-errors/m-p/902226#M356527</link>
      <description>&lt;P&gt;&lt;BR /&gt;17974 %macro do_over(values, between, phrase);&lt;BR /&gt;17975 %mend;&lt;BR /&gt;17976&lt;BR /&gt;17977 proc sql;&lt;BR /&gt;17978 create table Kothari_1 as&lt;BR /&gt;17979 /* fitted value computed as sum of coefficients in b multiplied by values in a */&lt;BR /&gt;17980 select a.*, b.intercept + %do_over(values=inv_at_l drevadj ppe roa, between=%str(+),&lt;BR /&gt;17980! phrase=a.? * b.?) as fitted,&lt;BR /&gt;------&lt;BR /&gt;22&lt;BR /&gt;ERROR 22-322: Syntax error, expecting one of the following: !, !!, &amp;amp;, (, *, **, +, ',', -, '.',&lt;BR /&gt;/, &amp;lt;, &amp;lt;=, &amp;lt;&amp;gt;, =, &amp;gt;, &amp;gt;=, ?, AND, AS, CONTAINS, EQ, EQT, FROM, GE, GET, GT, GTT, LE,&lt;BR /&gt;LET, LIKE, LT, LTT, NE, NET, OR, ^=, |, ||, ~=.&lt;/P&gt;&lt;P&gt;17981 /* abnormal accruals are ta - fitted */&lt;BR /&gt;17982 a.tac - calculated fitted as DA_Kothari_w_minm10,&lt;BR /&gt;17983 /* absolute abnormal accruals */&lt;BR /&gt;17984 abs (calculated DA_Kothari_w_minm10) as ABSDA_Kothari_w_minm10&lt;BR /&gt;17985 from main_sample_w a left join kothari_w b&lt;BR /&gt;17986 on a.sic2 = b.sic2 and a.fyear = b.fyear&lt;BR /&gt;17987 /* at a minimum 10 obs (5 degrees of freedom) */&lt;BR /&gt;17988 and b._EDF_ &amp;gt; 5 ;&lt;BR /&gt;17989 quit;&lt;BR /&gt;NOTE: The SAS System stopped processing this step because of errors.&lt;BR /&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;BR /&gt;real time 0.02 seconds&lt;BR /&gt;cpu time 0.03 seconds&lt;/P&gt;</description>
      <pubDate>Thu, 09 Nov 2023 01:28:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-errors/m-p/902226#M356527</guid>
      <dc:creator>nazmul</dc:creator>
      <dc:date>2023-11-09T01:28:52Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro errors</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-errors/m-p/902228#M356528</link>
      <description>&lt;P&gt;You haven't shared the macro definition with us nor have you shared SAS log with option mprint set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This function style macro will pre-process. The SQL parser will look at what the macro returns.&lt;/P&gt;
&lt;P&gt;After the macro executed it needs to return a string that's a SQL expression (or single value) that's valid SQL in the context where you're calling the macro.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1699493950645.png" style="width: 894px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89560iE8B0ACE5D25B29A4/image-dimensions/894x38?v=v2" width="894" height="38" role="button" title="Patrick_0-1699493950645.png" alt="Patrick_0-1699493950645.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;From the looks of it the macro returns a string that's not valid SQL in the context where you use it. Without the actual macro code and a SAS log using option mprint that shows us to what the macro resolves it's not really possible to provide more guidance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What you could do to ease debugging and maintenance:&lt;/P&gt;
&lt;P&gt;1. First resolve the macro and store the result in a macro variable&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let sql_expr=%do_over(values=inv_at_l drevadj ppe roa, between=%str(+),phrase=a.? * b.?);
%put sql_expr: %nrbquote(&amp;amp;sql_expr);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;2. Use the macro variable instead of the macro call in the SQL&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;b.intercept + &amp;amp;sql_expr as fitted,&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Nov 2023 02:01:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-errors/m-p/902228#M356528</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-11-09T02:01:58Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro errors</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-errors/m-p/902231#M356529</link>
      <description>Hi Patrick:&lt;BR /&gt;&lt;BR /&gt;Thank you for your response.&lt;BR /&gt;&lt;BR /&gt;SAS codes:&lt;BR /&gt;&lt;BR /&gt;%macro do_over(values, between, phrase);&lt;BR /&gt;%mend;&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table Kothari_1 as&lt;BR /&gt;select a.*, b.intercept + %do_over(values=inv_at_l drevadj ppe roa,&lt;BR /&gt;between=%str(+), phrase=a.? * b.?) as fitted,&lt;BR /&gt;a.tac - calculated fitted as DA_Kothari_w_minm10,&lt;BR /&gt;abs (calculated DA_Kothari_w_minm10) as ABSDA_Kothari_w_minm10&lt;BR /&gt;from main_sample_w a left join kothari_w b&lt;BR /&gt;on a.sic2 = b.sic2 and a.fyear = b.fyear&lt;BR /&gt;and b._EDF_ &amp;gt; 5 ;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;Log:&lt;BR /&gt;18037 %macro do_over(values, between, phrase);&lt;BR /&gt;18038 %mend;&lt;BR /&gt;18039&lt;BR /&gt;18040 proc sql;&lt;BR /&gt;18041 create table Kothari_1 as&lt;BR /&gt;18042 select a.*, b.intercept + %do_over(values=inv_at_l drevadj ppe roa,&lt;BR /&gt;between=%str(+),&lt;BR /&gt;18042! phrase=a.? * b.?) as fitted,&lt;BR /&gt;------&lt;BR /&gt;22&lt;BR /&gt;ERROR 22-322: Syntax error, expecting one of the following: !, !!, &amp;amp;, (, *,&lt;BR /&gt;**, +, ',', -, '.',&lt;BR /&gt;/, &amp;lt;, &amp;lt;=, &amp;lt;&amp;gt;, =, &amp;gt;, &amp;gt;=, ?, AND, AS, CONTAINS, EQ, EQT, FROM,&lt;BR /&gt;GE, GET, GT, GTT, LE,&lt;BR /&gt;LET, LIKE, LT, LTT, NE, NET, OR, ^=, |, ||, ~=.&lt;BR /&gt;&lt;BR /&gt;18043 a.tac - calculated fitted as DA_Kothari_w_minm10,&lt;BR /&gt;18044 abs (calculated DA_Kothari_w_minm10) as ABSDA_Kothari_w_minm10&lt;BR /&gt;18045 from main_sample_w a left join kothari_w b&lt;BR /&gt;18046 on a.sic2 = b.sic2 and a.fyear = b.fyear&lt;BR /&gt;18047 and b._EDF_ &amp;gt; 5 ;&lt;BR /&gt;18048 quit;&lt;BR /&gt;NOTE: The SAS System stopped processing this step because of errors.&lt;BR /&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;BR /&gt;real time 0.00 seconds&lt;BR /&gt;cpu time 0.01 seconds&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 09 Nov 2023 01:54:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-errors/m-p/902231#M356529</guid>
      <dc:creator>nazmul</dc:creator>
      <dc:date>2023-11-09T01:54:54Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro errors</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-errors/m-p/902232#M356530</link>
      <description>&lt;P&gt;Hi Patrick, I modified the code based on your suggestion and got the following errors.&lt;/P&gt;&lt;P&gt;SAS codes:&lt;/P&gt;&lt;P&gt;%macro do_over(values, between, phrase);&lt;BR /&gt;%let sql_expr=%do_over(values=inv_at_l drevadj ppe roa, between=%str(+),phrase=a.? * b.?);&lt;BR /&gt;%put %nrbquote(&amp;amp;sql_expr);&lt;BR /&gt;%mend;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table Kothari_1 as&lt;BR /&gt;select a.*,&lt;BR /&gt;b.intercept + &amp;amp;sql_expr as fitted, a.tac - calculated fitted as DA_Kothari_w_minm10,&lt;BR /&gt;abs (calculated DA_Kothari_w_minm10) as ABSDA_Kothari_w_minm10&lt;BR /&gt;from main_sample_w a left join kothari_w b&lt;BR /&gt;on a.sic2 = b.sic2 and a.fyear = b.fyear&lt;BR /&gt;and b._EDF_ &amp;gt; 5 ;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Log file:&lt;/P&gt;&lt;P&gt;18063 %macro do_over(values, between, phrase);&lt;BR /&gt;18064 %let sql_expr=%do_over(values=inv_at_l drevadj ppe roa, between=%str(+),phrase=a.? * b.?);&lt;BR /&gt;18065 %put %nrbquote(&amp;amp;sql_expr);&lt;BR /&gt;18066 %mend;&lt;BR /&gt;18067&lt;BR /&gt;18068 proc sql;&lt;BR /&gt;18069 create table Kothari_1 as&lt;BR /&gt;18070 select a.*,&lt;BR /&gt;18071 b.intercept + &amp;amp;sql_expr as fitted, a.tac - calculated fitted as DA_Kothari_w_minm10,&lt;BR /&gt;-&lt;BR /&gt;22&lt;BR /&gt;WARNING: Apparent symbolic reference SQL_EXPR not resolved.&lt;BR /&gt;ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string,&lt;BR /&gt;a numeric constant, a datetime constant, a missing value, BTRIM, INPUT, PUT,&lt;BR /&gt;SUBSTRING, USER.&lt;/P&gt;&lt;P&gt;18072 abs (calculated DA_Kothari_w_minm10) as ABSDA_Kothari_w_minm10&lt;BR /&gt;18073 from main_sample_w a left join kothari_w b&lt;BR /&gt;18074 on a.sic2 = b.sic2 and a.fyear = b.fyear&lt;BR /&gt;18075 and b._EDF_ &amp;gt; 5 ;&lt;BR /&gt;18076 quit;&lt;BR /&gt;NOTE: The SAS System stopped processing this step because of errors.&lt;BR /&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;BR /&gt;real time 0.01 seconds&lt;BR /&gt;cpu time 0.01 second&lt;/P&gt;</description>
      <pubDate>Thu, 09 Nov 2023 02:04:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-errors/m-p/902232#M356530</guid>
      <dc:creator>nazmul</dc:creator>
      <dc:date>2023-11-09T02:04:01Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro errors</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-errors/m-p/902234#M356531</link>
      <description>&lt;P&gt;What we need from you is the actual macro DEFINITION (the actual code between %macro and %mend).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please run below code and share the SAS log with us. NOTE: You need to replace %macro do_over ... %mend with your actual code. You can run the rest of below "as is".&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* macro DEFINITION - here the macro gets &lt;STRONG&gt;Compiled&lt;/STRONG&gt; */
%macro do_over(values=, between=, phrase=);
  /* HERE SOME CODE!! */
%mend;

options mprint mlogic merror;
/* macro CALL: Here the macro gets &lt;STRONG&gt;Executed&lt;/STRONG&gt; and the result gets assigned to macro variable &amp;amp;sql_expr */
%let sql_expr=%do_over(values=inv_at_l drevadj ppe roa, between=%str(+),phrase=a.? * b.?);

/* Write result of macro call to SAS log */
%put %nrbquote(&amp;amp;sql_expr);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Nov 2023 02:21:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-errors/m-p/902234#M356531</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-11-09T02:21:59Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro errors</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-errors/m-p/902243#M356534</link>
      <description>&lt;P&gt;Just what is this supposed to actually do (in words)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro do_over(values, between, phrase);
%let sql_expr=%do_over(values=inv_at_l drevadj ppe roa, between=%str(+),phrase=a.? * b.?);
%put %nrbquote(&amp;amp;sql_expr);
%mend;&lt;/PRE&gt;
&lt;P&gt;You do not use the parameters values, between or phrase anywhere, if you were the code should show &amp;amp;values &amp;amp;between and &amp;amp;phrase in the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The macro defines itself using itself, meaning it calls itself recursively and has no rule for stopping the recursion so likely over flows the macro space somewhere.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Nov 2023 03:12:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-errors/m-p/902243#M356534</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-11-09T03:12:21Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro errors</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-errors/m-p/902290#M356560</link>
      <description>&lt;P&gt;This strikes me as a case of the &lt;A href="https://www.computer-darkroom.com/lr2_find_folder/find-folder.htm" target="_self"&gt;XY Problem&lt;/A&gt;, where a programmer is trying to do something and achieve some end goal, and picks a particularly poor approach. Then the programmer gets stuck and asks for help in implementing this particularly poor approach. It also seems to me that the programmer&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/143540"&gt;@nazmul&lt;/a&gt;&amp;nbsp;should take a step back, and describe the entire problem here, starting with a description of the data and continuing on to describe the end goal. (The end goal is not to get this macro code to work). At that point, I get the very strong feeling there are simpler methods available that may not even involve complicated macro coding.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One reason I get the feeling that simpler methods are available is the attempt to force DO loops and arithmetical formulas into PROC SQL. While you can do arithmetic in SQL, its usually not the best choice unless the arithmetic is extremely simple; there are usually better choices in SAS that involve less programming and also ought to execute faster.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/143540"&gt;@nazmul&lt;/a&gt;&amp;nbsp;please describe the entire problem (which is not: how to get the macro to work).&lt;/P&gt;</description>
      <pubDate>Thu, 09 Nov 2023 13:05:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-errors/m-p/902290#M356560</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-11-09T13:05:16Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro errors</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-errors/m-p/902337#M356582</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;The purpose of the macro language is to save you from typing code.&amp;nbsp; In this case, use of a %do_over helper macro is a complex programming method.&amp;nbsp; But you probably don't need it, you can just type the code yourself.&amp;nbsp; Do you know the SQL code you want to have?&amp;nbsp; It looks to me like, if it was working, this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select a.*, b.intercept
+ %do_over(values=inv_at_l drevadj ppe roa, between=%str(+), phrase=a.? * b.?) as fitted,&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;would be the same as typing this code (without the do_over macro):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select a.*, b.intercept 
+ a.inv_at_l * b.inv_at_l + a.drevadj * b.drevadj + a.ppe * b.ppe + a.roa*b.roa as fitted,&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So in this case, using the macro does not save you much typing.&amp;nbsp; I would just type the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Nov 2023 15:10:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-errors/m-p/902337#M356582</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-11-09T15:10:11Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro errors</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-errors/m-p/902341#M356585</link>
      <description>&lt;P&gt;Good points,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And if the real problem that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/143540"&gt;@nazmul&lt;/a&gt;&amp;nbsp;is trying to solve has (for example) 40 columns that need to be added, then this goes back to my point that SQL is not a good tool for arithmetic in general, unless it is extremely simple math. If this is the case, a DATA step would make a lot more sense than doing this in SQL.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Nov 2023 15:16:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-errors/m-p/902341#M356585</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-11-09T15:16:04Z</dc:date>
    </item>
  </channel>
</rss>

