<?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 to pass in a macro list into a macro function &amp;amp; use WHERE with PROC SQL? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-pass-in-a-macro-list-into-a-macro-function-amp-use-WHERE/m-p/798544#M313945</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/187270"&gt;@narnia649&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The main goal is to take a data table that is user input &amp;amp; filter a database table for just the values from the user input data table. This occurs multiple times so it would be nice to have a function that can be called and have the filters pass into the function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;test ~ User Input Table&lt;/P&gt;
&lt;P&gt;test_two ~ Database Table&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It works fine outside of the macro function, but not within.&lt;BR /&gt;I was able to fix one of the errors relating to using %BQUOTE, but now it gives me a syntax error:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Error:&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;ERROR 22-322: Syntax error, expecting one of the following: a quoted string, a numeric constant, a datetime constant, a missing value, (,
              -, SELECT.

ERROR 200-322: The symbol is not recognized and will be ignored.&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Please turn on the macro debugging option&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and then run your code again.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please show us the &lt;FONT color="#FF0000"&gt;ENTIRE&lt;/FONT&gt; log for this macro, not selected parts of the log. For everyone's benefit, please note that in the future, we do not want to see parts of the log separated from the code that generated it. We need the &lt;FONT color="#FF0000"&gt;ENTIRE&lt;/FONT&gt; log for the PROC or DATA step or macro that has the error.&lt;/P&gt;</description>
    <pubDate>Fri, 25 Feb 2022 14:10:51 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2022-02-25T14:10:51Z</dc:date>
    <item>
      <title>How to pass in a macro list into a macro function &amp; use WHERE with PROC SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-pass-in-a-macro-list-into-a-macro-function-amp-use-WHERE/m-p/798542#M313944</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;The main goal is to take a data table that is user input &amp;amp; filter a database table for just the values from the user input data table. This occurs multiple times so it would be nice to have a function that can be called and have the filters pass into the function.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;test ~ User Input Table&lt;/P&gt;&lt;P&gt;test_two ~ Database Table&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It works fine outside of the macro function, but not within.&lt;BR /&gt;I was able to fix one of the errors relating to using %BQUOTE, but now it gives me a syntax error:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Error:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;304  options mprint;
305
306  DATA test;
307  INPUT x1 $ x2;
308  DATALINES;

NOTE: The data set WORK.TEST has 6 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


315  ;
316  RUN;
317
318  PROC SQL;
319      SELECT DISTINCT quote(trim(x1))
320      INTO :x_list separated BY ","
321      FROM test
322      ;
WARNING: No output destinations active.
323  QUIT;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


324
325  DATA test_two;
326  INPUT x1 $ x2;
327  DATALINES;

NOTE: The data set WORK.TEST_TWO has 6 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


334  ;
335  RUN;
336
337  %PUT &amp;amp;x_list;
"A","B"
338
339
340  PROC SQL;
341      CREATE TABLE query_result_no_macro AS
342      SELECT *
343      FROM test_two
344      WHERE x1 IN (&amp;amp;x_list)
345      ;
NOTE: Table WORK.QUERY_RESULT_NO_MACRO created, with 4 rows and 2 columns.

346  QUIT;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.03 seconds
      cpu time            0.01 seconds


347
348  %MACRO query_test(x1_list=);
349      PROC SQL;
350          CREATE TABLE query_result_macro AS
351          SELECT *
352          FROM test_two
353          WHERE x1 IN (&amp;amp;x1_list)
354          ;
355      QUIT;
356  %MEND query_test;
357
358  %query_test(x1_list=%BQUOTE(&amp;amp;x_list));
MPRINT(QUERY_TEST):   PROC SQL;
MPRINT(QUERY_TEST):   CREATE TABLE query_result_macro AS SELECT * FROM test_two WHERE x1 IN ("A","B") ;
MPRINT(QUERY_TEST):   QUIT;
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

NOTE: Line generated by the macro variable "X1_LIST".
1    "A","B"
     -
     22
      -
      200

ERROR 22-322: Syntax error, expecting one of the following: a quoted string, a numeric constant, a datetime constant, a missing value, (,
              -, SELECT.

ERROR 200-322: The symbol is not recognized and will be ignored.&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Code:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;DATA test;
INPUT x1 $ x2;
DATALINES;
A 1
A 2
A 3
B 1
B 2
B 3
;
RUN;

PROC SQL;
	SELECT DISTINCT quote(trim(x1))
	INTO :x_list separated BY ","
	FROM test
	;
QUIT;

DATA test_two;
INPUT x1 $ x2;
DATALINES;
A 1
A 2
B 3
B 4
C 5
C 6
;
RUN;

%PUT &amp;amp;x_list;


PROC SQL;
	CREATE TABLE query_result_no_macro AS
	SELECT *
	FROM test_two
	WHERE x1 IN (&amp;amp;x_list)
	;
QUIT;

%MACRO query_test(x1_list=);
	PROC SQL;
		CREATE TABLE query_result_macro AS
		SELECT *
		FROM test_two
		WHERE x1 IN (&amp;amp;x1_list)
		;
	QUIT;
%MEND query_test;

%query_test(x1_list=%BQUOTE(&amp;amp;x_list));&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Feb 2022 14:12:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-pass-in-a-macro-list-into-a-macro-function-amp-use-WHERE/m-p/798542#M313944</guid>
      <dc:creator>narnia649</dc:creator>
      <dc:date>2022-02-25T14:12:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to pass in a macro list into a macro function &amp; use WHERE with PROC SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-pass-in-a-macro-list-into-a-macro-function-amp-use-WHERE/m-p/798544#M313945</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/187270"&gt;@narnia649&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The main goal is to take a data table that is user input &amp;amp; filter a database table for just the values from the user input data table. This occurs multiple times so it would be nice to have a function that can be called and have the filters pass into the function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;test ~ User Input Table&lt;/P&gt;
&lt;P&gt;test_two ~ Database Table&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It works fine outside of the macro function, but not within.&lt;BR /&gt;I was able to fix one of the errors relating to using %BQUOTE, but now it gives me a syntax error:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Error:&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;ERROR 22-322: Syntax error, expecting one of the following: a quoted string, a numeric constant, a datetime constant, a missing value, (,
              -, SELECT.

ERROR 200-322: The symbol is not recognized and will be ignored.&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Please turn on the macro debugging option&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and then run your code again.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please show us the &lt;FONT color="#FF0000"&gt;ENTIRE&lt;/FONT&gt; log for this macro, not selected parts of the log. For everyone's benefit, please note that in the future, we do not want to see parts of the log separated from the code that generated it. We need the &lt;FONT color="#FF0000"&gt;ENTIRE&lt;/FONT&gt; log for the PROC or DATA step or macro that has the error.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Feb 2022 14:10:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-pass-in-a-macro-list-into-a-macro-function-amp-use-WHERE/m-p/798544#M313945</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-02-25T14:10:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to pass in a macro list into a macro function &amp; use WHERE with PROC SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-pass-in-a-macro-list-into-a-macro-function-amp-use-WHERE/m-p/798545#M313946</link>
      <description>Done&lt;BR /&gt;</description>
      <pubDate>Fri, 25 Feb 2022 14:12:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-pass-in-a-macro-list-into-a-macro-function-amp-use-WHERE/m-p/798545#M313946</guid>
      <dc:creator>narnia649</dc:creator>
      <dc:date>2022-02-25T14:12:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to pass in a macro list into a macro function &amp; use WHERE with PROC SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-pass-in-a-macro-list-into-a-macro-function-amp-use-WHERE/m-p/798548#M313948</link>
      <description>&lt;P&gt;Also, no macro or macro variables needed here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
	CREATE TABLE query_result_no_macro AS
	SELECT *
	FROM test_two
	WHERE x1 IN (select distinct x1 from test)
	;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Feb 2022 14:25:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-pass-in-a-macro-list-into-a-macro-function-amp-use-WHERE/m-p/798548#M313948</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-02-25T14:25:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to pass in a macro list into a macro function &amp; use WHERE with PROC SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-pass-in-a-macro-list-into-a-macro-function-amp-use-WHERE/m-p/798550#M313950</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/187270"&gt;@narnia649&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The easiest fix is to replace the comma in the INTO clause with a blank:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	INTO :x_list separated BY " "&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The IN operator doesn't require commas between the values.Then you don't need %BQUOTE to mask the comma in the macro call and %UNQUOTE to unmask it in the macro (which is what you omitted).&lt;/P&gt;</description>
      <pubDate>Fri, 25 Feb 2022 14:27:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-pass-in-a-macro-list-into-a-macro-function-amp-use-WHERE/m-p/798550#M313950</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-02-25T14:27:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to pass in a macro list into a macro function &amp; use WHERE with PROC SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-pass-in-a-macro-list-into-a-macro-function-amp-use-WHERE/m-p/798552#M313952</link>
      <description>&lt;P&gt;This works&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%query_test(x1_list=%QUOTE(&amp;amp;x_list))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;but this does not work&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%query_test(x1_list=%BQUOTE(&amp;amp;x_list))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I leave it to others to explain why %QUOTE works but %BQUOTE does not work.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Feb 2022 14:29:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-pass-in-a-macro-list-into-a-macro-function-amp-use-WHERE/m-p/798552#M313952</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-02-25T14:29:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to pass in a macro list into a macro function &amp; use WHERE with PROC SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-pass-in-a-macro-list-into-a-macro-function-amp-use-WHERE/m-p/798553#M313953</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This looks like a known problem where SAS doesn't always automatically remove the special characters used to implement the macro quoting (%bquote).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could change your macro to explicitly unquote the value:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO query_test(x1_list=);
	PROC SQL;
		CREATE TABLE query_result_macro AS
		SELECT *
		FROM test_two
		WHERE x1 IN (%unquote(&amp;amp;x1_list))
		;
	QUIT;
%MEND query_test;

%query_test(x1_list=%BQUOTE(&amp;amp;x_list));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Often it's easier to avoid introducing macro quoting. In this case, you're using quoting to mask the comma.&amp;nbsp; But the IN operator doesn't require the comma, you can use a space delimited list. So the below would work without any macro quoting:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL noprint;
	SELECT DISTINCT quote(trim(x1))
	INTO :x_list separated BY " "  /*space delimited list rather than comma delimited*/
	FROM test
	;
QUIT;

%MACRO query_test(x1_list=);
	PROC SQL ;
		CREATE TABLE query_result_macro AS
		SELECT *
		FROM test_two
		WHERE x1 IN (&amp;amp;x1_list)
		;
	QUIT;
%MEND query_test;


%query_test(x1_list=&amp;amp;x_list)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Feb 2022 14:35:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-pass-in-a-macro-list-into-a-macro-function-amp-use-WHERE/m-p/798553#M313953</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-02-25T14:35:53Z</dc:date>
    </item>
  </channel>
</rss>

