<?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: Rolling Cumulative Sum in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Rolling-Cumulative-Sum/m-p/108000#M39298</link>
    <description>&lt;P&gt;This should be a little faster. It is a simple offset counting routine, it hinges on the the power of the Loc() and matrix functions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Items to Note:&lt;/P&gt;
&lt;UL style="list-style-position: inside;"&gt;
&lt;LI&gt;change the path or user_id or set dt = current data table();&lt;/LI&gt;
&lt;LI&gt;creating a constant like &lt;STRONG&gt;drng&lt;/STRONG&gt;&amp;nbsp;finds&lt;STRONG&gt; In Days&lt;/STRONG&gt;(3) once possibly saving finding it 500k times.&amp;nbsp; This depends upon how JMP optimizes/parses JSL code.&lt;/LI&gt;
&lt;LI&gt;In line 30 of the script, I used &lt;STRONG&gt;&amp;lt;= drng&amp;nbsp;&lt;/STRONG&gt;so that the results of this script would match your results.&amp;nbsp; However, consider whether this should be a strict less than (&amp;lt;). Row 37 in your table sums rows 33-37, Jan19 thru Jan22, that is 4 days. There are other instances. Suppose you wanted the last 30 days of January, that should be Jan02 thru Jan31.&lt;/LI&gt;
&lt;LI&gt;:Date is formatted as m/d/y, however, the date value includes time. If you wanted to roll by calendar day (not time), comment out line 30 and uncomment line 31.&amp;nbsp; The value 86400 = 24*60*60 or &lt;STRONG&gt;In Days&lt;/STRONG&gt;(1)&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default to Here(1);

dt = Open("c:\users\&amp;lt;user_id&amp;gt;\downloads\cumulative_sum_test_table.jmp");

//This assumes the data table is soted by Group and SubGroup and Date
scol = dt &amp;lt;&amp;lt; new column("Sequence", numeric, continuous);
scol &amp;lt;&amp;lt; set each value(Row());
ccol = dt &amp;lt;&amp;lt; new Column("CUMULATIVE_SUM_VALUE_2", numeric, continuous);
summarize(Data Table(dt), _ttt = By(:GROUP, :SUBGROUP), _ibeg=Min(scol), _iend=Max(scol) );

dt &amp;lt;&amp;lt; delete column(scol);  //no longer needed

dmat = :DATE  &amp;lt;&amp;lt; get as matrix;
vmat = :VALUE &amp;lt;&amp;lt; get as matrix;

//for each group
drng = In Days(3);  //259200
For(i=1, i&amp;lt;=nitems(_ttt[1]), i++,
    b = _ibeg[i];
    e = _iend[i];
	dsub = dmat[ b::e ];
	vsub = vmat[ b::e ];
	For(j = b, j &amp;lt;= e, j++,
	    //j=37
	    k= j-b+1;
//JMP12 converted a 1x1 matrix to a scalar
        if(k==1, 
           ccol[j] = vsub[1] 
           ,    //else
           idx = loc( 0&amp;lt;= -1 * dsub[1::k] + dmat[j] &amp;lt;= drng  );  //should this be &amp;lt; not &amp;lt;=&lt;BR /&gt;           //idx = loc( 0&amp;lt;= Floor( (-1 * dsub[1::k] + dmat[j])/86400) &amp;lt;= 3 );   // &amp;lt; ?? 
		   ccol[j] = sum(vsub[idx])
		 );  //end if
	); // end for j
);

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 25 Jan 2019 13:38:34 GMT</pubDate>
    <dc:creator>gzmorgan0</dc:creator>
    <dc:date>2019-01-25T13:38:34Z</dc:date>
    <item>
      <title>Rolling Cumulative Sum</title>
      <link>https://community.jmp.com/t5/Discussions/Rolling-Cumulative-Sum/m-p/107998#M39296</link>
      <description>&lt;P&gt;I am trying to find an efficient way to calculate Rolling 3-day CUMULATIVE SUM.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have some for loops that work, but the data sets I am working with are ~500k rows with many more GROUPS, SUBGROUPS and VALUE columns so it takes ~30 minutes to calculate. The example data set is very stripped down and basic.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I moved the values into a matrix and that speeds it up a little, but not enough.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is a GROUP and SUBGROUP columns that are to be CUMULATIVE SUM seperately from eachother. Also for each row the CUMULATIVESUM can only be for DATE - 3 days. Anything before :DATE[]-3 days, and after :DATE[] is not used for that row's CUMULATIVE SUM.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a feeling a column formula using LAG will probably be fastest, but I cannot get it to work properly.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Speed is very important.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried subsetting the tables, and that just made it slower.&lt;/P&gt;&lt;P&gt;See table and JSL. The top two JSL methods work perfectly, the third JSL method doesn't work properly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank You!&lt;/P&gt;&lt;P&gt;[edit] JMP Pro 12.2.0&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="table.JPG" style="width: 549px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/15234iD3357EB66281E264/image-size/large?v=v2&amp;amp;px=999" role="button" title="table.JPG" alt="table.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = Current Data Table();

For( i = 1, i &amp;lt;= N Rows( dt ), i = i + 1,
:CUMULATIVE_SUM_VALUE[i] = :VALUE[i];
For( c = i - 1, c &amp;gt; 0, c = c - 1,
If( :SUBGROUP[c] == :SUBGROUP[i] &amp;amp; :GROUP[c] == :GROUP[i] &amp;amp; (:DATE[i] - :DATE[c]) &amp;lt;= In Days( 3 ),
:CUMULATIVE_SUM_VALUE[i] = :CUMULATIVE_SUM_VALUE[i] + :VALUE[c],
c = -1
)
);
);



//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////

//1=GROUP, 2=SUBGROUP, 3=DATE, 4=VALUE, 5=CUMULATIVE_SUM_VALUE

TT = dt &amp;lt;&amp;lt; Get All Columns As Matrix;

For( i = 1, i &amp;lt;= N Rows( TT ), i = i + 1,
TT[i, 5] = TT[i, 4];
For( c = i - 1, c &amp;gt; 0, c = c - 1,
If( TT[c, 2] == TT[i, 2] &amp;amp; TT[c, 1] == TT[i, 1] &amp;amp; (TT[i, 3] - TT[c, 3]) &amp;lt;= In Days( 3 ),
TT[i, 5] = TT[i, 5] + TT[c, 4],
c = -1
)
);
);

For Each Row( :CUMULATIVE_SUM_VALUE = TT[Row(), 5] );


//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////

dt &amp;lt;&amp;lt; New Column( "CUMULATIVE_SUM_VALUE_2",
Formula(
//:VALUE;
If(
Row() == 1, :VALUE,
:GROUP == Lag( :GROUP, 1 ) &amp;amp; :SUBGROUP == Lag( :SUBGROUP, 1 ) &amp;amp; :DATE - Lag( :DATE, 1 ) &amp;lt;= In Days( 3 ), :VALUE + Lag( :CUMULATIVE_SUM_VALUE, 1 )
,
:GROUP != Lag( :GROUP, 1 ) &amp;amp; :SUBGROUP != Lag( :SUBGROUP, 1 ) &amp;amp; :DATE - Lag( :DATE, 1 ) &amp;lt;= In Days( 3 ), :VALUE
,
:GROUP == Lag( :GROUP, 1 ) &amp;amp; :SUBGROUP == Lag( :SUBGROUP, 1 ) &amp;amp; :DATE - Lag( :DATE, 1 ) &amp;gt; In Days( 3 ), :VALUE
,
:GROUP != Lag( :GROUP, 1 ) | :SUBGROUP != Lag( :SUBGROUP, 1 ), :VALUE

)
),
Eval Formula
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Jan 2019 10:13:37 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Rolling-Cumulative-Sum/m-p/107998#M39296</guid>
      <dc:creator>ts2</dc:creator>
      <dc:date>2019-01-25T10:13:37Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Cumulative Sum</title>
      <link>https://community.jmp.com/t5/Discussions/Rolling-Cumulative-Sum/m-p/108000#M39298</link>
      <description>&lt;P&gt;This should be a little faster. It is a simple offset counting routine, it hinges on the the power of the Loc() and matrix functions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Items to Note:&lt;/P&gt;
&lt;UL style="list-style-position: inside;"&gt;
&lt;LI&gt;change the path or user_id or set dt = current data table();&lt;/LI&gt;
&lt;LI&gt;creating a constant like &lt;STRONG&gt;drng&lt;/STRONG&gt;&amp;nbsp;finds&lt;STRONG&gt; In Days&lt;/STRONG&gt;(3) once possibly saving finding it 500k times.&amp;nbsp; This depends upon how JMP optimizes/parses JSL code.&lt;/LI&gt;
&lt;LI&gt;In line 30 of the script, I used &lt;STRONG&gt;&amp;lt;= drng&amp;nbsp;&lt;/STRONG&gt;so that the results of this script would match your results.&amp;nbsp; However, consider whether this should be a strict less than (&amp;lt;). Row 37 in your table sums rows 33-37, Jan19 thru Jan22, that is 4 days. There are other instances. Suppose you wanted the last 30 days of January, that should be Jan02 thru Jan31.&lt;/LI&gt;
&lt;LI&gt;:Date is formatted as m/d/y, however, the date value includes time. If you wanted to roll by calendar day (not time), comment out line 30 and uncomment line 31.&amp;nbsp; The value 86400 = 24*60*60 or &lt;STRONG&gt;In Days&lt;/STRONG&gt;(1)&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default to Here(1);

dt = Open("c:\users\&amp;lt;user_id&amp;gt;\downloads\cumulative_sum_test_table.jmp");

//This assumes the data table is soted by Group and SubGroup and Date
scol = dt &amp;lt;&amp;lt; new column("Sequence", numeric, continuous);
scol &amp;lt;&amp;lt; set each value(Row());
ccol = dt &amp;lt;&amp;lt; new Column("CUMULATIVE_SUM_VALUE_2", numeric, continuous);
summarize(Data Table(dt), _ttt = By(:GROUP, :SUBGROUP), _ibeg=Min(scol), _iend=Max(scol) );

dt &amp;lt;&amp;lt; delete column(scol);  //no longer needed

dmat = :DATE  &amp;lt;&amp;lt; get as matrix;
vmat = :VALUE &amp;lt;&amp;lt; get as matrix;

//for each group
drng = In Days(3);  //259200
For(i=1, i&amp;lt;=nitems(_ttt[1]), i++,
    b = _ibeg[i];
    e = _iend[i];
	dsub = dmat[ b::e ];
	vsub = vmat[ b::e ];
	For(j = b, j &amp;lt;= e, j++,
	    //j=37
	    k= j-b+1;
//JMP12 converted a 1x1 matrix to a scalar
        if(k==1, 
           ccol[j] = vsub[1] 
           ,    //else
           idx = loc( 0&amp;lt;= -1 * dsub[1::k] + dmat[j] &amp;lt;= drng  );  //should this be &amp;lt; not &amp;lt;=&lt;BR /&gt;           //idx = loc( 0&amp;lt;= Floor( (-1 * dsub[1::k] + dmat[j])/86400) &amp;lt;= 3 );   // &amp;lt; ?? 
		   ccol[j] = sum(vsub[idx])
		 );  //end if
	); // end for j
);

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Jan 2019 13:38:34 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Rolling-Cumulative-Sum/m-p/108000#M39298</guid>
      <dc:creator>gzmorgan0</dc:creator>
      <dc:date>2019-01-25T13:38:34Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Cumulative Sum</title>
      <link>https://community.jmp.com/t5/Discussions/Rolling-Cumulative-Sum/m-p/108010#M39303</link>
      <description>&lt;P&gt;This works great. I am seeing &amp;gt;200% speed increase using this method with my dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There was a small bug where if there was only one row for a SubGroup (you had a JSL comment about a 1x1 matrix converted to scalar). I have no idea why this would happen.&lt;/P&gt;&lt;P&gt;I made sure these would stay a matrix as so:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;	dsub = Matrix(dmat[b :: e]);
	vsub = Matrix(vmat[b :: e]);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Jan 2019 15:41:47 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Rolling-Cumulative-Sum/m-p/108010#M39303</guid>
      <dc:creator>ts2</dc:creator>
      <dc:date>2019-01-25T15:41:47Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Cumulative Sum</title>
      <link>https://community.jmp.com/t5/Discussions/Rolling-Cumulative-Sum/m-p/108030#M39315</link>
      <description>&lt;P&gt;Good catch. I made a rookie mistake not catching the corner case for a 1 row subgroup. I am so glad JMP no longer converts a 1x1 matrix to a scalar.&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-jsl"&gt;&lt;CODE class="  language-jsl"&gt;&lt;SPAN class="token comment"&gt;//JMP12 converted a 1x1 matrix to a scalar&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Jan 2019 23:24:34 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Rolling-Cumulative-Sum/m-p/108030#M39315</guid>
      <dc:creator>gzmorgan0</dc:creator>
      <dc:date>2019-01-25T23:24:34Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Cumulative Sum</title>
      <link>https://community.jmp.com/t5/Discussions/Rolling-Cumulative-Sum/m-p/925301#M108335</link>
      <description>&lt;P&gt;No idea if this would be faster or slower, but it was a lot simpler for me to conceptualize (as an inexperienced scripter).&lt;/P&gt;
&lt;P&gt;Assuming the table is sorted by GROUP, SUBGROUP, &amp;amp; date, and that you would never have more than 10 data points in a 3 day period (you can change this), this formula would do the trick.&lt;/P&gt;
&lt;DIV&gt;&lt;EM&gt;Summation( i = 0, 10,&lt;/EM&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;EM&gt; If( :GROUP == Lag( :GROUP, i ) &amp;amp; :SUBGROUP == Lag( :SUBGROUP, i ) &amp;amp; Date Difference( Lag( :DATE, i ), :DATE, "Day" ) &amp;lt;= 3,&lt;/EM&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;EM&gt; Lag( :VALUE, i ),&lt;/EM&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;EM&gt; 0&lt;/EM&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;EM&gt; )&lt;/EM&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;EM&gt;)&lt;/EM&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;Cheers&lt;/DIV&gt;</description>
      <pubDate>Wed, 21 Jan 2026 22:10:06 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Rolling-Cumulative-Sum/m-p/925301#M108335</guid>
      <dc:creator>Frooksie</dc:creator>
      <dc:date>2026-01-21T22:10:06Z</dc:date>
    </item>
  </channel>
</rss>

