<?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 to create formula column with variable column names in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/JSL-to-create-formula-column-with-variable-column-names/m-p/709609#M89355</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I need to create a new formula column via script using variables for column names and constant.&lt;/P&gt;&lt;P&gt;Tried various combinations of Expr, Eval, Parse, Eval Insert for no avail.&lt;/P&gt;&lt;P&gt;See sample.&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;row = 11;
col = "foo";

for (i = 1, i &amp;lt;= ncols(dt), i++,
dt &amp;lt;&amp;lt; New Column( "ratio " || char(i), numeric,
formula(( column(col) - column(col)[row] ) / (Col Max(col) - Col Min(col))));
);

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 15 Dec 2023 12:35:31 GMT</pubDate>
    <dc:creator>RonB</dc:creator>
    <dc:date>2023-12-15T12:35:31Z</dc:date>
    <item>
      <title>JSL to create formula column with variable column names</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-to-create-formula-column-with-variable-column-names/m-p/709609#M89355</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I need to create a new formula column via script using variables for column names and constant.&lt;/P&gt;&lt;P&gt;Tried various combinations of Expr, Eval, Parse, Eval Insert for no avail.&lt;/P&gt;&lt;P&gt;See sample.&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;row = 11;
col = "foo";

for (i = 1, i &amp;lt;= ncols(dt), i++,
dt &amp;lt;&amp;lt; New Column( "ratio " || char(i), numeric,
formula(( column(col) - column(col)[row] ) / (Col Max(col) - Col Min(col))));
);

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Dec 2023 12:35:31 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-to-create-formula-column-with-variable-column-names/m-p/709609#M89355</guid>
      <dc:creator>RonB</dc:creator>
      <dc:date>2023-12-15T12:35:31Z</dc:date>
    </item>
    <item>
      <title>Re: JSL to create formula column with variable column names</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-to-create-formula-column-with-variable-column-names/m-p/709625#M89356</link>
      <description>&lt;P&gt;Using&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;for (i = 1, i &amp;lt;= ncols(dt), i++,&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;when you are adding a new column within the For() loop results in the loop never coming to an end.&amp;nbsp; The ncols(dt) increases by 1 with each loop, and therefore i will never reach ncols(dt);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The formula() element of a New Column() function does not parse it's contents as it is created for the column.&amp;nbsp; It only parses it's contents when the formula is run for each row.&amp;nbsp; Therefore, the col and row references can cause issues when those values are changed.&amp;nbsp; The proper way to handle this, is to substitute into the formula, the actual values for col and row, before the formula is created.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = 
// Open Data Table: Big Class.jmp
// → Data Table( "Big Class" )
Open( "$SAMPLE_DATA/Big Class.jmp" );

row = 11;
col = "height";
stop = n cols(dt);

For( i=1, i&amp;lt;= stop, i++,
	Eval(
		Substitute(
				Expr(
					dt &amp;lt;&amp;lt; New Column( "ratio " || Char( i ),
						numeric,
						formula( (As Column( _col_ ) - As Column( _col_ )[_row_]) / (Col Max( As Column( _col_ ) ) - Col Min( As Column( _col_ ) )) )
					)
				),
			Expr( _col_ ), col,
			Expr( _row_ ), row
		)
	);
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Dec 2023 13:20:35 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-to-create-formula-column-with-variable-column-names/m-p/709625#M89356</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2023-12-15T13:20:35Z</dc:date>
    </item>
    <item>
      <title>Re: JSL to create formula column with variable column names</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-to-create-formula-column-with-variable-column-names/m-p/709914#M89377</link>
      <description>&lt;P&gt;Many thanks!&lt;/P&gt;</description>
      <pubDate>Sun, 17 Dec 2023 08:18:04 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-to-create-formula-column-with-variable-column-names/m-p/709914#M89377</guid>
      <dc:creator>RonB</dc:creator>
      <dc:date>2023-12-17T08:18:04Z</dc:date>
    </item>
  </channel>
</rss>

