<?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: Proc SQL with a macro in where statement to select on ID's in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-with-a-macro-in-where-statement-to-select-on-ID-s/m-p/613526#M179186</link>
    <description>&lt;P&gt;Thanks! This is indeed a much cleaner solution to the problem.&lt;/P&gt;</description>
    <pubDate>Mon, 23 Dec 2019 13:16:23 GMT</pubDate>
    <dc:creator>NickVe</dc:creator>
    <dc:date>2019-12-23T13:16:23Z</dc:date>
    <item>
      <title>Proc SQL with a macro in where statement to select on ID's</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-with-a-macro-in-where-statement-to-select-on-ID-s/m-p/613504#M179178</link>
      <description>&lt;P&gt;I am currently trying to write a small SAS macro that does the following:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;The macro reads the input ID values from an input table: "input_table".&lt;/LI&gt;&lt;LI&gt;The ID values are used to query an oracle database for the variable "TARGET".&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;The macro is shown below.&lt;/P&gt;&lt;P&gt;Whenever I run the macro, the filtering on ID does not seem to work and the proc sql return an empty table. I can not get my head around what might be going wrong, all help is welcome!&lt;/P&gt;&lt;P&gt;My current solution is using an inner join, which does the job. However, the SQL solution is strongly preferred for efficiency reasons.&lt;/P&gt;&lt;HR /&gt;&lt;P&gt;QUESTION: Why is the Proc SQL not selecting records based on the list "id_list"?&lt;/P&gt;&lt;HR /&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro query_DB_from_table(input_table = , output_table = );
/* PART 1: Get IDs from the input table */
%local id_list;
proc sql noprint;
    select ID into: id_list separated by "' , '"
    from &amp;amp;input_table;
quit;
/* PART 2: Query the Oracle Database */
proc sql noprint;
    create table &amp;amp;output_table as
    select ID, TARGET
    from ORACLE_DB
    where ID in (%str(')%bquote(&amp;amp;id_list)%str('))
    order by ID;
quit;
%mend query_DB_from_table;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Dec 2019 10:29:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-with-a-macro-in-where-statement-to-select-on-ID-s/m-p/613504#M179178</guid>
      <dc:creator>NickVe</dc:creator>
      <dc:date>2019-12-23T10:29:48Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL with a macro in where statement to select on ID's</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-with-a-macro-in-where-statement-to-select-on-ID-s/m-p/613516#M179184</link>
      <description>&lt;P&gt;It seem that you are trying to do something that SAS offers OOTB.&lt;/P&gt;
&lt;P&gt;Take a look at DBKEY option.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Dec 2019 11:59:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-with-a-macro-in-where-statement-to-select-on-ID-s/m-p/613516#M179184</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2019-12-23T11:59:46Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL with a macro in where statement to select on ID's</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-with-a-macro-in-where-statement-to-select-on-ID-s/m-p/613522#M179185</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/283816"&gt;@NickVe&lt;/a&gt;&amp;nbsp; You could keep it much simpler using quote function . Example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  select quote(ID) into: id_list separated by ' , '
    from &amp;amp;input_table;




where ID in (&amp;amp;id_list)  /*filter*/&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Dec 2019 12:52:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-with-a-macro-in-where-statement-to-select-on-ID-s/m-p/613522#M179185</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-12-23T12:52:44Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL with a macro in where statement to select on ID's</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-with-a-macro-in-where-statement-to-select-on-ID-s/m-p/613526#M179186</link>
      <description>&lt;P&gt;Thanks! This is indeed a much cleaner solution to the problem.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Dec 2019 13:16:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-with-a-macro-in-where-statement-to-select-on-ID-s/m-p/613526#M179186</guid>
      <dc:creator>NickVe</dc:creator>
      <dc:date>2019-12-23T13:16:23Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL with a macro in where statement to select on ID's</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-with-a-macro-in-where-statement-to-select-on-ID-s/m-p/613527#M179187</link>
      <description>Thanks for the tip, I will have a look at this!</description>
      <pubDate>Mon, 23 Dec 2019 13:16:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-with-a-macro-in-where-statement-to-select-on-ID-s/m-p/613527#M179187</guid>
      <dc:creator>NickVe</dc:creator>
      <dc:date>2019-12-23T13:16:50Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL with a macro in where statement to select on ID's</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-with-a-macro-in-where-statement-to-select-on-ID-s/m-p/613531#M179190</link>
      <description>&lt;P&gt;Good suggestions given above ... I would like to add an explanation as to why&amp;nbsp;this macro didn't work as originally written.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First, you should always turn on &lt;FONT face="courier new,courier"&gt;options mprint;&lt;/FONT&gt; before you run any macro, this will show in the log the code that SAS generates, making the whole thing easier to debug.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have a syntax error, you should be using&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%str(%')&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;twice, as otherwise the single quote is not properly resolved when SAS executes; the % sign indicating to the %STR() function that there is a single unmatched quote.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next you need the %unquote() function:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    where name in (%unquote(%str(%')%bquote(&amp;amp;id_list)%str(%')))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;but the reasoning isn't particularly simple to explain. All I know is that when it seems like your code is properly generated in the log, and yet SAS reports errors, %unquote() often fixes the problem.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The moral of the story is that creating macro strings with quotes and commas can be difficult, and the simpler methods (such as that provided by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;of creating a quoted string works much better and with many fewer complications, and so I highly recommend that you don't try to add your own quotes around the value of a macro variable unless nothing else seems to work.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Dec 2019 13:33:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-with-a-macro-in-where-statement-to-select-on-ID-s/m-p/613531#M179190</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-12-23T13:33:07Z</dc:date>
    </item>
  </channel>
</rss>

