<?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: SELECT statement inside a Macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SELECT-statement-inside-a-Macro/m-p/933320#M367067</link>
    <description>&lt;P&gt;Thanks PaigeMiller! It works!&lt;/P&gt;</description>
    <pubDate>Fri, 21 Jun 2024 14:09:44 GMT</pubDate>
    <dc:creator>mvalsamis</dc:creator>
    <dc:date>2024-06-21T14:09:44Z</dc:date>
    <item>
      <title>SELECT statement inside a Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SELECT-statement-inside-a-Macro/m-p/933276#M367060</link>
      <description>&lt;P&gt;Hi all in the Community! I want to create a macro with a SQL statement in it. The variables must be one of the macro's parameters. The macro is as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro sqlstep(vars=);&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp; select &amp;lt;insert variables here&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; from work.class;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Suppose I want to select 2 variables: name and gender. A way to select them is to write the macro as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro sqlstep(vars=);&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp; select &amp;amp;vars&lt;/P&gt;&lt;P&gt;&amp;nbsp; from work.class;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And I select them when invoking the macro:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%sqlstep(vars=name %str(,) gender).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But this is a time consuming way, and probably inefficient (imagine I have 50 variables instead of 2). Do you know any way to put the comma &lt;STRONG&gt;inside the macro,&amp;nbsp;&lt;/STRONG&gt;instead of the invocation? I want to invoke it like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%sqlstep(vars=name gender)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That's all. I hope that I didn't write a novel!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jun 2024 12:47:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SELECT-statement-inside-a-Macro/m-p/933276#M367060</guid>
      <dc:creator>mvalsamis</dc:creator>
      <dc:date>2024-06-21T12:47:31Z</dc:date>
    </item>
    <item>
      <title>Re: SELECT statement inside a Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SELECT-statement-inside-a-Macro/m-p/933277#M367061</link>
      <description>&lt;P&gt;Inside the macro&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&amp;nbsp;select %sysfunc(translate(&amp;amp;vars,%str(,),%str( )))&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Jun 2024 12:53:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SELECT-statement-inside-a-Macro/m-p/933277#M367061</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-06-21T12:53:19Z</dc:date>
    </item>
    <item>
      <title>Re: SELECT statement inside a Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SELECT-statement-inside-a-Macro/m-p/933279#M367062</link>
      <description>&lt;P&gt;For your simple example just use SAS code instead of SQL code and then you won't have to worry so much about commas.&amp;nbsp; Here are a couple of examples:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc print data=class;
  var &amp;amp;vars;
run;
proc sql;
select * from class(keep=&amp;amp;vars);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But in general you just need a way to convert the list from space delimited to comma delimited.&amp;nbsp; There are many examples&amp;nbsp;to do this that have been shared on this forum over the years.&amp;nbsp; One example is to use TRANSLATE() function.&amp;nbsp; To make it easier for the user also use the COMPBL() function to first reduce multiple spaces into one.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro sqlstep(vars=);
proc sql;
  select %sysfunc(translate(%qsysfunc(compbl(&amp;amp;vars)),%str(,),%str( )))
  from work.class;
quit;
%mend;

 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jun 2024 12:59:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SELECT-statement-inside-a-Macro/m-p/933279#M367062</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-06-21T12:59:01Z</dc:date>
    </item>
    <item>
      <title>Re: SELECT statement inside a Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SELECT-statement-inside-a-Macro/m-p/933320#M367067</link>
      <description>&lt;P&gt;Thanks PaigeMiller! It works!&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jun 2024 14:09:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SELECT-statement-inside-a-Macro/m-p/933320#M367067</guid>
      <dc:creator>mvalsamis</dc:creator>
      <dc:date>2024-06-21T14:09:44Z</dc:date>
    </item>
    <item>
      <title>Re: SELECT statement inside a Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SELECT-statement-inside-a-Macro/m-p/933322#M367068</link>
      <description>&lt;P&gt;Thanks Tom!&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jun 2024 14:10:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SELECT-statement-inside-a-Macro/m-p/933322#M367068</guid>
      <dc:creator>mvalsamis</dc:creator>
      <dc:date>2024-06-21T14:10:25Z</dc:date>
    </item>
  </channel>
</rss>

