<?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: Zig Zag Matrix In sas in Developers</title>
    <link>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746094#M1199</link>
    <description>&lt;P&gt;You've seen a row-by-row solution with direct calculation of each element of a given row.&amp;nbsp; And a serpentine solution that winds through the matrix.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a row by row solution in which only row 1 is "directly" calculated. Every subsequent row is calculated from the prior row&amp;nbsp; &amp;nbsp;(i.e.&amp;nbsp; &amp;nbsp;X{i,j}=X{i-1,j+1}-1).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Row 1 has day1=1,&amp;nbsp; &amp;nbsp; &amp;nbsp;day2=day1+2, day3=day2+3,&amp;nbsp; etc. up to a maximum increment of the number of&amp;nbsp; rows.&amp;nbsp; &amp;nbsp; And the last &amp;amp;norders increments (for the "extra" array elements below) will be&amp;nbsp; &amp;nbsp;4,3,2,1&amp;nbsp; (for &amp;amp;norders=4).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My first program failed to properly set up the values for order 1 (thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;).&amp;nbsp; Here's the corrected version:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let norders=4;
%let ncols=10;
%let array_length=%eval(&amp;amp;ncols+&amp;amp;norders);

data want (drop=_:);
  order=1;
  array days {&amp;amp;array_length} day1-day&amp;amp;ncols  _dummy1-_dummy&amp;amp;norders (1);

  do order=1 to &amp;amp;norders;
    do _d=ifn(order=1,2,1) to &amp;amp;array_length-order+1;
      _d2=&amp;amp;array_length - _d + 1 ; 
      if order=1 then days{_d}= days{_d-1}+min(_d,_d2,&amp;amp;norders);
      else days{_d}=days{_d+1}-1;
    end;
    output;
  end;
    
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The array is given the length of&amp;nbsp; NORDERS+NCOLS, (not just NCOLS), which is fully populated for order1.&amp;nbsp; This allows the diagonal copying to provide valid values for the lower right segment of the matrix.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also the first element of array DAYS is initialized to a 1, which make programming the do loop iteration over days easier (do &lt;EM&gt;&lt;STRONG&gt;_d=2 to &amp;amp;array_length&lt;/STRONG&gt;&lt;/EM&gt; for order=1,&amp;nbsp; &amp;nbsp;but &lt;EM&gt;&lt;STRONG&gt;_d=1 to &amp;amp;array_length-order+1&lt;/STRONG&gt;&lt;/EM&gt; for other orders).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The _d2 variable is for counting down from the upper bound of the array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 07 Jun 2021 19:23:15 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2021-06-07T19:23:15Z</dc:date>
    <item>
      <title>Zig Zag Matrix In sas</title>
      <link>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/745989#M1191</link>
      <description>&lt;P&gt;Hi Friends,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a query, I need the code for the below output, Could you please me on this. Its the Zig Zag concept I need to implement in SAS.&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 528pt;" border="0" width="704" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" class="xl65" style="height: 15.0pt; width: 48pt;"&gt;Order&lt;/TD&gt;
&lt;TD width="64" class="xl65" style="border-left: none; width: 48pt;"&gt;Day1&lt;/TD&gt;
&lt;TD width="64" class="xl65" style="border-left: none; width: 48pt;"&gt;Day2&lt;/TD&gt;
&lt;TD width="64" class="xl65" style="border-left: none; width: 48pt;"&gt;Day3&lt;/TD&gt;
&lt;TD width="64" class="xl65" style="border-left: none; width: 48pt;"&gt;Day4&lt;/TD&gt;
&lt;TD width="64" class="xl65" style="border-left: none; width: 48pt;"&gt;Day5&lt;/TD&gt;
&lt;TD width="64" class="xl65" style="border-left: none; width: 48pt;"&gt;Day6&lt;/TD&gt;
&lt;TD width="64" class="xl65" style="border-left: none; width: 48pt;"&gt;Day7&lt;/TD&gt;
&lt;TD width="64" class="xl65" style="border-left: none; width: 48pt;"&gt;Day8&lt;/TD&gt;
&lt;TD width="64" class="xl65" style="border-left: none; width: 48pt;"&gt;Day9&lt;/TD&gt;
&lt;TD width="64" class="xl65" style="border-left: none; width: 48pt;"&gt;Day10&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl65" style="height: 15.0pt; border-top: none;"&gt;1&lt;/TD&gt;
&lt;TD align="right" class="xl66" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;TD align="right" class="xl67" style="border-top: none; border-left: none;"&gt;3&lt;/TD&gt;
&lt;TD align="right" class="xl68" style="border-top: none; border-left: none;"&gt;6&lt;/TD&gt;
&lt;TD align="right" class="xl69" style="border-top: none; border-left: none;"&gt;10&lt;/TD&gt;
&lt;TD align="right" class="xl70" style="border-top: none; border-left: none;"&gt;14&lt;/TD&gt;
&lt;TD align="right" class="xl71" style="border-top: none; border-left: none;"&gt;18&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;?&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;?&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;?&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;?&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl65" style="height: 15.0pt; border-top: none;"&gt;2&lt;/TD&gt;
&lt;TD align="right" class="xl67" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;
&lt;TD align="right" class="xl68" style="border-top: none; border-left: none;"&gt;5&lt;/TD&gt;
&lt;TD align="right" class="xl69" style="border-top: none; border-left: none;"&gt;9&lt;/TD&gt;
&lt;TD align="right" class="xl70" style="border-top: none; border-left: none;"&gt;13&lt;/TD&gt;
&lt;TD align="right" class="xl71" style="border-top: none; border-left: none;"&gt;17&lt;/TD&gt;
&lt;TD align="right" class="xl72" style="border-top: none; border-left: none;"&gt;21&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;?&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;?&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;?&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;?&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl65" style="height: 15.0pt; border-top: none;"&gt;3&lt;/TD&gt;
&lt;TD align="right" class="xl68" style="border-top: none; border-left: none;"&gt;4&lt;/TD&gt;
&lt;TD align="right" class="xl69" style="border-top: none; border-left: none;"&gt;8&lt;/TD&gt;
&lt;TD align="right" class="xl70" style="border-top: none; border-left: none;"&gt;12&lt;/TD&gt;
&lt;TD align="right" class="xl71" style="border-top: none; border-left: none;"&gt;16&lt;/TD&gt;
&lt;TD align="right" class="xl72" style="border-top: none; border-left: none;"&gt;20&lt;/TD&gt;
&lt;TD align="right" class="xl73" style="border-top: none; border-left: none;"&gt;23&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;?&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;?&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;?&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;?&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl65" style="height: 15.0pt; border-top: none;"&gt;4&lt;/TD&gt;
&lt;TD align="right" class="xl69" style="border-top: none; border-left: none;"&gt;7&lt;/TD&gt;
&lt;TD align="right" class="xl70" style="border-top: none; border-left: none;"&gt;11&lt;/TD&gt;
&lt;TD align="right" class="xl71" style="border-top: none; border-left: none;"&gt;15&lt;/TD&gt;
&lt;TD align="right" class="xl72" style="border-top: none; border-left: none;"&gt;19&lt;/TD&gt;
&lt;TD align="right" class="xl73" style="border-top: none; border-left: none;"&gt;22&lt;/TD&gt;
&lt;TD align="right" class="xl70" style="border-top: none; border-left: none;"&gt;24&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;?&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;?&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;?&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;?&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ChrisHemedinger_0-1623170453815.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/60134i4AE80AAE36506A23/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ChrisHemedinger_0-1623170453815.png" alt="ChrisHemedinger_0-1623170453815.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The above table for 4*6 table -- I need to create a table automatically n rows and n columns with the same logic.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Jun 2021 16:42:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/745989#M1191</guid>
      <dc:creator>ganeshsas764</dc:creator>
      <dc:date>2021-06-08T16:42:03Z</dc:date>
    </item>
    <item>
      <title>Re: Zig Zag Matrix In sas</title>
      <link>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/745991#M1192</link>
      <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ganeshsas764_0-1622900956924.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/60049iE8765DE073B15D43/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ganeshsas764_0-1622900956924.png" alt="ganeshsas764_0-1622900956924.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The above table for 4*6 table -- I need to create a table automatically n rows and n columns with the same logic.&lt;/P&gt;</description>
      <pubDate>Sat, 05 Jun 2021 13:51:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/745991#M1192</guid>
      <dc:creator>ganeshsas764</dc:creator>
      <dc:date>2021-06-05T13:51:00Z</dc:date>
    </item>
    <item>
      <title>Re: Zig Zag Matrix In sas</title>
      <link>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746019#M1193</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/78522"&gt;@ganeshsas764&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are at least two possible approaches:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;In a loop from &lt;FONT face="courier new,courier"&gt;1&lt;/FONT&gt; to &lt;FONT face="courier new,courier"&gt;m*n&lt;/FONT&gt; (where &lt;FONT face="courier new,courier"&gt;m&lt;/FONT&gt; is the number of rows and &lt;FONT face="courier new,courier"&gt;n&lt;/FONT&gt; the number of columns of the matrix) compute the (row, column) coordinates &lt;FONT face="courier new,courier"&gt;(i, j)&lt;/FONT&gt; of the matrix element where the value of the index variable will occur. Then store this value in a lookup table (e.g., two-dimensional array or hash object). Once the lookup table has been populated, write it to a dataset.&lt;/LI&gt;
&lt;LI&gt;In two nested loops from 1 to&amp;nbsp;&lt;FONT face="courier new,courier"&gt;m&lt;/FONT&gt;&amp;nbsp;and 1 to&amp;nbsp;&lt;FONT face="courier new,courier"&gt;n&lt;/FONT&gt; compute the value that is to occur at the position defined by the values, say, &lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;j&lt;/FONT&gt; of the two index variables. Then those computed values can be written row by row to a dataset.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Maybe approach #1 would be more elegant, but here is an implementation of approach #2:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro zigzag(m,n,out=want);
data &amp;amp;out(rename=(i=Order) drop=j k);
do i=1 to &amp;amp;m;
array Day[&amp;amp;n];
  do j=1 to &amp;amp;n;
    k=i+j-2;
    Day[j]=(k*k+k
            -(k&amp;gt;&amp;amp;m)*(k-&amp;amp;m)*(k-&amp;amp;m+1)
            -(k&amp;gt;&amp;amp;n)*(k-&amp;amp;n)*(k-&amp;amp;n+1))/2 + min(j,&amp;amp;m-i+1);
  end;
  output;
end;
run;
%mend zigzag;

%zigzag(4,6)

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You may want to run a whole series of test cases, which is the purpose of macro&amp;nbsp;&lt;FONT face="courier new,courier"&gt;testzz&lt;/FONT&gt;&amp;nbsp;below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro testzz(maxrows,maxcols,out=want);
%local m n;
%do m=1 %to &amp;amp;maxrows;
  %do n=1 %to &amp;amp;maxcols;
    %zigzag(&amp;amp;m,&amp;amp;n,out=&amp;amp;out)
    title "m=&amp;amp;m n=&amp;amp;n";
    proc print data=&amp;amp;out noobs;
    run;
  %end;
%end;
title;
%mend testzz;

%testzz(6,6)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;(Edit: minor simplifications)&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 05 Jun 2021 17:56:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746019#M1193</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-06-05T17:56:40Z</dc:date>
    </item>
    <item>
      <title>Re: Zig Zag Matrix In sas</title>
      <link>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746028#M1194</link>
      <description>&lt;P&gt;Here is method using a temporary 2-D matrix.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let rows=4;
%let cols=6 ;

data want;
  array x[&amp;amp;rows,&amp;amp;cols] _temporary_ ;
  array out day1-day&amp;amp;cols ;

  do diag=1 to &amp;amp;rows+&amp;amp;cols-1;
    row=min(&amp;amp;rows,diag);
    col=1 + max(0,diag-&amp;amp;rows);
    do while(1&amp;lt;=row and col&amp;lt;=&amp;amp;cols);
      counter+1;
      x[row,col]=counter;
      col+1;
      row+-1;
    end;
  end;

  do row=1 to &amp;amp;rows;
    do col=1 to &amp;amp;cols;
      out[col] = x[row,col];
    end;
    output;
  end;
  drop diag row col counter ;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs    day1    day2    day3    day4    day5    day6

 1       1       3       6      10      14      18
 2       2       5       9      13      17      21
 3       4       8      12      16      20      23
 4       7      11      15      19      22      24
&lt;/PRE&gt;</description>
      <pubDate>Sat, 05 Jun 2021 18:35:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746028#M1194</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-06-05T18:35:37Z</dc:date>
    </item>
    <item>
      <title>Re: Zig Zag Matrix In sas</title>
      <link>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746064#M1195</link>
      <description>&lt;P&gt;Thank you very much for your help with this, it's working fine.&amp;nbsp; &amp;nbsp;I really impressed with your coding skills.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 06 Jun 2021 05:36:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746064#M1195</guid>
      <dc:creator>ganeshsas764</dc:creator>
      <dc:date>2021-06-06T05:36:11Z</dc:date>
    </item>
    <item>
      <title>Re: Zig Zag Matrix In sas</title>
      <link>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746083#M1196</link>
      <description>&lt;P&gt;It is easy for IML.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let nrow=4;
%let ncol=6;

proc iml;
count=0;
max=&amp;amp;nrow &amp;lt;&amp;gt; &amp;amp;ncol;
x=j(max,max,0);

do n=2 to 2#max;
 temp=loc(row(x)+col(x)=n);
 idx=idx//temp[ncol(temp):1];
end;

dim=nrow(x)||ncol(x);
s=ndx2sub(dim,idx);
do i=1 to nrow(s);
 if s[i,1]&amp;lt;=&amp;amp;nrow &amp;amp; s[i,2]&amp;lt;=&amp;amp;ncol then do;
     count=count+1;
	 full=full//(count||s[i,]);
 end;
end;
want=full(full);

create want from want[c=("day1":"day&amp;amp;ncol")];
append from want;
close;
quit;


proc print;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1622981068232.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/60057i17B00DBC70F440EA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1622981068232.png" alt="Ksharp_0-1622981068232.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 06 Jun 2021 12:04:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746083#M1196</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-06-06T12:04:42Z</dc:date>
    </item>
    <item>
      <title>Re: Zig Zag Matrix In sas</title>
      <link>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746092#M1197</link>
      <description>Okay I will try to learn IML, Thanks for your response</description>
      <pubDate>Sun, 06 Jun 2021 14:41:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746092#M1197</guid>
      <dc:creator>ganeshsas764</dc:creator>
      <dc:date>2021-06-06T14:41:17Z</dc:date>
    </item>
    <item>
      <title>Re: Zig Zag Matrix In sas</title>
      <link>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746093#M1198</link>
      <description>Thank you for your response,</description>
      <pubDate>Sun, 06 Jun 2021 14:42:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746093#M1198</guid>
      <dc:creator>ganeshsas764</dc:creator>
      <dc:date>2021-06-06T14:42:13Z</dc:date>
    </item>
    <item>
      <title>Re: Zig Zag Matrix In sas</title>
      <link>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746094#M1199</link>
      <description>&lt;P&gt;You've seen a row-by-row solution with direct calculation of each element of a given row.&amp;nbsp; And a serpentine solution that winds through the matrix.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a row by row solution in which only row 1 is "directly" calculated. Every subsequent row is calculated from the prior row&amp;nbsp; &amp;nbsp;(i.e.&amp;nbsp; &amp;nbsp;X{i,j}=X{i-1,j+1}-1).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Row 1 has day1=1,&amp;nbsp; &amp;nbsp; &amp;nbsp;day2=day1+2, day3=day2+3,&amp;nbsp; etc. up to a maximum increment of the number of&amp;nbsp; rows.&amp;nbsp; &amp;nbsp; And the last &amp;amp;norders increments (for the "extra" array elements below) will be&amp;nbsp; &amp;nbsp;4,3,2,1&amp;nbsp; (for &amp;amp;norders=4).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My first program failed to properly set up the values for order 1 (thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;).&amp;nbsp; Here's the corrected version:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let norders=4;
%let ncols=10;
%let array_length=%eval(&amp;amp;ncols+&amp;amp;norders);

data want (drop=_:);
  order=1;
  array days {&amp;amp;array_length} day1-day&amp;amp;ncols  _dummy1-_dummy&amp;amp;norders (1);

  do order=1 to &amp;amp;norders;
    do _d=ifn(order=1,2,1) to &amp;amp;array_length-order+1;
      _d2=&amp;amp;array_length - _d + 1 ; 
      if order=1 then days{_d}= days{_d-1}+min(_d,_d2,&amp;amp;norders);
      else days{_d}=days{_d+1}-1;
    end;
    output;
  end;
    
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The array is given the length of&amp;nbsp; NORDERS+NCOLS, (not just NCOLS), which is fully populated for order1.&amp;nbsp; This allows the diagonal copying to provide valid values for the lower right segment of the matrix.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also the first element of array DAYS is initialized to a 1, which make programming the do loop iteration over days easier (do &lt;EM&gt;&lt;STRONG&gt;_d=2 to &amp;amp;array_length&lt;/STRONG&gt;&lt;/EM&gt; for order=1,&amp;nbsp; &amp;nbsp;but &lt;EM&gt;&lt;STRONG&gt;_d=1 to &amp;amp;array_length-order+1&lt;/STRONG&gt;&lt;/EM&gt; for other orders).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The _d2 variable is for counting down from the upper bound of the array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Jun 2021 19:23:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746094#M1199</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-06-07T19:23:15Z</dc:date>
    </item>
    <item>
      <title>Re: Zig Zag Matrix In sas</title>
      <link>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746095#M1200</link>
      <description>&lt;P&gt;I like that idea.&lt;/P&gt;
&lt;P&gt;Basically the increment needed for the first row's value in a 4 row by 6 col solution is:&lt;/P&gt;
&lt;PRE&gt;COL:   1  2  3  4  5  6  7  8  9 10
Value: 1  3  6 10 14 18 22 25 27 28
Diff:  1  2  3  4  4  4  4  3  2  1&lt;/PRE&gt;
&lt;P&gt;So the amount to increment increases by one up to the number of ROWS and stays constant until reaching the number of columns, and then the increment decreases by one each time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let rows=4 ;
%let cols=6 ;
data want ;
  do row=1 to &amp;amp;rows;
    array x day1-day&amp;amp;cols d1-d&amp;amp;rows ;
    if row=1 then do increment=1 to &amp;amp;cols,&amp;amp;rows to 1 by -1 ;
      col+1;
      next+min(&amp;amp;rows,increment);
      x[col]=next;
    end;
    else do col=1 to dim(x)-1;
      x[col]=x[col+1]-1;
    end;
    output;
  end;
  drop next col increment d1-d&amp;amp;rows;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note the advantage of my first solution using the 2-D temporary array is that it is easy to adapt to fill the "matrix" with values from a dataset instead of the just the sequence 1,2,3,...&lt;/P&gt;</description>
      <pubDate>Sun, 06 Jun 2021 16:10:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746095#M1200</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-06-06T16:10:29Z</dc:date>
    </item>
    <item>
      <title>Re: Zig Zag Matrix In sas</title>
      <link>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746204#M1201</link>
      <description>&lt;P&gt;mkeintz,&lt;/P&gt;
&lt;P&gt;When I set the following:&lt;/P&gt;
&lt;P&gt;%let norders=40;&lt;BR /&gt;%let ncols=2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your code get wrong result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1623066707931.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/60079i8300E40205D05C43/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1623066707931.png" alt="Ksharp_0-1623066707931.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Jun 2021 11:51:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746204#M1201</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-06-07T11:51:47Z</dc:date>
    </item>
    <item>
      <title>Re: Zig Zag Matrix In sas</title>
      <link>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746207#M1202</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp; ,&lt;/P&gt;
&lt;P&gt;I found another version of Zig Zag Matrix in internet.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1623066830380.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/60080i325F762E65D56F3D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1623066830380.png" alt="Ksharp_0-1623066830380.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do you get it ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I post IML code firstly here .&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let nrow=4 ;
%let ncol=6;

proc iml;
max=&amp;amp;nrow &amp;lt;&amp;gt; &amp;amp;ncol;
j=j(max,max,0);
do n=2 to 2#max;
  temp=loc(row(j)+col(j)=n);
  if mod(n-1,2)=1 then idx=idx||t(temp[ncol(temp):1]);
     else idx=idx||temp;
end;
dim=max||max;
sub=ndx2sub(dim,idx) ;
count=0;
do i=1 to nrow(sub);
  if sub[i,1]&amp;lt;=&amp;amp;nrow &amp;amp; sub[i,2]&amp;lt;=&amp;amp;ncol then do;
    count=count+1;
 full=full//(count||sub[i,]);
  end;
end;
want=full(full);
create want from want[c=("date1":"date&amp;amp;ncol")];
append from want;
close;
quit;


proc print;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_1-1623066904842.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/60081i85E34E0387B2966C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_1-1623066904842.png" alt="Ksharp_1-1623066904842.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Jun 2021 11:55:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746207#M1202</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-06-07T11:55:04Z</dc:date>
    </item>
    <item>
      <title>Re: Zig Zag Matrix In sas</title>
      <link>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746306#M1205</link>
      <description>&lt;P&gt;So that one is alternating the order that the diagonals are traversed instead of always going in the same direction.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let rows=4;
%let cols=6;

data want;
  array x[&amp;amp;rows,&amp;amp;cols] _temporary_ ;
  array out day1-day&amp;amp;cols ;
  do diag=1 to &amp;amp;rows+&amp;amp;cols-1;
    direction=-1+2*mod(diag,2);
    if direction=1 then do;
      row=min(diag,&amp;amp;rows);
      col=1 + max(0,diag-&amp;amp;rows);
    end;
    else do;
      col=min(diag,&amp;amp;cols) ;
      row=1 + max(0,diag-&amp;amp;cols);
    end;
    do while(1&amp;lt;=row&amp;lt;=&amp;amp;rows and 1&amp;lt;=col&amp;lt;=&amp;amp;cols);
      counter+1;
      x[row,col]=counter;
      col+direction;
      row+-direction;
    end;
  end;

  do row=1 to &amp;amp;rows;
    do col=1 to &amp;amp;cols;
      out[col] = x[row,col];
    end;
    output;
  end;
  drop diag row col direction counter ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;To modify the above code to replicate the original ZigZag order just force DIRECTION to always be one instead of alternating between 1 and -1.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Jun 2021 17:27:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746306#M1205</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-06-07T17:27:05Z</dc:date>
    </item>
    <item>
      <title>Re: Zig Zag Matrix In sas</title>
      <link>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746433#M1206</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp; ,&lt;/P&gt;
&lt;P&gt;I still get wrong result when I set this :&lt;/P&gt;
&lt;P&gt;%let norders=40;&lt;BR /&gt;%let ncols=2;&lt;BR /&gt;%let array_length=%eval(&amp;amp;ncols+&amp;amp;norders);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1623154135281.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/60117iB2D8EFE267192074/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1623154135281.png" alt="Ksharp_0-1623154135281.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Jun 2021 12:09:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746433#M1206</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-06-08T12:09:12Z</dc:date>
    </item>
    <item>
      <title>Re: Zig Zag Matrix In sas</title>
      <link>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746434#M1207</link>
      <description>Tom, &lt;BR /&gt;Cool. It is great honor for pfizer to have you  vice-president .</description>
      <pubDate>Tue, 08 Jun 2021 12:14:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Zig-Zag-Matrix-In-sas/m-p/746434#M1207</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-06-08T12:14:01Z</dc:date>
    </item>
  </channel>
</rss>

