<?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: Set column formula dynamically with JSL in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Set-column-formula-dynamically-with-JSL/m-p/847991#M102304</link>
    <description>&lt;P&gt;Not 100% sure what you are trying to do but this might give some ideas&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1); 

dt = open("$SAMPLE_DATA/Big Class.jmp");
dt &amp;lt;&amp;lt; new column("LL_outliers", Numeric, Continuous, Set Each Value(50));
dt &amp;lt;&amp;lt; new column("UL_outliers", Numeric, Continuous, Set Each Value(70));

all_cols = dt &amp;lt;&amp;lt; Get Column Names("Continuous", "String");

colname = all_cols[1];
f_expr = Substitute(
	Expr(If(_col_ &amp;gt; :UL_outliers | _col_ &amp;lt; :LL_outliers,
		1,
		0
	)),
	Expr(_col_), Name Expr(AsColumn(dt, colname));
);

new_col = dt &amp;lt;&amp;lt; New Column("F", Numeric, Nominal);
Eval(EvalExpr(
	new_col &amp;lt;&amp;lt; Set Formula(Expr(NameExpr(f_expr)));
));&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 15 Mar 2025 07:56:25 GMT</pubDate>
    <dc:creator>jthi</dc:creator>
    <dc:date>2025-03-15T07:56:25Z</dc:date>
    <item>
      <title>Set column formula dynamically with JSL</title>
      <link>https://community.jmp.com/t5/Discussions/Set-column-formula-dynamically-with-JSL/m-p/847453#M102226</link>
      <description>&lt;P&gt;I'm working on a JSL script where I need to dynamically build a formula for a given column inside a loop. The idea is to reference a different column for each iteration, so that the formula uses the evaluated column name (e.g., &lt;CODE&gt;:col_name&lt;/CODE&gt;) rather than inserting the literal code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's a simplified version of what I'm trying to achieve in the loop (I have tried many variations of Expr, Eval, Parse in this code to no avail):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;all_cols = dt &amp;lt;&amp;lt; Get Column Names();
col = Column(all_cols[N Items(all_cols)-1]); // Changes for every i in the loop

// Dynamically build the formula expression using column names directly
formula = If(
    col &amp;gt; :UL_outliers | col &amp;lt; :LL_outliers,
    1,
    0
);

outlier_col &amp;lt;&amp;lt; Set Formula(formula);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The problem is that when I set the formula using &lt;CODE&gt;Set Formula()&lt;/CODE&gt;, instead of dynamically evaluating the column reference, the formula is inserted literally into the column's formula editor. As a result, the column formula is not evaluated and is simply:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;If(
    col &amp;gt; :UL_outliers | col &amp;lt; :LL_outliers,
    1,
    0
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I have read &lt;A href="https://community.jmp.com/t5/Discussions/Dynamically-created-formula-not-evaluating/m-p/481612#M72587" target="_self"&gt;Dynamically-created-formula-not-evaluating&lt;/A&gt;&amp;nbsp;and some other related posts, but my mind is melting. I just want some 1's or 0's in my outlier column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help or workaround would be greatly appreciated. Thanks in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Mar 2025 08:16:26 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Set-column-formula-dynamically-with-JSL/m-p/847453#M102226</guid>
      <dc:creator>CSkjodt</dc:creator>
      <dc:date>2025-03-12T08:16:26Z</dc:date>
    </item>
    <item>
      <title>Re: Set column formula dynamically with JSL</title>
      <link>https://community.jmp.com/t5/Discussions/Set-column-formula-dynamically-with-JSL/m-p/847991#M102304</link>
      <description>&lt;P&gt;Not 100% sure what you are trying to do but this might give some ideas&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1); 

dt = open("$SAMPLE_DATA/Big Class.jmp");
dt &amp;lt;&amp;lt; new column("LL_outliers", Numeric, Continuous, Set Each Value(50));
dt &amp;lt;&amp;lt; new column("UL_outliers", Numeric, Continuous, Set Each Value(70));

all_cols = dt &amp;lt;&amp;lt; Get Column Names("Continuous", "String");

colname = all_cols[1];
f_expr = Substitute(
	Expr(If(_col_ &amp;gt; :UL_outliers | _col_ &amp;lt; :LL_outliers,
		1,
		0
	)),
	Expr(_col_), Name Expr(AsColumn(dt, colname));
);

new_col = dt &amp;lt;&amp;lt; New Column("F", Numeric, Nominal);
Eval(EvalExpr(
	new_col &amp;lt;&amp;lt; Set Formula(Expr(NameExpr(f_expr)));
));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 15 Mar 2025 07:56:25 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Set-column-formula-dynamically-with-JSL/m-p/847991#M102304</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2025-03-15T07:56:25Z</dc:date>
    </item>
    <item>
      <title>Re: Set column formula dynamically with JSL</title>
      <link>https://community.jmp.com/t5/Discussions/Set-column-formula-dynamically-with-JSL/m-p/848002#M102308</link>
      <description>&lt;P&gt;JMP has a string data type:&lt;/P&gt;
&lt;P&gt;a = "cat";&lt;/P&gt;
&lt;P&gt;b = "3 + 5";&lt;/P&gt;
&lt;P&gt;a and b are strings. B looks like an expression, but is not.&lt;/P&gt;
&lt;P&gt;c = parse( b );&lt;/P&gt;
&lt;P&gt;c is an expression, which can be shown as a tree:&lt;/P&gt;
&lt;PRE&gt;   operator(+)
     /      \
   3         5&lt;/PRE&gt;
&lt;P&gt;c = expr( 3 + 5 ); // is another way to get the same expression stored in c&lt;/P&gt;
&lt;P&gt;d = eval( c ); // or just d = c; the right hand side is evaluated anyway&lt;/P&gt;
&lt;P&gt;d is 8.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;Sometimes JMP will automatically evaluate c to 8, sometimes not. Sometimes you want 8, sometimes you want 3+5.&lt;/EM&gt; &lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;e = nameexpr(c);&lt;/P&gt;
&lt;P&gt;e gets the expression that c holds; think of nameexpr as a thin wrapper; when the wrapper is evaluated it blocks a deeper evaluation and returns the 3+5 expression, unevaluated, rather than 8.&lt;/P&gt;
&lt;P&gt;JMP also has a set of functions for manipulating an expression tree, not covered here. &lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/14366"&gt;@jthi&lt;/a&gt;&amp;nbsp; used substitute, for example.&lt;/P&gt;
&lt;P&gt;I think part of what you are puzzling over is the difference between the way different functions in JSL will handle an expression argument:&lt;/P&gt;
&lt;P&gt;cos(c) , like most functions, will evaluate c to 8. This is what you expect.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;colname&amp;lt;&amp;lt;setformula( c ) &lt;EM&gt;literally&lt;/EM&gt; copies the expression (c, not 3+5, not 8 ) 8)&lt;/img&gt; to the column formula without evaluating it. (Just as it would for&amp;nbsp; &amp;lt;&amp;lt;setformula(col1 + col2), which is what you want. Not like the cos() function.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So setformula has two &lt;EM&gt;&lt;STRONG&gt;special&lt;/STRONG&gt;&lt;/EM&gt; cases:&lt;/P&gt;
&lt;P&gt;colname&amp;lt;&amp;lt;setformula( c )&amp;nbsp;&amp;nbsp;&amp;nbsp; --&amp;gt; c&amp;nbsp; (not special)&lt;/P&gt;
&lt;P&gt;colname&amp;lt;&amp;lt;setformula( eval(c) )&amp;nbsp; --&amp;gt; 8&amp;nbsp;&amp;nbsp; (eval wrapper)&lt;/P&gt;
&lt;P&gt;colname&amp;lt;&amp;lt;setformula( nameexpr(c) )&amp;nbsp;&amp;nbsp; --&amp;gt; 3+5&amp;nbsp;&amp;nbsp; (nameexpr wrapper)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that these are indeed special cases; setformula makes an extra effort to detect the eval or nameexpr wrapper to make the JSL do what you expect.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now that I'm nearly done I wish I'd used x+y rather than 3+8 for part of this; it would be more clear why you might want to postpone the evaluation to a later point in time. Also, even a single number like 8 is an expression; eval(8) is 8.&lt;/P&gt;</description>
      <pubDate>Sun, 16 Mar 2025 04:19:23 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Set-column-formula-dynamically-with-JSL/m-p/848002#M102308</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2025-03-16T04:19:23Z</dc:date>
    </item>
    <item>
      <title>Re: Set column formula dynamically with JSL</title>
      <link>https://community.jmp.com/t5/Discussions/Set-column-formula-dynamically-with-JSL/m-p/848173#M102336</link>
      <description>&lt;P&gt;Thank you for the mindbending but very educational explanation. I think I understand it at least conceptually.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Guess I'll find out.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Thanks a lot ! :)&lt;/img&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Mar 2025 14:49:36 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Set-column-formula-dynamically-with-JSL/m-p/848173#M102336</guid>
      <dc:creator>CSkjodt</dc:creator>
      <dc:date>2025-03-17T14:49:36Z</dc:date>
    </item>
  </channel>
</rss>

