<?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 SAS Macro Question in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Question/m-p/453037#M114400</link>
    <description>&lt;P&gt;Was hoping someone could help me.&amp;nbsp; Probably an easy answer to this, but I can't figure it out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's say I have the following macro:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro first(dsn, bylist, outdat);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;proc sql;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;create table &amp;amp;outdat&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;select &amp;amp;bylist&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;from &amp;amp;dsn;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;quit;&lt;/P&gt;
&lt;P&gt;%mend first;&lt;/P&gt;
&lt;P&gt;%first(dsn=mydata, bylist=x y z, outdat=outfile);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My problem is that I want to build a select statement based on the variables I feed the macro using the macro variable bylist.&amp;nbsp; The problem is, of course, that the list has to be separated by commas in the select statement in proc sql.&amp;nbsp; I need to feed the macro a list of items to use in that select statement because the variables will change with each call to the macro and the number of variables in that bylist will change as well.&amp;nbsp; In other words I need to tell the macro what variables I want to use in the select statement with each call to the macro.&amp;nbsp; Is there an easy way to do this?&amp;nbsp; I'm drawing a blank.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance so much for your help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 11 Apr 2018 03:00:47 GMT</pubDate>
    <dc:creator>DJENS</dc:creator>
    <dc:date>2018-04-11T03:00:47Z</dc:date>
    <item>
      <title>SAS Macro Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Question/m-p/453037#M114400</link>
      <description>&lt;P&gt;Was hoping someone could help me.&amp;nbsp; Probably an easy answer to this, but I can't figure it out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's say I have the following macro:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro first(dsn, bylist, outdat);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;proc sql;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;create table &amp;amp;outdat&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;select &amp;amp;bylist&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;from &amp;amp;dsn;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;quit;&lt;/P&gt;
&lt;P&gt;%mend first;&lt;/P&gt;
&lt;P&gt;%first(dsn=mydata, bylist=x y z, outdat=outfile);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My problem is that I want to build a select statement based on the variables I feed the macro using the macro variable bylist.&amp;nbsp; The problem is, of course, that the list has to be separated by commas in the select statement in proc sql.&amp;nbsp; I need to feed the macro a list of items to use in that select statement because the variables will change with each call to the macro and the number of variables in that bylist will change as well.&amp;nbsp; In other words I need to tell the macro what variables I want to use in the select statement with each call to the macro.&amp;nbsp; Is there an easy way to do this?&amp;nbsp; I'm drawing a blank.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance so much for your help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Apr 2018 03:00:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Question/m-p/453037#M114400</guid>
      <dc:creator>DJENS</dc:creator>
      <dc:date>2018-04-11T03:00:47Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Question/m-p/453048#M114410</link>
      <description>&lt;P&gt;The comma is used as a separator for macro parameters.&lt;/P&gt;
&lt;P&gt;You need to tell SAS that these commas are part of the parameter value and are not separators.&lt;/P&gt;
&lt;P&gt;This is done thus:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;%first(dsn=mydata, bylist=%str(x, y, z) , outdat=outfile);&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively, if you don't want to burden the users of the macro, add the commas inside the macro (replace intermediate spaces with commas).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;%first(dsn=mydata, bylist=x y z , outdat=outfile);&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;select %sysfunc(translate(%str(&amp;amp;bylist), %str(,), %str( ) ) )&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Apr 2018 03:37:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Question/m-p/453048#M114410</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-04-11T03:37:48Z</dc:date>
    </item>
  </channel>
</rss>

