<?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 to reference a previously specified macro parameter in another parameter in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-reference-a-previously-specified-macro-parameter-in/m-p/824265#M325505</link>
    <description>&lt;P&gt;What about a simple fix like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table work.test_data as
%PROC_SQL_script_generator(input_data=BCVA, parameter1=BCVAALL, parameter2=%STR(BCVAALL as newvar length=200), parameter3=%STR(, BCVAYNSP as parameter3))
	union all
%PROC_SQL_script_generator(input_data=CHYP, parameter1=CHYPALL, parameter2=%STR(CHYPALL as newvar length=200), parameter3=%STR(, CHYPYNSP as parameter3))
	order by subjectid, eventname, parameter1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PARAMETER1 is only usable within the macro itself, not in another parameter being passed into the macro.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 20 Jul 2022 00:37:13 GMT</pubDate>
    <dc:creator>SASKiwi</dc:creator>
    <dc:date>2022-07-20T00:37:13Z</dc:date>
    <item>
      <title>How to reference a previously specified macro parameter in another parameter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-reference-a-previously-specified-macro-parameter-in/m-p/824260#M325503</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have a working SAS macro that writes part of PROC SQL code, which I use it to vertically stack multiple datasets that have the same data structure as a single dataset. An oversimplified of using the macro as the following:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table work.test_data as
%PROC_SQL_script_generator(input_data=BCVA, parameter1=BCVAALL, parameter2=BCVAYN, parameter3=%STR(, BCVAYNSP as parameter3))
	union all
%PROC_SQL_script_generator(input_data=CHYP, parameter1=CHYPALL, parameter2=CHYPYN, parameter3=%STR(, CHYPYNSP as parameter3))
	union all
%PROC_SQL_script_generator(input_data=CS, parameter1=FCNSALL, parameter2=CSYN, parameter3=%STR(, CSYNSP as parameter3))
	union all
%PROC_SQL_script_generator(input_data=FCS, parameter1=FCRSALL, parameter2=FCSYN, parameter3=%STR(, FCSYNSP as parameter3))	
	order by subjectid, eventname, parameter1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I am looking to make the macro more flexible so that it could pass SAS code to the macro definition. I thought I would be able to pass part of my PROC SQL code through the use of the %STR() function, however, I got stuck when I specify a parameter using a previously specified parameter as the following code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table work.test_data as
%PROC_SQL_script_generator(input_data=BCVA, parameter1=BCVAALL, parameter2=%STR(&amp;amp;parameter1. as newvar length=200), parameter3=%STR(, BCVAYNSP as parameter3))
	union all
%PROC_SQL_script_generator(input_data=CHYP, parameter1=CHYPALL, parameter2=%STR(&amp;amp;parameter1. as newvar length=200), parameter3=%STR(, CHYPYNSP as parameter3))
	order by subjectid, eventname, parameter1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This results in a WARNING saying Apparent symbolic reference parameter1 not resolved and the new variable generated by parameter2 has incorrect values. I have tried a number of other functions like %QUOTE, %NRSTR% but none of them work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Although I am unable to provide you with a sample data for testing, the following example maybe able to mimic the usage above&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test(v1=,v2=);
  %put &amp;amp;v2.;
%mend;
%test(v1=BCVAALL,v2=%STR(&amp;amp;v1. as anotherVar));
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As the following output shows, the macro calling above reproduces the WARNING (line 43) but the value of &amp;amp;v2 seems to be correctly printed (line 44)&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Chang_0-1658273719440.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/73512i922BEA334AE931A3/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Chang_0-1658273719440.png" alt="Chang_0-1658273719440.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;If it is not appropriate to reference a previously specified parameter in the same macro, my last resort would be to just type up the variable names like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table work.test_data as
%PROC_SQL_script_generator(input_data=BCVA, parameter1=BCVAALL, parameter2=%STR(BCVAALL as newvar length=200), parameter3=%STR(, BCVAYNSP as parameter3))
	union all
%PROC_SQL_script_generator(input_data=CHYP, parameter1=CHYPALL, parameter2=%STR(CHYPALL as newvar length=200), parameter3=%STR(, CHYPYNSP as parameter3))
	order by subjectid, eventname, parameter1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Any comments would be highly appreciated.&lt;BR /&gt;Chang&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jul 2022 23:41:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-reference-a-previously-specified-macro-parameter-in/m-p/824260#M325503</guid>
      <dc:creator>Chang</dc:creator>
      <dc:date>2022-07-19T23:41:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to reference a previously specified macro parameter in another parameter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-reference-a-previously-specified-macro-parameter-in/m-p/824265#M325505</link>
      <description>&lt;P&gt;What about a simple fix like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table work.test_data as
%PROC_SQL_script_generator(input_data=BCVA, parameter1=BCVAALL, parameter2=%STR(BCVAALL as newvar length=200), parameter3=%STR(, BCVAYNSP as parameter3))
	union all
%PROC_SQL_script_generator(input_data=CHYP, parameter1=CHYPALL, parameter2=%STR(CHYPALL as newvar length=200), parameter3=%STR(, CHYPYNSP as parameter3))
	order by subjectid, eventname, parameter1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PARAMETER1 is only usable within the macro itself, not in another parameter being passed into the macro.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jul 2022 00:37:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-reference-a-previously-specified-macro-parameter-in/m-p/824265#M325505</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2022-07-20T00:37:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to reference a previously specified macro parameter in another parameter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-reference-a-previously-specified-macro-parameter-in/m-p/824287#M325519</link>
      <description>&lt;P&gt;WHY?&lt;/P&gt;
&lt;P&gt;Which of these two statements would you rather type, read, maintain?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%mymacro(data=BCVA, parm1=BCVAALL, parm2=BCVAALL as newvar length=200, parm3=%STR(, BCVAYNSP as parameter3))
%mymacro(data=BCVA, parm1=BCVAALL, parm2=%STR(&amp;amp;parm1. as newvar length=200), parm3=%STR(, BCVAYNSP as parameter3))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The first one is SHORTER, CLEARER and it also WORKS.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jul 2022 06:09:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-reference-a-previously-specified-macro-parameter-in/m-p/824287#M325519</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-07-20T06:09:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to reference a previously specified macro parameter in another parameter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-reference-a-previously-specified-macro-parameter-in/m-p/824311#M325529</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30542"&gt;@Chang&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could avoid the macro processor's attempt to resolve the macro variable reference prematurely by supplying something else in the macro call and then let code &lt;EM&gt;in&lt;/EM&gt; the macro translate that "something" into what you actually need.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Simple example: Omit the ampersand in the macro call and insert it in the macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test(v1=,v2=);
  %let v2=&amp;amp;&amp;amp;&amp;amp;v2;
  %put &amp;amp;v2.;
%mend;
%test(v1=BCVAALL,v2=v1 as anotherVar);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit:&lt;/P&gt;
&lt;P&gt;Second example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test(v1=,v2=);
  %let v2=%sysfunc(translate(&amp;amp;v2,&amp;amp;,#));
  %put &amp;amp;v2.;
%mend;
%test(v1=BCVAALL,v2=#v1 as anotherVar);
%test(v1=BCVAALL,v2=XYZ as anotherVar);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Jul 2022 09:43:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-reference-a-previously-specified-macro-parameter-in/m-p/824311#M325529</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-07-20T09:43:00Z</dc:date>
    </item>
  </channel>
</rss>

