<?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 %do loop as a macro parameter value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/do-loop-as-a-macro-parameter-value/m-p/787305#M251545</link>
    <description>&lt;P&gt;I attempted to modularize several macros, one of which calls the REPORT procedure.&amp;nbsp; The default call using %DO loop in the COLUMN statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do __i = 1 %to &amp;amp;num_of_cols. ;
   col&amp;amp;__i.
%end ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;A more complex call might intend the following:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;( "Group 1"
  %do __i = 1 %to %eval( &amp;amp;num_of_cols. - 1 ) ;
        col__&amp;amp;i.
  %end ;
)
col__&amp;amp;i.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since the first macro generates NUM_OF_COLS (dynamically), I want to pass that value from the calling program, through the first macro, and have it resolve in the second macro, which is called by the first.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have tried a variety of %str(%%)do and %str(%%)str(%%) do to now avail.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would appreciate any tips, corrections, or references.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kevin&lt;/P&gt;</description>
    <pubDate>Thu, 23 Dec 2021 22:40:41 GMT</pubDate>
    <dc:creator>KevinViel</dc:creator>
    <dc:date>2021-12-23T22:40:41Z</dc:date>
    <item>
      <title>%do loop as a macro parameter value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-as-a-macro-parameter-value/m-p/787305#M251545</link>
      <description>&lt;P&gt;I attempted to modularize several macros, one of which calls the REPORT procedure.&amp;nbsp; The default call using %DO loop in the COLUMN statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do __i = 1 %to &amp;amp;num_of_cols. ;
   col&amp;amp;__i.
%end ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;A more complex call might intend the following:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;( "Group 1"
  %do __i = 1 %to %eval( &amp;amp;num_of_cols. - 1 ) ;
        col__&amp;amp;i.
  %end ;
)
col__&amp;amp;i.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since the first macro generates NUM_OF_COLS (dynamically), I want to pass that value from the calling program, through the first macro, and have it resolve in the second macro, which is called by the first.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have tried a variety of %str(%%)do and %str(%%)str(%%) do to now avail.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would appreciate any tips, corrections, or references.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kevin&lt;/P&gt;</description>
      <pubDate>Thu, 23 Dec 2021 22:40:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-as-a-macro-parameter-value/m-p/787305#M251545</guid>
      <dc:creator>KevinViel</dc:creator>
      <dc:date>2021-12-23T22:40:41Z</dc:date>
    </item>
    <item>
      <title>Re: %do loop as a macro parameter value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-as-a-macro-parameter-value/m-p/787308#M251547</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I cannot entirely follow your reasoning, but you may be helped with the&amp;nbsp;%GLOBAL Macro Statement.&lt;/P&gt;
&lt;P&gt;%GLOBAL Macro Statement :&lt;/P&gt;
&lt;P class="xisDoc-shortDescription"&gt;Creates macro variables that are available during the execution of an entire SAS session.&lt;/P&gt;
&lt;P class="xisDoc-shortDescription"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="xisDoc-shortDescription"&gt;Syntax&lt;BR /&gt;%GLOBAL macro-variable(s);&lt;/P&gt;
&lt;P class="xisDoc-shortDescription"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="xisDoc-shortDescription"&gt;Koen&lt;/P&gt;</description>
      <pubDate>Thu, 23 Dec 2021 22:55:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-as-a-macro-parameter-value/m-p/787308#M251547</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-12-23T22:55:57Z</dc:date>
    </item>
    <item>
      <title>Re: %do loop as a macro parameter value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-as-a-macro-parameter-value/m-p/787312#M251551</link>
      <description>&lt;P&gt;You'll have to supply more details about what is in the macros, and how the first macro calls the second.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When one macro calls another, its macro variables are automatically available.&amp;nbsp; So it's not possible to imagine where the problem lies.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Dec 2021 00:50:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-as-a-macro-parameter-value/m-p/787312#M251551</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-12-24T00:50:30Z</dc:date>
    </item>
    <item>
      <title>Re: %do loop as a macro parameter value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-as-a-macro-parameter-value/m-p/787317#M251552</link>
      <description>&lt;P&gt;A word of advice.&amp;nbsp; Don't.&amp;nbsp; It is not worth the headache.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You cannot inject something like a %DO loop into an already compiled and running macro. But you could call some other macro that runs a %DO loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Add single quotes around the macro code you want to pass in and the use DEQUOTE() to remove them when you want it to run.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro some_function(a,b,c);
%put &amp;amp;=a &amp;amp;=b &amp;amp;=c;
%mend;
%macro mymacro(parm1,parm2,subroutine);
%put &amp;amp;=parm1 &amp;amp;=parm2;
%sysfunc(dequote(&amp;amp;subroutine));
%mend mymacro;

%mymacro(parm1=xx,parm2=yy,subroutine='%some_function(1,2,3)');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;730   %mymacro(parm1=xx,parm2=yy,subroutine='%some_function(1,2,3)');
PARM1=xx PARM2=yy
A=1 B=2 C=3
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Dec 2021 04:17:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-as-a-macro-parameter-value/m-p/787317#M251552</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-12-24T04:17:56Z</dc:date>
    </item>
    <item>
      <title>Re: %do loop as a macro parameter value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-as-a-macro-parameter-value/m-p/788097#M251906</link>
      <description>&lt;P&gt;Tom,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; That was my conclusion before posting.&amp;nbsp; I was providing the users some "free code" (free text) ability, but I will have to limit it.&amp;nbsp; Thank you for the example.&amp;nbsp; I wonder if&amp;nbsp; %sysfunc(dequote()) and %unquote() perform the same?&amp;nbsp; I will look it up in my free time, i.e. after I get out from under the wall of work and emails from being off a week.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Happy New Year!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kevin&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jan 2022 14:37:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-as-a-macro-parameter-value/m-p/788097#M251906</guid>
      <dc:creator>KevinViel</dc:creator>
      <dc:date>2022-01-03T14:37:08Z</dc:date>
    </item>
    <item>
      <title>Re: %do loop as a macro parameter value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-as-a-macro-parameter-value/m-p/788105#M251911</link>
      <description>&lt;P&gt;%UNQUOTE() removes macro quoting.&amp;nbsp; DEQUOTE() removes actual quotes.&amp;nbsp; If you use %SYSFUNC() to call DEQUOTE() then the result has the macro quoting removed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It will be easier for users to pass in values that contain macro triggers by using physical single quotes then figuring out the complex macro quoting rules/functions.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jan 2022 15:32:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-as-a-macro-parameter-value/m-p/788105#M251911</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-01-03T15:32:59Z</dc:date>
    </item>
    <item>
      <title>Re: %do loop as a macro parameter value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-as-a-macro-parameter-value/m-p/788142#M251926</link>
      <description>&lt;P&gt;Hi Kevin,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm not understanding your question, can you explain more?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It looks like you're trying to pass in the upper limit for the %do loop, right?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you do something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro inner (limit=);
  %local i ;

  %put Before unquoting: &amp;amp;=limit ;
  %let limit=%unquote(&amp;amp;limit) ;
  %put After unquoting: &amp;amp;=limit ;

  %do i=1 %to &amp;amp;limit ;
    %put &amp;amp;=i ;
  %end ;

%mend inner ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That will allow users to do a simple call like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%inner(limit=3)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Users could also do more complex calls like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let num_of_cols=3 ;

%inner(limit=&amp;amp;num_of_cols)
%inner(limit=%nrstr(&amp;amp;num_of_cols))
%inner(limit=%nrstr(%eval(&amp;amp;num_of_cols -1)))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So in those last two calls, the user has used %NRSTR() to delay resolution of the macro reference to NUM_OF_COLS, until it has been %UNQUOTED inside the macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Depending on your use case, you might want to set the default value of LIMIT to be&amp;nbsp;&lt;CODE class=" language-sas"&gt;%nrstr(&amp;amp;num_of_cols).&lt;/CODE&gt;&amp;nbsp;That would make the macro more tightly coupled to the calling environment, and less of a utility macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the users are at an advanced&amp;nbsp; macro language level where they would *want* to pass a complex value like&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;%eval( &amp;amp;num_of_cols. - 1 )&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I think it's reasonable to teach them to add %nrstr() when necessary to delay resolution of macro references.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jan 2022 17:10:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-as-a-macro-parameter-value/m-p/788142#M251926</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-01-03T17:10:29Z</dc:date>
    </item>
    <item>
      <title>Re: %do loop as a macro parameter value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loop-as-a-macro-parameter-value/m-p/788303#M252015</link>
      <description>&lt;P&gt;Quentin,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; That would work on a more rigid macro:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;               column %if %nrbquote(&amp;amp;report_column.) = %str()
                     %then
                        %do ;
                            section_order_1
                            row_order_1
                            row_1
                            %do __i = 1 %to &amp;amp;num_of_cols. ;
                               col&amp;amp;__i.
                            %end ;
                        %end ;
                      %else
                         %do ;
                             %unquote(&amp;amp;report_column.)
                         %end ;
                     ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This current version does not allow column headers, for instance.&amp;nbsp; Consider that COL1 and COL2 should have one header and COL3 and COL4 another and COL5 none.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This was an issue when the macro was three deep: a macro called a macro that called this macro.&amp;nbsp; I have removed the embedding and the program calls the three macros in succession.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Interesting, at least for me, but I am on a timer &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kevin&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jan 2022 15:40:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loop-as-a-macro-parameter-value/m-p/788303#M252015</guid>
      <dc:creator>KevinViel</dc:creator>
      <dc:date>2022-01-04T15:40:27Z</dc:date>
    </item>
  </channel>
</rss>

