<?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 Cumulated sum of rows in new column by different columns in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Cumulated-sum-of-rows-in-new-column-by-different-columns/m-p/232631#M46136</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = Current Data Table();


// List of column names for the table

col_names = dt &amp;lt;&amp;lt; get column names( string );


// Create cumulative sum of each D

looking_for_channel_1 = "D";

For( i = 1, i &amp;lt;= N Items( col_names ), i++,

// search column names for "D"

If( Starts With( col_names[i], looking_for_channel_1 ),

new_col = dt &amp;lt;&amp;lt; New Column( Char( col_names[i] ) || "&amp;gt;=",
Numeric,
.................................
);

);

);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Dear all,&lt;/P&gt;&lt;P&gt;I am a new scripter trying to solve a current issue. I am not very familiar with the syntax but I want to loop trough columns and rows to calculate the cumulated sum for each tool and row and save the result in a new column. I have attached a data table which shows the solution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks for any provided feedback.&lt;/P&gt;&lt;P&gt;BR&lt;/P&gt;&lt;P&gt;Martin&lt;/P&gt;</description>
    <pubDate>Thu, 07 Nov 2019 08:49:19 GMT</pubDate>
    <dc:creator>Martin91</dc:creator>
    <dc:date>2019-11-07T08:49:19Z</dc:date>
    <item>
      <title>Cumulated sum of rows in new column by different columns</title>
      <link>https://community.jmp.com/t5/Discussions/Cumulated-sum-of-rows-in-new-column-by-different-columns/m-p/232631#M46136</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = Current Data Table();


// List of column names for the table

col_names = dt &amp;lt;&amp;lt; get column names( string );


// Create cumulative sum of each D

looking_for_channel_1 = "D";

For( i = 1, i &amp;lt;= N Items( col_names ), i++,

// search column names for "D"

If( Starts With( col_names[i], looking_for_channel_1 ),

new_col = dt &amp;lt;&amp;lt; New Column( Char( col_names[i] ) || "&amp;gt;=",
Numeric,
.................................
);

);

);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Dear all,&lt;/P&gt;&lt;P&gt;I am a new scripter trying to solve a current issue. I am not very familiar with the syntax but I want to loop trough columns and rows to calculate the cumulated sum for each tool and row and save the result in a new column. I have attached a data table which shows the solution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks for any provided feedback.&lt;/P&gt;&lt;P&gt;BR&lt;/P&gt;&lt;P&gt;Martin&lt;/P&gt;</description>
      <pubDate>Thu, 07 Nov 2019 08:49:19 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Cumulated-sum-of-rows-in-new-column-by-different-columns/m-p/232631#M46136</guid>
      <dc:creator>Martin91</dc:creator>
      <dc:date>2019-11-07T08:49:19Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulated sum of rows in new column by different columns</title>
      <link>https://community.jmp.com/t5/Discussions/Cumulated-sum-of-rows-in-new-column-by-different-columns/m-p/232665#M46140</link>
      <description>&lt;P&gt;I have taken a different approach to solving your problem.&amp;nbsp; Looping through the data, accumulating the data as you propose will become quite slow with a lot of columna and rows.&amp;nbsp; Using builtin JMP functions will be much faster.&amp;nbsp; Below is a script that transposes the data into a tall data table to do the processing, using the builtin Col Cumulative Sum() function, and then transposes the data back to the wide format after processing is done.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;names default to here(1);
dt=current data table();

// Transform the data into a long table from the current wide table
// to allow for faster processing using the built in column function
//      col cumulative sum()
dtStack = dt &amp;lt;&amp;lt; Stack(
	columns( :D300, :D200, :D100, :D50, :D10 ),
	Source Label Column( "Label" ),
	Stacked Data Column( "Data" ),
	Name( "Non-stacked columns" )(Keep( :Tool, :Column 13 ))
);

// Add a new column to insure an alphbetical ordering of the original
// data table
dtStack &amp;lt;&amp;lt; New Column( "Row Number",
	Character,
	"Nominal",
	Formula(
		If( Lag( :Tool ) != :Tool,
			counter = 0
		);
		counter = counter + 1;
		result = Substr( "00", Length( Char( counter ) ) - 3 ) || Char( counter )
		 || " " || :Label;
	)
);

// Add the Cumulative Sum column
dtStack &amp;lt;&amp;lt; New Column( "xx",
	Numeric,
	"Continuous",
	Format( "Best", 12 ),
	Formula( Col Cumulative Sum( :Data, :Tool ) ),
	Set Selected
);

// Transpose the data back into a wide format 
dtFinal = dtStack &amp;lt;&amp;lt; Split(
	Split By( :Row Number ),
	Split( :Data, :Name( "xx" ) ),
	Group( :Tool ),
	Remaining Columns( Drop All ),
	Sort by Column Property
);

// Close the nolonger needed stack data table
close(dtStack,nosave);

// Pass across the column names adjusting them back to the desired form
For( i = 2, i &amp;lt;= N Cols( dtFinal ), i++,
	If(
		Word( 1, Column( dtFinal, i ) &amp;lt;&amp;lt; get name, " " ) == "Data",
			Column( dtFinal, i ) &amp;lt;&amp;lt; set name( Substr( Column( dtFinal, i ) &amp;lt;&amp;lt; get name, 10 ) )
	,
		Word( 1, Column( dtFinal, i ) &amp;lt;&amp;lt; get name, " " ) == "xx",
			Column( dtFinal, i ) &amp;lt;&amp;lt; set name( Substr( Column( dtFinal, i ) &amp;lt;&amp;lt; get name, 8 ) || "&amp;gt;=" )
	)
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Nov 2019 11:21:40 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Cumulated-sum-of-rows-in-new-column-by-different-columns/m-p/232665#M46140</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2019-11-07T11:21:40Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulated sum of rows in new column by different columns</title>
      <link>https://community.jmp.com/t5/Discussions/Cumulated-sum-of-rows-in-new-column-by-different-columns/m-p/232669#M46143</link>
      <description>&lt;P&gt;Here is another approach, just creating the new columns using formula columns&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );

// This JSL assume the table contains only the Tool column,
// followed by the Dnnn columns.  No cummulative columns have been
// created yet
dt = Current Data Table();

totCol = N Cols( dt );

// Loop across the columns creating the new cumulative sums
For( i = 2, i &amp;lt;= totCol, i++,
	Eval(
		Substitute(
				Expr(

					dt &amp;lt;&amp;lt; New Column( __newname__, formula( __formula__ ) )
				),
			Expr( __newname__ ), Column( dt, i ) &amp;lt;&amp;lt; get name || "&amp;gt;=",
			Expr( __formula__ ),
				If( i == 2,
					Parse( Column( dt, i ) &amp;lt;&amp;lt; get name ),
					Parse(
						"sum( :Name(\!"" || char( Column( dt, i - 1 ) &amp;lt;&amp;lt; get name ) || "&amp;gt;=\!"), " || char( Column( dt, i ) &amp;lt;&amp;lt; get name )
						 || ")"
					)
				)
		)
	)
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Nov 2019 11:48:19 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Cumulated-sum-of-rows-in-new-column-by-different-columns/m-p/232669#M46143</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2019-11-07T11:48:19Z</dc:date>
    </item>
  </channel>
</rss>

