<?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: How do I use/refer a macro variable in a macro parameter? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-refer-a-macro-variable-in-a-macro-parameter/m-p/778628#M247871</link>
    <description>You need to tell SAS to ignore the comma's in those macro parameters.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Details and examples here:&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/sgf/2019/07/17/passing-comma-delimited-values-into-sas-macros-and-macro-functions/" target="_blank"&gt;https://blogs.sas.com/content/sgf/2019/07/17/passing-comma-delimited-values-into-sas-macros-and-macro-functions/&lt;/A&gt;</description>
    <pubDate>Thu, 04 Nov 2021 23:02:54 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2021-11-04T23:02:54Z</dc:date>
    <item>
      <title>How do I use/refer a macro variable in a macro parameter?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-refer-a-macro-variable-in-a-macro-parameter/m-p/778624#M247869</link>
      <description>&lt;P&gt;I want to create TableB, TableC, TableD, TableE from TableA using macros.&lt;/P&gt;
&lt;P&gt;How do I use/refer a macro variable in a macro parameter?&lt;BR /&gt;Can I use/refer &amp;amp; in a parameter statement? See &amp;amp;vars&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;%LET keep_vars_1_mm =&lt;BR /&gt;DateKey_MdStart&lt;BR /&gt;,DateKey_MdEnd&lt;BR /&gt;,Year&lt;BR /&gt;,YEARQQ&lt;BR /&gt;,YEARMO&lt;BR /&gt;,Group_level_1&lt;BR /&gt;,Group_level_2&lt;BR /&gt;;&lt;BR /&gt;%LET keep_vars_2_mm =&lt;BR /&gt;DateKey_MdStart&lt;BR /&gt;,DateKey_MdEnd&lt;BR /&gt;,Year&lt;BR /&gt;,YEARQQ&lt;BR /&gt;,YEARMO&lt;BR /&gt;,Group_level_1&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;%LET keep_vars_3_qtr =&lt;BR /&gt;DateKey_MdStart&lt;BR /&gt;,DateKey_MdEnd&lt;BR /&gt;,Year&lt;BR /&gt;,YEARQQ&lt;BR /&gt;,Group_level_1&lt;BR /&gt;,Group_level_2&lt;BR /&gt;;&lt;BR /&gt;%LET keep_vars_4_qtr =&lt;BR /&gt;DateKey_MdStart&lt;BR /&gt;,DateKey_MdEnd&lt;BR /&gt;,Year&lt;BR /&gt;,YEARQQ&lt;BR /&gt;,Group_level_1&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;;&lt;BR /&gt;%LET Date1=DateKey_MdStart;&lt;BR /&gt;%LET Date2= DateKey_MdEnd;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;%MACRO MyMacro(lib=, dataset=, vars=, from_lib=, from_dataset=); &lt;BR /&gt;PROC SQL;&lt;BR /&gt;CREATE TABLE &amp;amp;lib..&amp;amp;dataset AS&lt;BR /&gt;&amp;amp;vars&lt;BR /&gt;,(SUM(Numeric_var) AS X&lt;BR /&gt;FROM &amp;amp;from_lib..&amp;amp;from_dataset&lt;BR /&gt;WHERE &amp;amp;Date1 &amp;gt;= INTNX('qtr', today()-1, 0, 'B') &lt;BR /&gt;AND &amp;amp;Date2 &amp;lt;= INTNX ('qtr', today()-1, 0, 'E')&lt;BR /&gt;GROUP BY&lt;BR /&gt;&amp;amp;vars&lt;BR /&gt;ORDER BY&lt;BR /&gt;&amp;amp;vars&lt;BR /&gt;;&lt;BR /&gt;QUIT;&lt;BR /&gt;%MEND;&lt;BR /&gt;%MyMacro(lib=WORK, dataset=TableB, vars=&amp;amp;keep_vars_1_mm, from_lib=LibA, from_dataset=TableA);&lt;BR /&gt;%MyMacro(lib=WORK, dataset=TableC, vars=&amp;amp;keep_vars_2_mm, from_lib=LibA, from_dataset=TableA);&lt;BR /&gt;%MyMacro(lib=WORK, dataset=TableD, vars=&amp;amp;keep_vars_3_qtr, from_lib=LibA, from_dataset=TableA);&lt;BR /&gt;%MyMacro(lib=WORK, dataset=TableE, vars=&amp;amp;keep_vars_4_qtr, from_lib=LibA, from_dataset=TableA);&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 22:40:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-refer-a-macro-variable-in-a-macro-parameter/m-p/778624#M247869</guid>
      <dc:creator>Pili1100</dc:creator>
      <dc:date>2021-11-04T22:40:48Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use/refer a macro variable in a macro parameter?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-refer-a-macro-variable-in-a-macro-parameter/m-p/778628#M247871</link>
      <description>You need to tell SAS to ignore the comma's in those macro parameters.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Details and examples here:&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/sgf/2019/07/17/passing-comma-delimited-values-into-sas-macros-and-macro-functions/" target="_blank"&gt;https://blogs.sas.com/content/sgf/2019/07/17/passing-comma-delimited-values-into-sas-macros-and-macro-functions/&lt;/A&gt;</description>
      <pubDate>Thu, 04 Nov 2021 23:02:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-refer-a-macro-variable-in-a-macro-parameter/m-p/778628#M247871</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-11-04T23:02:54Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use/refer a macro variable in a macro parameter?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-refer-a-macro-variable-in-a-macro-parameter/m-p/778655#M247879</link>
      <description>&lt;P&gt;If you want to pass a list of values in as the value of a parameter to a macro then DO NOT USE COMMAS in the list.&lt;/P&gt;
&lt;P&gt;If you have commas then you need to use either macro quoting of the commas:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MyMacro(lib=WORK, dataset=TableB, vars=%quote(&amp;amp;keep_vars_1_mm), from_lib=LibA, from_dataset=TableA);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or actual quoting around the commas (which the macro might need to remove) or else the commas will look like parameter separators in the macro call.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just design the macro to take a space delimited list of variables:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET Date1=DateKey_MdStart;
%LET Date2= DateKey_MdEnd;
%LET keep_vars_1_mm = DateKey_MdStart DateKey_MdEnd Year YEARQQ YEARMO Group_level_1 Group_level_2 ;
%MyMacro(lib=WORK, dataset=TableB, vars=&amp;amp;keep_vars_1_mm, from_lib=LibA, from_dataset=TableA);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Let the macro figure out if the spaces need to be converted to commas or not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For your example you could just use PROC SUMMARY to do the summation in which case you really don't want to commas.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro MyMacro(lib=, dataset=, vars=, from_lib=, from_dataset=);
%if not %symexist(date1) %then %put ERROR: Required macro variable not defined.;
%else %do;

proc summary data=&amp;amp;from_lib..&amp;amp;from_dataset nway ;
  where &amp;amp;Date1 &amp;gt;= INTNX('qtr', today()-1, 0, 'B')
    and &amp;amp;Date2 &amp;lt;= INTNX ('qtr', today()-1, 0, 'E')
  ;
  class &amp;amp;vars ;
  var Numeric_var ;
  output out=&amp;amp;lib..&amp;amp;dataset sum=X ;
run;

%end;
%mend MyMacro;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you really truly have to use PROC SQL so that you have to insert all of those annoying commas then it is not hard to convert the spaces to commas so SQL will be happy.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro MyMacro(lib=, dataset=, vars=, from_lib=, from_dataset=);
%let vars=%sysfunc(translate(%sysfunc(compbl(&amp;amp;vars)),%str(,),%str( )));
PROC SQL;
CREATE TABLE &amp;amp;lib..&amp;amp;dataset AS &amp;amp;vars,&amp;nbsp;....&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, 05 Nov 2021 03:23:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-refer-a-macro-variable-in-a-macro-parameter/m-p/778655#M247879</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-11-05T03:23:04Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use/refer a macro variable in a macro parameter?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-refer-a-macro-variable-in-a-macro-parameter/m-p/778678#M247898</link>
      <description>&lt;P&gt;I tried&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;%let vars=%sysfunc(translate(%sysfunc(compbl(&amp;amp;vars)),%str(,),%str( )));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and it worked like a charm! Thanks!&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Nov 2021 08:55:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-refer-a-macro-variable-in-a-macro-parameter/m-p/778678#M247898</guid>
      <dc:creator>Pili1100</dc:creator>
      <dc:date>2021-11-05T08:55:06Z</dc:date>
    </item>
  </channel>
</rss>

