<?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: Dynamically created formula not evaluating in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Dynamically-created-formula-not-evaluating/m-p/481654#M72592</link>
    <description>&lt;P&gt;try&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Expr(Col Median())&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;with the extra ().&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The parser sees the parens and sets the &lt;EM&gt;function&lt;/EM&gt; flag on the node in the expression tree. Without parens, the &lt;EM&gt;Col Median&lt;/EM&gt; node is just a name that happens to have children that won't be used ( except by arg() and char() conversion ). At run time the name is not in the name space of variables, and the name space of builtin functions won't be used, thus the message.&lt;/P&gt;
&lt;P&gt;I think this explanation is consistent with the behavior of all 5 tests.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Formula&lt;/EM&gt;(...) is different from ColMedian(...); ColMedian is a JSL function. &lt;EM&gt;Formula&lt;/EM&gt; is a keyword that the NewColumn message recognizes. The keyword handling expects children, so it might not need expr(Formula()), but would probably work just fine with it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;edit: I'm pretty sure the &lt;EM&gt;function&lt;/EM&gt; flag is not exposed in JSL via arg(), etc.&lt;/P&gt;</description>
    <pubDate>Sat, 23 Apr 2022 15:46:25 GMT</pubDate>
    <dc:creator>Craige_Hales</dc:creator>
    <dc:date>2022-04-23T15:46:25Z</dc:date>
    <item>
      <title>Dynamically created formula not evaluating</title>
      <link>https://community.jmp.com/t5/Discussions/Dynamically-created-formula-not-evaluating/m-p/481612#M72587</link>
      <description>&lt;P&gt;Came across a problem when I tried to dynamically create formula columns. I did manage to solve it in the end, but I would like to know what is happening here. My guess is that it is somehow related to how I'm building the formula and that the "formula" gets evaluated before it is set to column and then it somehow messes up the column formula.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an example: columns created by test1 and test2 do have issues and test3 works fine (test4 and test5 also seem to work fine, but I'm not sure I would feel confident in using them as I have no idea why they do get converted into column references from strings)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

test1 = function({dt, val_col, by_cols}, {Default Local},
	ref_list = Transform Each({col_name}, Insert(by_cols, val_col, 1),
		Name Expr(As Column(dt, col_name));
	);
	
	new_col = Eval(Substitute(
		Expr(dt &amp;lt;&amp;lt; New Column("Formula", Numeric, Continuous,
			Formula(_new_formula_)
		)),
		Expr(_new_formula_), Substitute(ref_list, Expr(list), Expr(Col Median))
	));
	return(new_col);
);

test2 = function({dt, val_col, by_cols}, {Default Local},
	ref_list = Transform Each({col_name}, Insert(by_cols, val_col, 1),
		Name Expr(As Column(dt, col_name));
	);
	new_formula = Substitute(ref_list, Expr(list), Expr(Col Median));		
	new_col = Eval(Substitute(
		Expr(dt &amp;lt;&amp;lt; New Column("Formula", Numeric, Continuous,
			Formula(_new_formula_)
		)),
		Expr(_new_formula_), Name Expr(new_formula)
	));
	return(new_col);
);

test3 = function({dt, val_col, by_cols}, {Default Local},
	ref_list = Transform Each({col_name}, Insert(by_cols, val_col, 1),
		Name Expr(As Column(dt, col_name));
	);
	
	new_formula = Char(Substitute(ref_list, Expr(list), Expr(Col Median)));
	new_col = Eval(Substitute(
		Expr(dt &amp;lt;&amp;lt; New Column("Formula", Numeric, Continuous,
			Formula(_new_formula_)
		)),
		Expr(_new_formula_), Parse(new_formula)
	));
	return(new_col);
);

test4 = function({dt, val_col, by_cols}, {Default Local},
	new_col = Eval(Substitute(
		Expr(dt &amp;lt;&amp;lt; New Column("Formula", Numeric, Continuous,
			Formula(Col Median(_new_formula_))
		)),
		Expr(_new_formula_), Insert(by_cols, val_col, 1)
	));
	return(new_col);
);

test5 = function({dt, val_col, by_cols}, {Default Local},
	new_col = Eval(Substitute(
		Expr(dt &amp;lt;&amp;lt; New Column("Formula", Numeric, Continuous,
			Formula(_new_formula_)
		)),
		Expr(_new_formula_), Substitute(Insert(by_cols, val_col, 1), Expr(List), Expr(Col Median))
	));
	return(new_col);
);

dt = Open("$SAMPLE_DATA/Big Class.jmp");
by_cols = {"age", "sex"};
val_col = "height";

col1 = test1(dt, val_col, by_cols);
col2 = test2(dt, val_col, by_cols);
col3 = test3(dt, val_col, by_cols);
col4 = test3(dt, val_col, by_cols);
col5 = test3(dt, val_col, by_cols);

Show(col1 &amp;lt;&amp;lt; get formula);
Show(col2 &amp;lt;&amp;lt; get formula);
Show(col3 &amp;lt;&amp;lt; get formula);
Show(col4 &amp;lt;&amp;lt; get formula);
Show(col5 &amp;lt;&amp;lt; get formula);

//dt1 = dt &amp;lt;&amp;lt; Subset(All Rows, Selected columns(0), Suppress formula evaluation(0));
//Close(dt, no save);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If I for example open Formula 2's formula editor and press apply I might get an error&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jthi_0-1650696518692.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/41989i6360F1DA3E493A9D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jthi_0-1650696518692.png" alt="jthi_0-1650696518692.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Is there any way to recover from the "broken" formulas and is there a possibility to see it during creation that it will break? I can use Parse(Char()) to get working formula and I can also create subset of the data and the formulas will work there.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Tested with JMP Pro 16.2.0 on Windows 10.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 16:57:35 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Dynamically-created-formula-not-evaluating/m-p/481612#M72587</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2023-06-09T16:57:35Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically created formula not evaluating</title>
      <link>https://community.jmp.com/t5/Discussions/Dynamically-created-formula-not-evaluating/m-p/481654#M72592</link>
      <description>&lt;P&gt;try&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Expr(Col Median())&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;with the extra ().&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The parser sees the parens and sets the &lt;EM&gt;function&lt;/EM&gt; flag on the node in the expression tree. Without parens, the &lt;EM&gt;Col Median&lt;/EM&gt; node is just a name that happens to have children that won't be used ( except by arg() and char() conversion ). At run time the name is not in the name space of variables, and the name space of builtin functions won't be used, thus the message.&lt;/P&gt;
&lt;P&gt;I think this explanation is consistent with the behavior of all 5 tests.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Formula&lt;/EM&gt;(...) is different from ColMedian(...); ColMedian is a JSL function. &lt;EM&gt;Formula&lt;/EM&gt; is a keyword that the NewColumn message recognizes. The keyword handling expects children, so it might not need expr(Formula()), but would probably work just fine with it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;edit: I'm pretty sure the &lt;EM&gt;function&lt;/EM&gt; flag is not exposed in JSL via arg(), etc.&lt;/P&gt;</description>
      <pubDate>Sat, 23 Apr 2022 15:46:25 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Dynamically-created-formula-not-evaluating/m-p/481654#M72592</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2022-04-23T15:46:25Z</dc:date>
    </item>
  </channel>
</rss>

