<?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: Macro code to repeat a code chunk in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-repeat-a-code-chunk/m-p/965656#M375924</link>
    <description>&lt;P&gt;To do that in macro code you will need to use a %DO loop.&amp;nbsp; &amp;nbsp;Note you will only be able to generate N-1 statements.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro running_log_sum(n);
%local i ;
%do i=1 %to &amp;amp;n-1;
c&amp;amp;i=log(sum(of const col1-col&amp;amp;i)/sum(of const col%eval(&amp;amp;i+1)-col&amp;amp;n));
%end;
%mend running_log_sum;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then call the macro to generate those statements inside of your data step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a; 
 set tran;
 const=0;
 %running_log_sum(15)
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you call it with N=7 you will get these statements generated.&lt;/P&gt;
&lt;PRE&gt; MPRINT(RUNNING_LOG_SUM):   c1=log(sum(of const col1-col1)/sum(of const col2-col7));
 MPRINT(RUNNING_LOG_SUM):   c2=log(sum(of const col1-col2)/sum(of const col3-col7));
 MPRINT(RUNNING_LOG_SUM):   c3=log(sum(of const col1-col3)/sum(of const col4-col7));
 MPRINT(RUNNING_LOG_SUM):   c4=log(sum(of const col1-col4)/sum(of const col5-col7));
 MPRINT(RUNNING_LOG_SUM):   c5=log(sum(of const col1-col5)/sum(of const col6-col7));
 MPRINT(RUNNING_LOG_SUM):   c6=log(sum(of const col1-col6)/sum(of const col7-col7));&lt;/PRE&gt;
&lt;P&gt;Note if you are already using a macro to generate that data step then there is no need to generate another macro since you can just put the %DO loop into your current macro at that point.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to do it without macro code then a little restructuring will make it more efficient.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a; 
  set tran;
  const=0;
  array cols col1-col14 ;
  array c [13] ;
  total = sum(of const cols[*]);
  left = const;
  do i=1 to dim(cols)-1;
    left+cols[i];
    c[i] = log(left/(total-left));
  end;
  drop total left;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 03 May 2025 15:42:58 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2025-05-03T15:42:58Z</dc:date>
    <item>
      <title>Macro code to repeat a code chunk</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-repeat-a-code-chunk/m-p/965629#M375911</link>
      <description>&lt;P&gt;I would like the lines below for c1= to c4= to go on for 14 columns. That means col5 is col14 and I should have c1= to c14=.&lt;/P&gt;
&lt;P&gt;Could someone please share a macro to do this loop?:&lt;/P&gt;
&lt;P&gt;data a; set tran;&lt;BR /&gt;const=0;&lt;BR /&gt;c1=log((sum(of col1-col1)+const)/(sum(of col2-col5)+const));&lt;BR /&gt;c2=log((sum(of col1-col2)+const)/(sum(of col3-col5)+const));&lt;BR /&gt;c3=log((sum(of col1-col3)+const)/(sum(of col4-col5)+const));&lt;BR /&gt;c4=log((sum(of col1-col4)+const)/(sum(of col5-col5)+const));&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Sat, 03 May 2025 01:08:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-repeat-a-code-chunk/m-p/965629#M375911</guid>
      <dc:creator>pink_poodle</dc:creator>
      <dc:date>2025-05-03T01:08:24Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code to repeat a code chunk</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-repeat-a-code-chunk/m-p/965642#M375915</link>
      <description>&lt;P&gt;You likely don't need any macro code. arrays in a data step should do it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
   set tran;
   Array c (14); /* This assumes that the variables C1 through c14 do not exist*/
                 /* this array statement will create C1 through c14*/
   Array cl (*) col1 - col15 /* this assumes the varaibles col1 -col15 exist 
                                and that you meant to have col15 as the last
                                instead of col5  (not actually stated in your
                                problem*/
  do i= 1 to 14;
     temp = sum(of cl[1] - cl[i ])+const)/(sum(of cl[i+1]-cl[15])+const);
     if temp &amp;gt; 0 the c[i]=log(temp);
  end;
  drop i temp;
run;&lt;/PRE&gt;
&lt;P&gt;Arrays are way of having shorthand references to groups of variables. The ARRAY statement creates the association with a (usually short name, such as C or CL above). Then and INDEX value that appears in parentheses identifies which specific value in numeric order of definition is meant to be used. So C[i] references C1, C2, etc to C14. Note that arithmetic may be used in the index to reference offsets such as CL[i+1] which references CL2 when i=1 or the second variable Col2 in the CL array.&lt;/P&gt;
&lt;P&gt;Since log of negative or 0 values is undefined I create a temporary variable to hold the results of the sums and division and test that is valid before using the log function. That might not be needed but I have no idea what actual values are in your variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Arrays are often the first thing to investigate when doing a series of related or basically identical calculations.&lt;/P&gt;</description>
      <pubDate>Sat, 03 May 2025 06:24:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-repeat-a-code-chunk/m-p/965642#M375915</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2025-05-03T06:24:29Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code to repeat a code chunk</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-repeat-a-code-chunk/m-p/965650#M375918</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/235176"&gt;@pink_poodle&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your SAS installation (like my ageing SAS 9.4M5 from 2016) does not yet support the convenient variable list syntax using array references suggested by &lt;A href="https://communities.sas.com/t5/forums/replypage/board-id/programming/message-id/375911#" target="_blank" rel="noopener"&gt;ballardw&lt;/A&gt;, you may want to resort to your idea of a macro loop, which could look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro cdef(m);
%local i;
%do i=1 %to %eval(&amp;amp;m-1);
  c&amp;amp;i=log((sum(of col1-col&amp;amp;i)+const)/(sum(of col%eval(&amp;amp;i+1)-col&amp;amp;m)+const));   
%end;
%mend cdef;

data want;
set tran;
const=0;
%cdef(15)
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that your description&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/235176"&gt;@pink_poodle&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I would like the lines below for c1= to c4= to go on for 14 columns. That means col5 is col14 and I should have c1= to c14=.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;is not consistent with your code, where the maximum &lt;FONT face="courier new,courier"&gt;col&lt;/FONT&gt; index equals the maximum &lt;FONT face="courier new,courier"&gt;c&lt;/FONT&gt; index &lt;EM&gt;plus 1&lt;/EM&gt;, so not both maximum indices can be 14. Hence, you'll need to replace &lt;FONT face="courier new,courier"&gt;%cdef(15)&lt;/FONT&gt; with&amp;nbsp;&lt;FONT face="courier new,courier"&gt;%cdef(14)&lt;/FONT&gt; in my suggested code if &lt;FONT face="courier new,courier"&gt;col15&lt;/FONT&gt;&amp;nbsp;does not exist in your real TRAN dataset.&lt;/P&gt;</description>
      <pubDate>Sat, 03 May 2025 12:47:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-repeat-a-code-chunk/m-p/965650#M375918</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2025-05-03T12:47:35Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code to repeat a code chunk</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-repeat-a-code-chunk/m-p/965656#M375924</link>
      <description>&lt;P&gt;To do that in macro code you will need to use a %DO loop.&amp;nbsp; &amp;nbsp;Note you will only be able to generate N-1 statements.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro running_log_sum(n);
%local i ;
%do i=1 %to &amp;amp;n-1;
c&amp;amp;i=log(sum(of const col1-col&amp;amp;i)/sum(of const col%eval(&amp;amp;i+1)-col&amp;amp;n));
%end;
%mend running_log_sum;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then call the macro to generate those statements inside of your data step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a; 
 set tran;
 const=0;
 %running_log_sum(15)
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you call it with N=7 you will get these statements generated.&lt;/P&gt;
&lt;PRE&gt; MPRINT(RUNNING_LOG_SUM):   c1=log(sum(of const col1-col1)/sum(of const col2-col7));
 MPRINT(RUNNING_LOG_SUM):   c2=log(sum(of const col1-col2)/sum(of const col3-col7));
 MPRINT(RUNNING_LOG_SUM):   c3=log(sum(of const col1-col3)/sum(of const col4-col7));
 MPRINT(RUNNING_LOG_SUM):   c4=log(sum(of const col1-col4)/sum(of const col5-col7));
 MPRINT(RUNNING_LOG_SUM):   c5=log(sum(of const col1-col5)/sum(of const col6-col7));
 MPRINT(RUNNING_LOG_SUM):   c6=log(sum(of const col1-col6)/sum(of const col7-col7));&lt;/PRE&gt;
&lt;P&gt;Note if you are already using a macro to generate that data step then there is no need to generate another macro since you can just put the %DO loop into your current macro at that point.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to do it without macro code then a little restructuring will make it more efficient.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a; 
  set tran;
  const=0;
  array cols col1-col14 ;
  array c [13] ;
  total = sum(of const cols[*]);
  left = const;
  do i=1 to dim(cols)-1;
    left+cols[i];
    c[i] = log(left/(total-left));
  end;
  drop total left;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 03 May 2025 15:42:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-repeat-a-code-chunk/m-p/965656#M375924</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-05-03T15:42:58Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code to repeat a code chunk</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-repeat-a-code-chunk/m-p/965658#M375926</link>
      <description>&lt;P&gt;Variable lists cannot be made with array references.&amp;nbsp; Variable lists are evaluated while the data step is being compiled.&amp;nbsp; Array references occur while the data step is executing.&amp;nbsp; They are incompatible.&lt;/P&gt;
&lt;PRE&gt;1    data test;
2      array x [10] (1:10);
3      i=1;
4      y=sum(of x[1]-x[10]);
                    -
                    22
ERROR 22-322: Syntax error, expecting one of the following: ), ','.

5     put (_all_) (=);
6    run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.TEST may be incomplete.  When this step was stopped there were 0 observations and 12 variables.
WARNING: Data set WORK.TEST was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 03 May 2025 22:05:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-repeat-a-code-chunk/m-p/965658#M375926</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-05-03T22:05:43Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code to repeat a code chunk</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-repeat-a-code-chunk/m-p/965671#M375935</link>
      <description>&lt;P&gt;Thank you so much,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;! These are very helpful solutions! I used the code chunk right above this message.&lt;/P&gt;</description>
      <pubDate>Sun, 04 May 2025 02:32:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-repeat-a-code-chunk/m-p/965671#M375935</guid>
      <dc:creator>pink_poodle</dc:creator>
      <dc:date>2025-05-04T02:32:21Z</dc:date>
    </item>
  </channel>
</rss>

