<?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: concatenating prompt multiple selection within where clause in proc sql in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/concatenating-prompt-multiple-selection-within-where-clause-in/m-p/656433#M78860</link>
    <description>&lt;P&gt;I assume you have macro variables like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let a1=James;
%let a2=Anna;
%let a3=Culbert;
%let a_count=3;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And you want to do something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select name from sashelp.class where name in(%catprompt(a));
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There seems to be some sort of macro quoting problem, meaning that the stuff in the IN clause does not resolve correctly. I was too lazy to find out what it was, so I just wrote a new version of the macro without quoting (the %STR gets unquoted by the %SYSFUNC):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro CatPrompt(prmpt);
  %local i delim;
  %do i=1 %to &amp;amp;&amp;amp;&amp;amp;prmpt._count;
     %do;&amp;amp;delim.%sysfunc(quote(&amp;amp;&amp;amp;&amp;amp;prmpt&amp;amp;i,%str(%')))%end;
     %let delim=,;
     %end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and that seems to work as intended.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The inner %DO...%END is just to avoid spurious whitespace in the output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit added:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you have input from users (they are unpredictable!) you may want to make sure that they do not break your precious macro by typing strange characters in the wrong places. So I did another version of the macro, getting the variable values using %SUPERQ (the macro quoting by %SUPERQ also gets neutralized by %SYSFUNC, and the QUOTE function makes sure that the quotes are balanced):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro CatPrompt(prmpt);
  %local i delim;
  %do i=1 %to &amp;amp;&amp;amp;&amp;amp;prmpt._count;
     %do;&amp;amp;delim.%sysfunc(quote(%superq(&amp;amp;prmpt.&amp;amp;i),%str(%')))%end;
     %let delim=,;
     %end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 10 Jun 2020 14:52:05 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2020-06-10T14:52:05Z</dc:date>
    <item>
      <title>concatenating prompt multiple selection within where clause in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/concatenating-prompt-multiple-selection-within-where-clause-in/m-p/652138#M78786</link>
      <description>Hi&lt;BR /&gt;I have a prompt which allows the user to select multiple items.&lt;BR /&gt;i'm trying to use these selections with in statement, but to use a macro for concatenating these selections, since i have many variables with prompts.&lt;BR /&gt;&lt;BR /&gt;the macro below works fine (concatenating with delimiter), but the proc sql makes an error.&lt;BR /&gt;i would appreciate any help&lt;BR /&gt;&lt;BR /&gt;here is the code:&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;%macro CatPrompt (prmpt);&lt;BR /&gt;%local i emit dlm1 dlm2;&lt;BR /&gt;%let emit=;&lt;BR /&gt;%let dlm1 = %str(%');&lt;BR /&gt;%let dlm2 = %str(,);&lt;BR /&gt;%let i = 1;&lt;BR /&gt;&lt;BR /&gt;%if &amp;amp;&amp;amp;&amp;amp;prmpt._count&amp;gt;0 %then %do;&lt;BR /&gt;%let emit = &amp;amp;dlm1.&amp;amp;&amp;amp;&amp;amp;prmpt&amp;amp;i.&amp;amp;dlm1.;&lt;BR /&gt;%do i=2 %to &amp;amp;&amp;amp;&amp;amp;prmpt._count;&lt;BR /&gt;%let emit=&amp;amp;emit.&amp;amp;dlm2.&amp;amp;dlm1.&amp;amp;&amp;amp;&amp;amp;prmpt&amp;amp;i.&amp;amp;dlm1.;&lt;BR /&gt;%end;&lt;BR /&gt;%end;&lt;BR /&gt;&lt;BR /&gt;&amp;amp;emit;&lt;BR /&gt;%mend CatPrompt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;PROC SQL;&lt;BR /&gt;CREATE TABLE WORK.Tbl2 AS&lt;BR /&gt;SELECT *&lt;BR /&gt;FROM WORK.Tbl1&lt;BR /&gt;WHERE (&amp;amp;Prompt_MultUsrSlct_count=0) OR (myFld1 IN (%CatPrompt(Prompt_MultUsrSlct)))&lt;BR /&gt;;&lt;BR /&gt;QUIT;</description>
      <pubDate>Mon, 01 Jun 2020 05:53:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/concatenating-prompt-multiple-selection-within-where-clause-in/m-p/652138#M78786</guid>
      <dc:creator>Lior2020</dc:creator>
      <dc:date>2020-06-01T05:53:07Z</dc:date>
    </item>
    <item>
      <title>Re: concatenating prompt multiple selection within where clause in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/concatenating-prompt-multiple-selection-within-where-clause-in/m-p/652142#M78787</link>
      <description>&lt;P&gt;Your macro does not create any code, so your condition ends up as&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;myFld1 IN ()&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Jun 2020 06:17:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/concatenating-prompt-multiple-selection-within-where-clause-in/m-p/652142#M78787</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-06-01T06:17:09Z</dc:date>
    </item>
    <item>
      <title>Re: concatenating prompt multiple selection within where clause in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/concatenating-prompt-multiple-selection-within-where-clause-in/m-p/652157#M78788</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;1) you have a semicolon in the end `&amp;amp;emit;` make it . or&lt;/P&gt;
&lt;P&gt;2) maybe this approach will help:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro CatPrompt (prmpt);
  %local i emit _dlm _count _prmpt;
  %let emit = "NOT-EXISTING-VALUE"; /* workaround for empty list */

  %if %bquote(&amp;amp;prmpt.) NE %then 
    %do;
      %LET emit = ;
      %LET _dlm = ;
      %LET _count = %QSYSFUNC(COUNTW(&amp;amp;prmpt.));
      %do i = 1 %to &amp;amp;_count;
        %LET _prmpt = %QSCAN(&amp;amp;prmpt., &amp;amp;i.);
        %let emit = &amp;amp;emit.&amp;amp;_dlm. %SYSFUNC(quote(&amp;amp;_prmpt.));
        %if &amp;amp;i. = 1 %then %LET _dlm =,;
      %end;
    %end;
&amp;amp;emit.
%mend CatPrompt;


%put *%CatPrompt(prmpt)*; /*single element list */

%put *%CatPrompt(A'A B'B C"C D"D E)*; /* multiple elements list with quotes in it */

%put *%CatPrompt()*; /* empty list */

%let list = a b c d;
%put *%CatPrompt(&amp;amp;list.)*; /* list in a macrovariable */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jun 2020 07:29:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/concatenating-prompt-multiple-selection-within-where-clause-in/m-p/652157#M78788</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-06-01T07:29:48Z</dc:date>
    </item>
    <item>
      <title>Re: concatenating prompt multiple selection within where clause in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/concatenating-prompt-multiple-selection-within-where-clause-in/m-p/652160#M78789</link>
      <description>Using the macro I posted:&lt;BR /&gt;%put %CatPrompt(Prompt_MultUsrSlct);&lt;BR /&gt;The results in the log are:&lt;BR /&gt;'a','b','c'&lt;BR /&gt;so it looks like the macro is ok. am i right?&lt;BR /&gt;&lt;BR /&gt;but the proc sql makes an error. here is the log (i simplified the where clause):&lt;BR /&gt;&lt;BR /&gt;36 PROC SQL;&lt;BR /&gt;37 CREATE TABLE WORK.Tbl2 AS&lt;BR /&gt;38 SELECT *&lt;BR /&gt;39 FROM WORK.Tbl1&lt;BR /&gt;40 WHERE myFld1 IN (%CatPrompt(Prompt_MultUsrSlct))&lt;BR /&gt;NOTE: Line generated by the macro variable "EMIT".&lt;BR /&gt;40 'a','b','c'&lt;BR /&gt;_&lt;BR /&gt;22&lt;BR /&gt;&lt;BR /&gt;_&lt;BR /&gt;&lt;BR /&gt;76&lt;BR /&gt;&lt;BR /&gt;ERROR 22-322: Syntax error, expecting one of the following: a quoted string, a numeric constant, a datetime constant, a missing value, (, -, SELECT.&lt;BR /&gt;&lt;BR /&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;BR /&gt;41 ;&lt;BR /&gt;&lt;BR /&gt;NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.&lt;BR /&gt;42 QUIT;</description>
      <pubDate>Mon, 01 Jun 2020 08:27:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/concatenating-prompt-multiple-selection-within-where-clause-in/m-p/652160#M78789</guid>
      <dc:creator>Lior2020</dc:creator>
      <dc:date>2020-06-01T08:27:01Z</dc:date>
    </item>
    <item>
      <title>Re: concatenating prompt multiple selection within where clause in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/concatenating-prompt-multiple-selection-within-where-clause-in/m-p/652161#M78790</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;try to make it:&lt;BR /&gt;&lt;BR /&gt;%UNQUOTE(&amp;amp;emit.)&lt;BR /&gt;&lt;BR /&gt;at the end of the macro&lt;BR /&gt;&lt;BR /&gt;Bart</description>
      <pubDate>Mon, 01 Jun 2020 08:30:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/concatenating-prompt-multiple-selection-within-where-clause-in/m-p/652161#M78790</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-06-01T08:30:09Z</dc:date>
    </item>
    <item>
      <title>Re: concatenating prompt multiple selection within where clause in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/concatenating-prompt-multiple-selection-within-where-clause-in/m-p/656433#M78860</link>
      <description>&lt;P&gt;I assume you have macro variables like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let a1=James;
%let a2=Anna;
%let a3=Culbert;
%let a_count=3;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And you want to do something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select name from sashelp.class where name in(%catprompt(a));
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There seems to be some sort of macro quoting problem, meaning that the stuff in the IN clause does not resolve correctly. I was too lazy to find out what it was, so I just wrote a new version of the macro without quoting (the %STR gets unquoted by the %SYSFUNC):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro CatPrompt(prmpt);
  %local i delim;
  %do i=1 %to &amp;amp;&amp;amp;&amp;amp;prmpt._count;
     %do;&amp;amp;delim.%sysfunc(quote(&amp;amp;&amp;amp;&amp;amp;prmpt&amp;amp;i,%str(%')))%end;
     %let delim=,;
     %end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and that seems to work as intended.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The inner %DO...%END is just to avoid spurious whitespace in the output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit added:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you have input from users (they are unpredictable!) you may want to make sure that they do not break your precious macro by typing strange characters in the wrong places. So I did another version of the macro, getting the variable values using %SUPERQ (the macro quoting by %SUPERQ also gets neutralized by %SYSFUNC, and the QUOTE function makes sure that the quotes are balanced):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro CatPrompt(prmpt);
  %local i delim;
  %do i=1 %to &amp;amp;&amp;amp;&amp;amp;prmpt._count;
     %do;&amp;amp;delim.%sysfunc(quote(%superq(&amp;amp;prmpt.&amp;amp;i),%str(%')))%end;
     %let delim=,;
     %end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Jun 2020 14:52:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/concatenating-prompt-multiple-selection-within-where-clause-in/m-p/656433#M78860</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-06-10T14:52:05Z</dc:date>
    </item>
  </channel>
</rss>

