<?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 Correct concatenation into an SQL statement in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Correct-concatenation-into-an-SQL-statement/m-p/43642#M11435</link>
    <description>I'm just starting to learn SQL/PROC SQL so hopefully this is an easy question to answer.  I want to take the values from one&lt;BR /&gt;
SQL statement and use them in another.  I am using an "INTO" statement because it's handy at creating a delimeter.  However&lt;BR /&gt;
when I need to use the "IN" statement in my where clause I need to concatenate the (' to the front and the ') to the end.  I thought&lt;BR /&gt;
it would be straightforward but I seem to get some sort of error no matter what I put in there.&lt;BR /&gt;
&lt;BR /&gt;
My Example below&lt;BR /&gt;
-----------------------------------------------------------------------------------------------&lt;BR /&gt;
proc sql;&lt;BR /&gt;
	select ID INTO :ID_VAR SEPARATED BY ''',''' from DB_RECORD WHERE FAMILY = 'ProductX';&lt;BR /&gt;
QUIT;&lt;BR /&gt;
%put &amp;amp;ID_VAR;&lt;BR /&gt;
-----------------------------------------------------------------------------------------------&lt;BR /&gt;
this yields &amp;amp;ID_VAR as&lt;BR /&gt;
&lt;BR /&gt;
	ID1','ID2','ID3','ID4&lt;BR /&gt;
	&lt;BR /&gt;
I then want to be able to query another database table with the contents of the &amp;amp;ID_VAR concatenated&lt;BR /&gt;
with (' on the front and ') on the back so that it works in my IN clause&lt;BR /&gt;
&lt;BR /&gt;
-----------------------------------------------------------------------------------------------&lt;BR /&gt;
proc sql;&lt;BR /&gt;
	select runtime, equipment from DB_PROCESS WHERE ID IN xxx;&lt;BR /&gt;
quit;&lt;BR /&gt;
-----------------------------------------------------------------------------------------------&lt;BR /&gt;
&lt;BR /&gt;
Obviously i'm looking to create xxx.  Maybe I'm going about this wrong but it seemed easy at the time.&lt;BR /&gt;
Please help me as I'm a noob.</description>
    <pubDate>Wed, 20 Jan 2010 21:18:03 GMT</pubDate>
    <dc:creator>GDA</dc:creator>
    <dc:date>2010-01-20T21:18:03Z</dc:date>
    <item>
      <title>Correct concatenation into an SQL statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Correct-concatenation-into-an-SQL-statement/m-p/43642#M11435</link>
      <description>I'm just starting to learn SQL/PROC SQL so hopefully this is an easy question to answer.  I want to take the values from one&lt;BR /&gt;
SQL statement and use them in another.  I am using an "INTO" statement because it's handy at creating a delimeter.  However&lt;BR /&gt;
when I need to use the "IN" statement in my where clause I need to concatenate the (' to the front and the ') to the end.  I thought&lt;BR /&gt;
it would be straightforward but I seem to get some sort of error no matter what I put in there.&lt;BR /&gt;
&lt;BR /&gt;
My Example below&lt;BR /&gt;
-----------------------------------------------------------------------------------------------&lt;BR /&gt;
proc sql;&lt;BR /&gt;
	select ID INTO :ID_VAR SEPARATED BY ''',''' from DB_RECORD WHERE FAMILY = 'ProductX';&lt;BR /&gt;
QUIT;&lt;BR /&gt;
%put &amp;amp;ID_VAR;&lt;BR /&gt;
-----------------------------------------------------------------------------------------------&lt;BR /&gt;
this yields &amp;amp;ID_VAR as&lt;BR /&gt;
&lt;BR /&gt;
	ID1','ID2','ID3','ID4&lt;BR /&gt;
	&lt;BR /&gt;
I then want to be able to query another database table with the contents of the &amp;amp;ID_VAR concatenated&lt;BR /&gt;
with (' on the front and ') on the back so that it works in my IN clause&lt;BR /&gt;
&lt;BR /&gt;
-----------------------------------------------------------------------------------------------&lt;BR /&gt;
proc sql;&lt;BR /&gt;
	select runtime, equipment from DB_PROCESS WHERE ID IN xxx;&lt;BR /&gt;
quit;&lt;BR /&gt;
-----------------------------------------------------------------------------------------------&lt;BR /&gt;
&lt;BR /&gt;
Obviously i'm looking to create xxx.  Maybe I'm going about this wrong but it seemed easy at the time.&lt;BR /&gt;
Please help me as I'm a noob.</description>
      <pubDate>Wed, 20 Jan 2010 21:18:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Correct-concatenation-into-an-SQL-statement/m-p/43642#M11435</guid>
      <dc:creator>GDA</dc:creator>
      <dc:date>2010-01-20T21:18:03Z</dc:date>
    </item>
    <item>
      <title>Re: Correct concatenation into an SQL statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Correct-concatenation-into-an-SQL-statement/m-p/43643#M11436</link>
      <description>Hi:&lt;BR /&gt;
  Check out the QUOTE function. It will put double quotes around your variable's value. I had this example sample that uses SASHELP.CLASS. &lt;BR /&gt;
                      &lt;BR /&gt;
  If you used the QUOTE  function, then you wouldn't have to worry about the parentheses ... they really belong to the WHERE and the IN operator anyway. You don't have to concatenate them to the list of names (or in your case, IDs) because macro variable substitution will plop the list of quoted names between the parentheses in the WHERE clause.&lt;BR /&gt;
               &lt;BR /&gt;
cynthia&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
   select quote(trim(name)) INTO :want_name SEPARATED BY "," &lt;BR /&gt;
       from sashelp.class &lt;BR /&gt;
       WHERE age ge 15;&lt;BR /&gt;
QUIT;&lt;BR /&gt;
%put &amp;amp;want_name; &lt;BR /&gt;
                                         &lt;BR /&gt;
ods listing;&lt;BR /&gt;
title 'Use with PROC SQL';&lt;BR /&gt;
proc sql;&lt;BR /&gt;
  select * &lt;BR /&gt;
  from sashelp.class&lt;BR /&gt;
  where name in (&amp;amp;want_name);&lt;BR /&gt;
quit;&lt;BR /&gt;
                                                  &lt;BR /&gt;
title 'Use with PROC PRINT';&lt;BR /&gt;
proc print data=sashelp.class;&lt;BR /&gt;
  where name in (&amp;amp;want_name);&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
[/pre]</description>
      <pubDate>Thu, 21 Jan 2010 01:18:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Correct-concatenation-into-an-SQL-statement/m-p/43643#M11436</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2010-01-21T01:18:57Z</dc:date>
    </item>
    <item>
      <title>Re: Correct concatenation into an SQL statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Correct-concatenation-into-an-SQL-statement/m-p/43644#M11437</link>
      <description>Or apply directly single quotes when resolving the macro:&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sql;&lt;BR /&gt;
select runtime, equipment from DB_PROCESS&lt;BR /&gt;
WHERE ID IN (%str(%')&amp;amp;WANT_NAME%str(%'));&lt;BR /&gt;
quit;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Portugal.&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;</description>
      <pubDate>Thu, 21 Jan 2010 09:12:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Correct-concatenation-into-an-SQL-statement/m-p/43644#M11437</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2010-01-21T09:12:04Z</dc:date>
    </item>
    <item>
      <title>Re: Correct concatenation into an SQL statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Correct-concatenation-into-an-SQL-statement/m-p/43645#M11438</link>
      <description>Thanks!.  It's working.  Please stay tuned for more easy questions from me in the future.</description>
      <pubDate>Thu, 21 Jan 2010 14:53:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Correct-concatenation-into-an-SQL-statement/m-p/43645#M11438</guid>
      <dc:creator>GDA</dc:creator>
      <dc:date>2010-01-21T14:53:29Z</dc:date>
    </item>
  </channel>
</rss>

