<?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 JSL: How to copy the last term in a formula and add this value to the same formula in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/JSL-How-to-copy-the-last-term-in-a-formula-and-add-this-value-to/m-p/532772#M75560</link>
    <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm looking for a script which allows me to select first multiple formula columns and in each formula column it takes the last numeric term and add this value to the already existing formula.&lt;/P&gt;&lt;P&gt;For illustration purpose:&lt;/P&gt;&lt;P&gt;One formula column before running the script:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Marco__0-1660203906836.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/44732i1066DAF8FF5F5DD5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Marco__0-1660203906836.png" alt="Marco__0-1660203906836.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;and afterwards (simply the value 39,78... was copied and added):&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Marco__1-1660203955042.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/44733i58F73261A464FDE8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Marco__1-1660203955042.png" alt="Marco__1-1660203955042.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since I have hundred of different formula columns it would be great to have an automated script here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Looking forward for your help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;/Marco&lt;/P&gt;</description>
    <pubDate>Fri, 09 Jun 2023 17:07:24 GMT</pubDate>
    <dc:creator>Marco_</dc:creator>
    <dc:date>2023-06-09T17:07:24Z</dc:date>
    <item>
      <title>JSL: How to copy the last term in a formula and add this value to the same formula</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-How-to-copy-the-last-term-in-a-formula-and-add-this-value-to/m-p/532772#M75560</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm looking for a script which allows me to select first multiple formula columns and in each formula column it takes the last numeric term and add this value to the already existing formula.&lt;/P&gt;&lt;P&gt;For illustration purpose:&lt;/P&gt;&lt;P&gt;One formula column before running the script:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Marco__0-1660203906836.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/44732i1066DAF8FF5F5DD5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Marco__0-1660203906836.png" alt="Marco__0-1660203906836.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;and afterwards (simply the value 39,78... was copied and added):&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Marco__1-1660203955042.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/44733i58F73261A464FDE8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Marco__1-1660203955042.png" alt="Marco__1-1660203955042.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since I have hundred of different formula columns it would be great to have an automated script here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Looking forward for your help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;/Marco&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 17:07:24 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-How-to-copy-the-last-term-in-a-formula-and-add-this-value-to/m-p/532772#M75560</guid>
      <dc:creator>Marco_</dc:creator>
      <dc:date>2023-06-09T17:07:24Z</dc:date>
    </item>
    <item>
      <title>Re: JSL: How to copy the last term in a formula and add this value to the same formula</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-How-to-copy-the-last-term-in-a-formula-and-add-this-value-to/m-p/532911#M75571</link>
      <description>&lt;P&gt;Here are two ways to do this, by manipulating the formula as a string and then as an expression:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );

//Make some sample data
dt =New Table( "Untitled",
	Add Rows( 100 )
);

For Each( {i}, 1 :: 6,
	Eval(
		Eval Expr(
			dt &amp;lt;&amp;lt; New Column( "ToFix " || char(i),
				Expression,
				"None",
				Formula( Vec Quadratic( [1 3 5, 3 2 6, 5 6 1], [1 3 5, 2 4 6] ) * Expr( i ) )
			)
		)
	)
);

// Select some columns
dt &amp;lt;&amp;lt; Select Columns( (dt &amp;lt;&amp;lt; Get Column References)[4::5] );

//'fix' each selected column formula by manipulating the formula as a string
for each( {col, c}, dt &amp;lt;&amp;lt; Get Column References( dt &amp;lt;&amp;lt; Get Selected Columns ),
	if( Starts With( col &amp;lt;&amp;lt; get name(), "ToFix"),
		newf = regex(
			char(col &amp;lt;&amp;lt; Get Formula),
			"(.* \* (\d+))",
			"\1 + \2"
		);
		//show(Parse(newf));
		Eval( Eval Expr(
			col &amp;lt;&amp;lt; Set Formula( Expr( Parse(newf) ) )
		) );
	)
);

// Select some different columns
dt &amp;lt;&amp;lt; Select Columns( (dt &amp;lt;&amp;lt; Get Column References)[6::7] );

//'fix' each selected column formula by manipulating the formula as an expression
for each( {col, c}, dt &amp;lt;&amp;lt; Get Column References( dt &amp;lt;&amp;lt; Get Selected Columns ),
	if( Starts With( col &amp;lt;&amp;lt; get name(), "ToFix"),
		f = col &amp;lt;&amp;lt; Get Formula;
		v = Arg(col &amp;lt;&amp;lt; Get Formula, 2);
		//show(Name Expr(f));
		newf = Eval Expr( Add(Expr(Name Expr(f)), Expr(Name Expr(v))));
		show(newf);
		Eval( Eval Expr(
			col &amp;lt;&amp;lt; Set Formula( Expr( Name Expr(newf) ) )
		) );
	)
);
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Aug 2022 16:50:30 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-How-to-copy-the-last-term-in-a-formula-and-add-this-value-to/m-p/532911#M75571</guid>
      <dc:creator>ih</dc:creator>
      <dc:date>2022-08-11T16:50:30Z</dc:date>
    </item>
    <item>
      <title>Re: JSL: How to copy the last term in a formula and add this value to the same formula</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-How-to-copy-the-last-term-in-a-formula-and-add-this-value-to/m-p/534161#M75683</link>
      <description>&lt;P&gt;Thanks a lot for your solution&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/6657"&gt;@ih&lt;/a&gt;&amp;nbsp;!&lt;/P&gt;&lt;P&gt;I had to adapt the regexp slightly to fulfill my needs, but your post was definitely the key to find it!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For any other user, please find below the adapted code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;
// Select some columns
dt = currentdatatable();
dt &amp;lt;&amp;lt; Get Selected Columns;



//'fix' each selected column formula by manipulating the formula as a string
for each( {col, c}, dt &amp;lt;&amp;lt; Get Column References( dt &amp;lt;&amp;lt; Get Selected Columns ),
		newf = regex(
			char(col &amp;lt;&amp;lt; Get Formula),
			"(.* \* (\d+\.?\d*))",
			"\1 + \2"
		);
		//show(Parse(newf));
		Eval( Eval Expr(
			col &amp;lt;&amp;lt; Set Formula( Expr( Parse(newf) ) )
		) );
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Aug 2022 09:46:34 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-How-to-copy-the-last-term-in-a-formula-and-add-this-value-to/m-p/534161#M75683</guid>
      <dc:creator>Marco_</dc:creator>
      <dc:date>2022-08-16T09:46:34Z</dc:date>
    </item>
    <item>
      <title>Re: JSL: How to copy the last term in a formula and add this value to the same formula</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-How-to-copy-the-last-term-in-a-formula-and-add-this-value-to/m-p/534166#M75685</link>
      <description>&lt;P&gt;You might also be able to use Arg() depending on your formulas. Below if very simple example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

dt = New Table("Untitled 2",
	Add Rows(2),
	New Column("Column 1", Numeric, "Continuous", Format("Best", 12), Set Values([1, 2])),
	New Column("Column 2", Numeric, "Continuous", Format("Best", 12), Formula(:Column 1 * 2)),
	New Column("Column 3", Numeric, "Continuous", Format("Best", 12), Formula(:Column 2 * 3))
);

cur_col_idx = 3;

Show(column(dt, cur_col_idx) &amp;lt;&amp;lt; get formula);

Eval(Substitute(
			Expr(
				Column(dt, cur_col_idx) &amp;lt;&amp;lt; Set Formula(_old_f_ + _new_mult_)
			),
		Expr(_old_f_), Column(dt, cur_col_idx) &amp;lt;&amp;lt; Get Formula,
		Expr(_new_mult_), 
			old_f = Column(dt, cur_col_idx - 1) &amp;lt;&amp;lt; Get Formula; 
			Arg Expr(old_f, N Arg(old_f))
));

Show(column(dt, cur_col_idx) &amp;lt;&amp;lt; get formula);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Aug 2022 10:47:19 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-How-to-copy-the-last-term-in-a-formula-and-add-this-value-to/m-p/534166#M75685</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2022-08-16T10:47:19Z</dc:date>
    </item>
  </channel>
</rss>

