<?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: Passing a query as a string to a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Passing-a-query-as-a-string-to-a-macro/m-p/664673#M198606</link>
    <description>&lt;P&gt;As a minimum you should show the actual code of the macro.&lt;/P&gt;
&lt;P&gt;The message you show is typical when a value is&amp;nbsp; used as a parameter that includes commas. The macro parser uses the comma to delimit the macro parameters. You need to mask or quote the commas in some manner,&lt;/P&gt;
&lt;PRE&gt;%quote(Query_Txt)&lt;/PRE&gt;
&lt;P&gt;Is using&lt;/P&gt;
&lt;PRE&gt;%quote(select a.cust_id, a.order_dt, prod_type, prod_subtype
from  VPS_PRM.order_data a
where a.order_dt = '2020-05-09')
&lt;/PRE&gt;
&lt;P&gt;as a call but the %quote function only allows a single parameter.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I &lt;STRONG&gt;strongly suspect&lt;/STRONG&gt; that you do not want the %quote function but the data step Quote function.&lt;/P&gt;</description>
    <pubDate>Wed, 24 Jun 2020 15:38:15 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-06-24T15:38:15Z</dc:date>
    <item>
      <title>Passing a query as a string to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-a-query-as-a-string-to-a-macro/m-p/664662#M198602</link>
      <description>&lt;P&gt;Hello - I am trying to pass a query (as a string) to a macro.&amp;nbsp; When I PUT the query variable this is what it looks like in the log.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT&gt;QUERY_TXT=select a.cust_id, a.order_dt,&lt;U&gt; prod_type,&lt;/U&gt; prod_subtype&lt;BR /&gt;from&amp;nbsp; VPS_PRM.order_data a&lt;BR /&gt;where a.order_dt = '2020-05-09'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT&gt;This is the code that I am using...&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;DIV&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;string = cats('%nrstr(%RunReport(ISD=',strip(Start_Dt),',IED=',strip(End_Dt),',Days=',strip(Lookback_Days),',
    AT=',strip(Attachment_Type),',
    Query=',%quote(Query_Txt),'));');&lt;/CODE&gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;&lt;FONT&gt;This is the error that I am getting...&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#ff6600"&gt;ERROR: All positional parameters must precede keyword parameters.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#000000"&gt;Subsequent errors point to the query_txt variable&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#ff6600"&gt;&lt;FONT&gt;ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#000000"&gt;&lt;FONT&gt;Any help to resolve my issue is much appreciated.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="#000000"&gt;&lt;FONT&gt;Thank you.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;CALL EXECUTE(string);&lt;/FONT&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 24 Jun 2020 15:25:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-a-query-as-a-string-to-a-macro/m-p/664662#M198602</guid>
      <dc:creator>SyidaRox</dc:creator>
      <dc:date>2020-06-24T15:25:02Z</dc:date>
    </item>
    <item>
      <title>Re: Passing a query as a string to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-a-query-as-a-string-to-a-macro/m-p/664667#M198604</link>
      <description>&lt;P&gt;Just realized that all of that code that I intended to display did not make it to the code section.&amp;nbsp; Here it is again...&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;string = cats('%nrstr(%RunReport(ISD=',strip(Start_Dt),',IED=',strip(End_Dt),',Days=',strip(Lookback_Days),', AT=',strip(Attachment_Type),', Query=',%quote(Query_Txt),'));'); 

CALL EXECUTE(string); &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 24 Jun 2020 15:31:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-a-query-as-a-string-to-a-macro/m-p/664667#M198604</guid>
      <dc:creator>SyidaRox</dc:creator>
      <dc:date>2020-06-24T15:31:36Z</dc:date>
    </item>
    <item>
      <title>Re: Passing a query as a string to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-a-query-as-a-string-to-a-macro/m-p/664672#M198605</link>
      <description>&lt;P&gt;There is obviously a comma or such in your Query_Txt variable. You put the %quote the wrong place, methinks. Try something like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;string = cats('%RunReport(ISD=',strip(Start_Dt),',IED=',strip(End_Dt),',Days=',strip(Lookback_Days),',
    AT=',strip(Attachment_Type),',
    Query=%quote(',Query_Txt,'));');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I also dropped the %NRSTR around the whole expression, as the only way to execute that string is to %UNQUOTE it, and that will remove the %QUOTE around the Query_Txt.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jun 2020 15:37:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-a-query-as-a-string-to-a-macro/m-p/664672#M198605</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-06-24T15:37:35Z</dc:date>
    </item>
    <item>
      <title>Re: Passing a query as a string to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-a-query-as-a-string-to-a-macro/m-p/664673#M198606</link>
      <description>&lt;P&gt;As a minimum you should show the actual code of the macro.&lt;/P&gt;
&lt;P&gt;The message you show is typical when a value is&amp;nbsp; used as a parameter that includes commas. The macro parser uses the comma to delimit the macro parameters. You need to mask or quote the commas in some manner,&lt;/P&gt;
&lt;PRE&gt;%quote(Query_Txt)&lt;/PRE&gt;
&lt;P&gt;Is using&lt;/P&gt;
&lt;PRE&gt;%quote(select a.cust_id, a.order_dt, prod_type, prod_subtype
from  VPS_PRM.order_data a
where a.order_dt = '2020-05-09')
&lt;/PRE&gt;
&lt;P&gt;as a call but the %quote function only allows a single parameter.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I &lt;STRONG&gt;strongly suspect&lt;/STRONG&gt; that you do not want the %quote function but the data step Quote function.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jun 2020 15:38:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-a-query-as-a-string-to-a-macro/m-p/664673#M198606</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-06-24T15:38:15Z</dc:date>
    </item>
    <item>
      <title>Re: Passing a query as a string to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-a-query-as-a-string-to-a-macro/m-p/664675#M198608</link>
      <description>&lt;P&gt;&amp;nbsp;Why do you use the macro function %QUOTE? This will be resolved while the data step is compiled, not when the string is populated during data step execution. Use the data step function QUOTE instead:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro print(q=);
%put &amp;amp;q;
%mend;

data _null_;
string = cats('%nrstr(%print(q=',quote('a,b'),'))');
call execute(string);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 24 Jun 2020 15:45:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-a-query-as-a-string-to-a-macro/m-p/664675#M198608</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-06-24T15:45:11Z</dc:date>
    </item>
    <item>
      <title>Re: Passing a query as a string to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-a-query-as-a-string-to-a-macro/m-p/664677#M198610</link>
      <description>&lt;P&gt;Don't wrap the %NRSTR() around the whole macro call, just the %MACRONAME part.&amp;nbsp; Don't macro quote the NAME of the varaible with the SQL code, but around the value being passed to the macro call.&amp;nbsp; If you are using CATS() you don't need the STRIP() function calls.&amp;nbsp; Format your code so you have some hope of being able to understand it when you look at it again in two weeks.&lt;/P&gt;
&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;string = 
cats('%nrstr(%RunReport)(ISD=',Start_Dt
    ,',IED=',End_Dt
    ,',Days=',Lookback_Days
    ,',AT=',Attachment_Type
    ,',Query=%quote(',Query_Txt,')'
    ,');'
    )
; 

CALL EXECUTE(string); &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;But you might need to add %NRSTR() around the %QUOTE function call.&lt;/P&gt;
&lt;P&gt;Or modify your macro to accept actual quotes around the value of the QUERY parameter.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro runreport(....,query=,...);
...%sysfunc(dequote(&amp;amp;query))...
%mend runreport;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Then pass a quoted value to call execute:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   ,',Query=',quote(trim(Query_Txt))&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 24 Jun 2020 16:31:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-a-query-as-a-string-to-a-macro/m-p/664677#M198610</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-06-24T16:31:10Z</dc:date>
    </item>
    <item>
      <title>Re: Passing a query as a string to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-a-query-as-a-string-to-a-macro/m-p/664683#M198614</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/49957"&gt;@SyidaRox&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your query_txt needs to be enclosed in a %str() to escape the embedded&amp;nbsp; ',' between your columns&lt;/P&gt;
&lt;P&gt;Your RunReport macro, needs to use %unquote(&amp;amp;query) to cater for the character values&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* mock macro for illustration only */
%macro RunReport(isd=, ied=, days=, at=, query=);
	%put _user_;
	proc sql;
		%unquote(&amp;amp;query);
	quit;
%mend;

DATA _null_;
/* Sample data */
Start_Dt='06-01-2020';
End_Dt='06-02-2020';
Lookback_Days='1';
Attachment_Type='pdf';

string = cats('%RunReport(ISD='
,STRIP(Start_Dt)
,',IED='
,STRIP(End_Dt)
,',Days='
,STRIP(Lookback_Days)
,', AT='
,STRIP(Attachment_Type)
,', Query=%str('
,'select a.cust_id, a.order_dt, prod_type, prod_subtype from  VPS_PRM.order_data a where a.order_dt = %"2020-05-09%")'
,');'); 

CALL EXECUTE(string);
STOP; 
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note: I split the CATS () function call across multiple lines for better readability&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;Ahmed&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jun 2020 16:05:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-a-query-as-a-string-to-a-macro/m-p/664683#M198614</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2020-06-24T16:05:43Z</dc:date>
    </item>
    <item>
      <title>Re: Passing a query as a string to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-a-query-as-a-string-to-a-macro/m-p/664719#M198629</link>
      <description>Thank you everyone for the assistance. I learned some new things today and I appreciate you all!</description>
      <pubDate>Wed, 24 Jun 2020 17:16:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-a-query-as-a-string-to-a-macro/m-p/664719#M198629</guid>
      <dc:creator>SyidaRox</dc:creator>
      <dc:date>2020-06-24T17:16:43Z</dc:date>
    </item>
  </channel>
</rss>

