<?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 Cumulative Sum by Rows in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-Sum-by-Rows/m-p/43900#M210</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;SPAN style="font-size: 8pt;"&gt;&lt;P&gt;&lt;SPAN style="font-size: 8pt;"&gt;A simple module for finding row-wise cumulative sum in IML:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 8pt;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Proc IML;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Start rowcumsum(x);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Step1 = cusum(x);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Step2 = 0 // Step1[1:nrow(Step1)-1,ncol(Step1)];&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Output = Step1 - repeat(Step2,1,ncol(Step1));&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return(Output);&lt;BR /&gt;Finish rowcumsum;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 8pt;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;a = {1 2 3,4 5 6, 7 8 9};&lt;BR /&gt;SASCUSUM = cusum(a);&lt;BR /&gt;ROWCUSUM = rowcumsum(a);&lt;/P&gt;&lt;P&gt;print a; print SASCUSUM; Print ROWCUSUM;&lt;BR /&gt;Quit;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 8pt;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 8pt;"&gt;a&lt;/SPAN&gt;&lt;/P&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;01 02 03&lt;/P&gt;&lt;P&gt;04 05 06&lt;/P&gt;&lt;P&gt;07 08 09&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;SASCUSUM&lt;/P&gt;&lt;P&gt;01 03 06&lt;/P&gt;&lt;P&gt;10 15 21&lt;/P&gt;&lt;P&gt;28 36 45&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;ROWCUSUM&lt;/P&gt;&lt;P&gt;01 03 06&lt;/P&gt;&lt;P&gt;04 09 15&lt;/P&gt;&lt;P&gt;07 15 24&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 02 Feb 2012 05:31:12 GMT</pubDate>
    <dc:creator>Ajay88</dc:creator>
    <dc:date>2012-02-02T05:31:12Z</dc:date>
    <item>
      <title>Cumulative Sum by Rows</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-Sum-by-Rows/m-p/43900#M210</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;SPAN style="font-size: 8pt;"&gt;&lt;P&gt;&lt;SPAN style="font-size: 8pt;"&gt;A simple module for finding row-wise cumulative sum in IML:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 8pt;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Proc IML;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Start rowcumsum(x);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Step1 = cusum(x);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Step2 = 0 // Step1[1:nrow(Step1)-1,ncol(Step1)];&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Output = Step1 - repeat(Step2,1,ncol(Step1));&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return(Output);&lt;BR /&gt;Finish rowcumsum;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 8pt;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;a = {1 2 3,4 5 6, 7 8 9};&lt;BR /&gt;SASCUSUM = cusum(a);&lt;BR /&gt;ROWCUSUM = rowcumsum(a);&lt;/P&gt;&lt;P&gt;print a; print SASCUSUM; Print ROWCUSUM;&lt;BR /&gt;Quit;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 8pt;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 8pt;"&gt;a&lt;/SPAN&gt;&lt;/P&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;01 02 03&lt;/P&gt;&lt;P&gt;04 05 06&lt;/P&gt;&lt;P&gt;07 08 09&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;SASCUSUM&lt;/P&gt;&lt;P&gt;01 03 06&lt;/P&gt;&lt;P&gt;10 15 21&lt;/P&gt;&lt;P&gt;28 36 45&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;ROWCUSUM&lt;/P&gt;&lt;P&gt;01 03 06&lt;/P&gt;&lt;P&gt;04 09 15&lt;/P&gt;&lt;P&gt;07 15 24&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Feb 2012 05:31:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-Sum-by-Rows/m-p/43900#M210</guid>
      <dc:creator>Ajay88</dc:creator>
      <dc:date>2012-02-02T05:31:12Z</dc:date>
    </item>
    <item>
      <title>Cumulative Sum by Rows</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-Sum-by-Rows/m-p/43901#M211</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You didn't ask a question, so I can't tell whether you are saying "here is how to do this" or whether you are asking "is this the best way to do this."&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If the former, yes, this is a good way to compute cumulative sums across rows of a matrix.It is a clever algorithm.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you are asking whether this is the fastest way, I'll offer two observations: you can use the &lt;A href="http://support.sas.com/documentation/cdl/en/imlug/64248/HTML/default/viewer.htm#imlug_langref_sect172.htm"&gt;LAG function&lt;/A&gt; (introduced in SAS/IML 9.22) to speed up Step2, and &lt;A href="http://blogs.sas.com/content/iml/2010/12/06/shorthand-notation-for-row-and-column-operations/"&gt;you don't need the REPEAT function&lt;/A&gt; when forming the OUTPUT matrix. Thus the following code is slightly more efficient:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;start rs(x);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Step1 = cusum(x);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Step2 = lag(Step1[,ncol(Step1)], 1); /* returns missing in Step2[1] */&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Step2[1] = 0; /* replace missing value with 0 */&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Output = Step1 - Step2;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; return( Output);&lt;/P&gt;&lt;P&gt;finish;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Unless your matrices are huge, it doesn't really matter which module you run. In fact, unless the number of rows is huge, you can just use a simple DO loop and the computation is just as fast:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Output = j(nrow(x), ncol(x));&lt;/P&gt;&lt;P&gt;do i = 1 to nrow(x);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Output[i,] = cusum(x[i,]);&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Feb 2012 14:12:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-Sum-by-Rows/m-p/43901#M211</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2012-02-02T14:12:00Z</dc:date>
    </item>
    <item>
      <title>Cumulative Sum by Rows</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-Sum-by-Rows/m-p/43902#M212</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for the variations, Rick. I just wanted to share the piece of code that I repeatedly found useful.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; My co's yet to make the step up from 9.20, which is slightly irritating because 9.22 has a decent set of functions introduced ... so instead of say, cuprod(x) which is available in 9.22, I use exp(cusum(log(x)))&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The loop increases the computation time drastically if this function is used within an optimization code. I noticed IML has this amazing speed-up (nearly 15x) for vectorized vs loop section (similar results to your blog post on running mean and variance).The results were a bit surprising because MATLAB hardly produced a 1.5x speedup for the same optimization code, and a fully vectorized optimization code in IML runs 3x faster than the same code in MATLAB!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 Feb 2012 11:15:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-Sum-by-Rows/m-p/43902#M212</guid>
      <dc:creator>Ajay88</dc:creator>
      <dc:date>2012-02-03T11:15:39Z</dc:date>
    </item>
    <item>
      <title>Cumulative Sum by Rows</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-Sum-by-Rows/m-p/43903#M213</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt; Impressive results! Congratulations, and thanks for sharing!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 Feb 2012 11:32:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-Sum-by-Rows/m-p/43903#M213</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2012-02-03T11:32:04Z</dc:date>
    </item>
  </channel>
</rss>

