<?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: how do I multiply each row values backward in one column until a criteria is met in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/how-do-I-multiply-each-row-values-backward-in-one-column-until-a/m-p/334200#M58198</link>
    <description>&lt;P&gt;Hi Jim &lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/2687"&gt;@txnelson&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;No, my column names do not change. They stay as col1, col2 as given.&amp;nbsp;I would like to add a&amp;nbsp;new column (col3) that does the backward&amp;nbsp;multiplication&amp;nbsp;for a same value given&amp;nbsp;in col2. I'm&amp;nbsp;demonstrating my need in&amp;nbsp;the following table.&amp;nbsp;Could you please help?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;col1 col2 col3&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;x&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;6 (i.e. 1 x 2 x 3)&lt;/P&gt;&lt;P&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 6 (i.e. 2 x 3)&lt;/P&gt;&lt;P&gt;3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;x&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3 (i.e. 3)&lt;/P&gt;&lt;P&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; y&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 240 (i.e. 2x4x5x6)&lt;/P&gt;&lt;P&gt;4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; y&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 120 (i.e. 4 x 5 x6)&lt;/P&gt;&lt;P&gt;5&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; y&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 30 (i.e. 5 x6)&lt;/P&gt;&lt;P&gt;6&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; y&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 6 (i.e. 6)&lt;/P&gt;&lt;P&gt;7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; z&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 504 (i.e. 7x8x9)&lt;/P&gt;&lt;P&gt;8&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; z&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 72 (i.e. 8x9)&lt;/P&gt;&lt;P&gt;9&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; z&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 9&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 16 Nov 2020 20:18:10 GMT</pubDate>
    <dc:creator>Tinghui</dc:creator>
    <dc:date>2020-11-16T20:18:10Z</dc:date>
    <item>
      <title>how do I multiply each row values backward in one column until a criteria is met</title>
      <link>https://community.jmp.com/t5/Discussions/how-do-I-multiply-each-row-values-backward-in-one-column-until-a/m-p/334101#M58194</link>
      <description>&lt;P&gt;Hello, I have the following challenge that I have trouble scripting with.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My starting data table looks like below. I would like to&amp;nbsp;multiply Xi&amp;nbsp;backwards starting from col2 =0 as the criteria&amp;nbsp;and put the new values in a new column.&lt;/P&gt;&lt;P&gt;col1 col2&lt;/P&gt;&lt;P&gt;X1&amp;nbsp; 0&lt;/P&gt;&lt;P&gt;X2&amp;nbsp; y2&lt;/P&gt;&lt;P&gt;X3&amp;nbsp; y3&lt;/P&gt;&lt;P&gt;…&lt;/P&gt;&lt;P&gt;Xn&amp;nbsp; yn&lt;/P&gt;&lt;P&gt;XX1 0&lt;/P&gt;&lt;P&gt;XX2 yy2&lt;/P&gt;&lt;P&gt;….&lt;/P&gt;&lt;P&gt;XXn yyn&lt;/P&gt;&lt;P&gt;….&lt;/P&gt;&lt;P&gt;….&lt;/P&gt;&lt;P&gt;My end data table would look like this:&lt;/P&gt;&lt;P&gt;col1 col2 col3&lt;/P&gt;&lt;P&gt;X1&amp;nbsp; 0&amp;nbsp; Xn*Xn-1*……X3*X2*X1&lt;/P&gt;&lt;P&gt;X2&amp;nbsp; y2 Xn*Xn-1*…..X3*X2&lt;/P&gt;&lt;P&gt;X3&amp;nbsp; y3 Xn*Xn-1*….*X3&lt;/P&gt;&lt;P&gt;…&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Xn&amp;nbsp; yn&amp;nbsp; Xn&lt;/P&gt;&lt;P&gt;XX1 0&amp;nbsp;&amp;nbsp; XXn*XXn-1*…..XX2*XX1&lt;/P&gt;&lt;P&gt;XX2 yy2 XXn*XXn-1*…..XX2&lt;/P&gt;&lt;P&gt;….&lt;/P&gt;&lt;P&gt;XXn yyn XXn&lt;/P&gt;&lt;P&gt;….&lt;/P&gt;&lt;P&gt;….&lt;/P&gt;&lt;P&gt;Can someone please help out? Very much appreciated!&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 22:22:25 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/how-do-I-multiply-each-row-values-backward-in-one-column-until-a/m-p/334101#M58194</guid>
      <dc:creator>Tinghui</dc:creator>
      <dc:date>2023-06-09T22:22:25Z</dc:date>
    </item>
    <item>
      <title>Re: how do I multiply each row values backward in one column until a criteria is met</title>
      <link>https://community.jmp.com/t5/Discussions/how-do-I-multiply-each-row-values-backward-in-one-column-until-a/m-p/334180#M58196</link>
      <description>&lt;P&gt;I don't understand your 3 sets of columns.&amp;nbsp; It appears as if you are changing column names in the middle of a data table, which will not work in JMP.&amp;nbsp; Is this table format coming from an Excel spreadsheet?&lt;/P&gt;
&lt;P&gt;Here is a simple formula that will do what you want for a data table that looks like&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="simple.PNG" style="width: 357px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/28398i8B6932B79AA0C5B1/image-size/large?v=v2&amp;amp;px=999" role="button" title="simple.PNG" alt="simple.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I have attached the data table.&lt;/P&gt;
&lt;P&gt;The formula used is:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;x = :col2[Row()];
If( Row() &amp;lt; N Rows( Current Data Table() ),
	For( i = Row() + 1, i &amp;lt;= N Rows( Current Data Table() ), i++,
		x = x * :col2[i]
	)
);
x;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This same idea can be used if there are multiple places in a data table that need to restart the multiplying.&amp;nbsp; The identification of when to restart and when to end would just have to be worked out&lt;/P&gt;</description>
      <pubDate>Mon, 16 Nov 2020 19:31:10 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/how-do-I-multiply-each-row-values-backward-in-one-column-until-a/m-p/334180#M58196</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2020-11-16T19:31:10Z</dc:date>
    </item>
    <item>
      <title>ARe: how do I multiply each row values backward in one column until a criteria is met</title>
      <link>https://community.jmp.com/t5/Discussions/how-do-I-multiply-each-row-values-backward-in-one-column-until-a/m-p/334185#M58197</link>
      <description>&lt;P&gt;I removed my post since I missed the grouping variable. My approach would not work in this scenario.&lt;/P&gt;</description>
      <pubDate>Mon, 16 Nov 2020 21:57:51 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/how-do-I-multiply-each-row-values-backward-in-one-column-until-a/m-p/334185#M58197</guid>
      <dc:creator>Dan_Obermiller</dc:creator>
      <dc:date>2020-11-16T21:57:51Z</dc:date>
    </item>
    <item>
      <title>Re: how do I multiply each row values backward in one column until a criteria is met</title>
      <link>https://community.jmp.com/t5/Discussions/how-do-I-multiply-each-row-values-backward-in-one-column-until-a/m-p/334200#M58198</link>
      <description>&lt;P&gt;Hi Jim &lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/2687"&gt;@txnelson&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;No, my column names do not change. They stay as col1, col2 as given.&amp;nbsp;I would like to add a&amp;nbsp;new column (col3) that does the backward&amp;nbsp;multiplication&amp;nbsp;for a same value given&amp;nbsp;in col2. I'm&amp;nbsp;demonstrating my need in&amp;nbsp;the following table.&amp;nbsp;Could you please help?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;col1 col2 col3&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;x&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;6 (i.e. 1 x 2 x 3)&lt;/P&gt;&lt;P&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 6 (i.e. 2 x 3)&lt;/P&gt;&lt;P&gt;3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;x&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3 (i.e. 3)&lt;/P&gt;&lt;P&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; y&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 240 (i.e. 2x4x5x6)&lt;/P&gt;&lt;P&gt;4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; y&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 120 (i.e. 4 x 5 x6)&lt;/P&gt;&lt;P&gt;5&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; y&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 30 (i.e. 5 x6)&lt;/P&gt;&lt;P&gt;6&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; y&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 6 (i.e. 6)&lt;/P&gt;&lt;P&gt;7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; z&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 504 (i.e. 7x8x9)&lt;/P&gt;&lt;P&gt;8&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; z&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 72 (i.e. 8x9)&lt;/P&gt;&lt;P&gt;9&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; z&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 9&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Nov 2020 20:18:10 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/how-do-I-multiply-each-row-values-backward-in-one-column-until-a/m-p/334200#M58198</guid>
      <dc:creator>Tinghui</dc:creator>
      <dc:date>2020-11-16T20:18:10Z</dc:date>
    </item>
    <item>
      <title>Re: how do I multiply each row values backward in one column until a criteria is met</title>
      <link>https://community.jmp.com/t5/Discussions/how-do-I-multiply-each-row-values-backward-in-one-column-until-a/m-p/334218#M58200</link>
      <description>&lt;P&gt;Here is the new formula......it is the same method as the last formula, but just having to find the start and end point for each of the groups.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;If( Row() == 1,
	curCol = :col2;
	end = Max( Current Data Table() &amp;lt;&amp;lt; get rows where( :col2 == curCol ) );
	x = :col1;
,
	:col2 != curCol;
	curCol = :col2;
	end = Max( Current Data Table() &amp;lt;&amp;lt; get rows where( :col2 == curCol ) );
	x = :col1;
);
For( i = Row() + 1, i &amp;lt;= end, i++,
	x = x * :col1[i]
);
x;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="startend.PNG" style="width: 371px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/28401iEBC6C0D4E1B5271F/image-size/large?v=v2&amp;amp;px=999" role="button" title="startend.PNG" alt="startend.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Nov 2020 21:13:44 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/how-do-I-multiply-each-row-values-backward-in-one-column-until-a/m-p/334218#M58200</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2020-11-16T21:13:44Z</dc:date>
    </item>
    <item>
      <title>Re: how do I multiply each row values backward in one column until a criteria is met</title>
      <link>https://community.jmp.com/t5/Discussions/how-do-I-multiply-each-row-values-backward-in-one-column-until-a/m-p/334486#M58223</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BLOCKQUOTE&gt;Hi Jim &lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/2687"&gt;@txnelson&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;That actually worked on a smaller table (i.e. several thousands of rows). Thank you.&lt;BR /&gt;&lt;BR /&gt;The 'new' issue that I'm having is that my data table is very large. As I embedded the formula into the new column and click on 'apply', the program got hung and I had to kill jmp to get out.&lt;BR /&gt;&lt;BR /&gt;As I hover over the formula, here is the warning message regarding 'current data table'.&lt;BR /&gt;&lt;BR /&gt;"Current data table() has side effects and is usually inappropriate to be used inside a formula. If this is necessary, you might use expr or eval to prevent execution in formulas....."&lt;BR /&gt;&lt;BR /&gt;My scripting skills are very limited. Thanks for your further help on this subject!&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;&lt;BR /&gt;Tinghui&lt;BR /&gt;&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BLOCKQUOTE&gt;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/2687"&gt;@txnelson&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;Here is the new formula......it is the same method as the last formula, but just having to find the start and end point for each of the groups.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;If( Row() == 1,
	curCol = :col2;
	end = Max( Current Data Table() &amp;lt;&amp;lt; get rows where( :col2 == curCol ) );
	x = :col1;
,
	:col2 != curCol;
	curCol = :col2;
	end = Max( Current Data Table() &amp;lt;&amp;lt; get rows where( :col2 == curCol ) );
	x = :col1;
);
For( i = Row() + 1, i &amp;lt;= end, i++,
	x = x * :col1[i]
);
x;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Nov 2020 14:42:30 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/how-do-I-multiply-each-row-values-backward-in-one-column-until-a/m-p/334486#M58223</guid>
      <dc:creator>Tinghui</dc:creator>
      <dc:date>2020-11-17T14:42:30Z</dc:date>
    </item>
    <item>
      <title>Re: how do I multiply each row values backward in one column until a criteria is met</title>
      <link>https://community.jmp.com/t5/Discussions/how-do-I-multiply-each-row-values-backward-in-one-column-until-a/m-p/334635#M58231</link>
      <description>&lt;P&gt;The calculations you want are very easy to do if the data are reversed, so here is a little script that will calculate the values using that idea&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;names default to here(1);

// create the sample table
dt = New Table( "example",
	Add Rows( 10 ),
	New Column( "col1",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [1, 2, 3, 2, 4, 5, 6, 7, 8, 9] ),
		Set Display Width( 48 )
	),
	New Column( "col2",
		Character( 1 ),
		"Nominal",
		Set Values( {"x", "x", "x", "y", "y", "y", "y", "z", "z", "z"} ),
		Set Display Width( 45 )
	)
);

// JSL to create the column you want
// create a new column to preserve the order of the data
dt &amp;lt;&amp;lt; new column("origRow", formula(row()));
dt:origRow &amp;lt;&amp;lt; delete formula;

// Reverse the order of the data
dt &amp;lt;&amp;lt; sort(by(:origRow), order(descending), replace table(1));

// create the new column
dt &amp;lt;&amp;lt; new column( "col3", formula(
	If( Row() == 1, x = :col1 );
If( Lag( :col2 ) != :col2,
	x = :col1,
	x = x * :col1
);
x;
));

// Since the formula is dependent on the order of the data, the formula needs
// to be removed and the values set to static values
dt:col3 &amp;lt;&amp;lt; delete formula;

// reorder the data table back to the original order and delete the origRow column
dt &amp;lt;&amp;lt; sort(by(:origRow), order(ascending), replace table(1));
dt &amp;lt;&amp;lt; delete columns(:origRow);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And here is a piece of JSL that will create the new column without sorting&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );

// create the sample table
dt = New Table( "example",
	Add Rows( 10 ),
	New Column( "col1",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [1, 2, 3, 2, 4, 5, 6, 7, 8, 9] ),
		Set Display Width( 48 )
	),
	New Column( "col2",
		Character( 1 ),
		"Nominal",
		Set Values( {"x", "x", "x", "y", "y", "y", "y", "z", "z", "z"} ),
		Set Display Width( 45 )
	)
);


// create the new column
dt &amp;lt;&amp;lt; New Column( "col3b" );

:col3b[N Rows( dt )] = :col1[N Rows( dt )];
For( i = N Rows( dt ) - 1, i &amp;gt;= 1, i--,
	If( :col2[i + 1] != :col2[i],
		:col3b[i] = :col1[i],
		:col3b[i] = :col3b[i + 1] * :col1[i]
	)
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Nov 2020 16:29:56 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/how-do-I-multiply-each-row-values-backward-in-one-column-until-a/m-p/334635#M58231</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2020-11-17T16:29:56Z</dc:date>
    </item>
    <item>
      <title>Re: how do I multiply each row values backward in one column until a criteria is met</title>
      <link>https://community.jmp.com/t5/Discussions/how-do-I-multiply-each-row-values-backward-in-one-column-until-a/m-p/334649#M58235</link>
      <description>&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/2687"&gt;@txnelson&lt;/a&gt;&lt;BR /&gt;&lt;BR /&gt;Thanks Jim! I really wish I were a scripting genius like yourself. It works great!</description>
      <pubDate>Tue, 17 Nov 2020 16:43:52 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/how-do-I-multiply-each-row-values-backward-in-one-column-until-a/m-p/334649#M58235</guid>
      <dc:creator>Tinghui</dc:creator>
      <dc:date>2020-11-17T16:43:52Z</dc:date>
    </item>
  </channel>
</rss>

