<?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 How to catch formula error in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/How-to-catch-formula-error/m-p/538148#M75936</link>
    <description>&lt;P&gt;I would catch errors when a formula contains missing column. In the following example, a table contains 2 columns. Adding a new column that uses a non existing column, the result is missing result (correctly) and an error that block my script that I am not able to catch because try statement doesn't work. Is there a workaround for this?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt= New Table( "Test",
	Add Rows( 1 ),
	New Column( "A",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [2] )
	),
	New Column( "B",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Selected,
		Set Values( [1] )
	)
);

formula=":Name(\!"C\!") - :Name(\!"B\!")";
try(
	eval(parse("dt &amp;lt;&amp;lt; new column(\!"value\!", Numeric, \!"Continuous\!", Formula(" ||formula || "))"));
,
	error="missing column";
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 09 Jun 2023 17:08:34 GMT</pubDate>
    <dc:creator>antonio-domenic</dc:creator>
    <dc:date>2023-06-09T17:08:34Z</dc:date>
    <item>
      <title>How to catch formula error</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-catch-formula-error/m-p/538148#M75936</link>
      <description>&lt;P&gt;I would catch errors when a formula contains missing column. In the following example, a table contains 2 columns. Adding a new column that uses a non existing column, the result is missing result (correctly) and an error that block my script that I am not able to catch because try statement doesn't work. Is there a workaround for this?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt= New Table( "Test",
	Add Rows( 1 ),
	New Column( "A",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [2] )
	),
	New Column( "B",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Selected,
		Set Values( [1] )
	)
);

formula=":Name(\!"C\!") - :Name(\!"B\!")";
try(
	eval(parse("dt &amp;lt;&amp;lt; new column(\!"value\!", Numeric, \!"Continuous\!", Formula(" ||formula || "))"));
,
	error="missing column";
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 17:08:34 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-catch-formula-error/m-p/538148#M75936</guid>
      <dc:creator>antonio-domenic</dc:creator>
      <dc:date>2023-06-09T17:08:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to catch formula error</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-catch-formula-error/m-p/538175#M75938</link>
      <description>&lt;P&gt;I am not exactly sure what you want the final outcome to be, if the column is missing, but the below is one way to handle the issue&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = New Table( "Test",
	Add Rows( 1 ),
	New Column( "A",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [2] )
	),
	New Column( "B",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Selected,
		Set Values( [1] )
	)
);

If( Try( dt:c &amp;lt;&amp;lt; get name, "" ) == "",
	type = "character";
	formula = "\!"missing column\!"";
,
	type = "numeric/continuous";
	formula = ":Name(\!"C\!") - :Name(\!"B\!")";
);


Eval(
	Parse(
		"dt &amp;lt;&amp;lt; new column(\!"value\!"," || type || ", Formula(" || formula
		 || "))"
	)
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Aug 2022 09:47:44 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-catch-formula-error/m-p/538175#M75938</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2022-08-29T09:47:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to catch formula error</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-catch-formula-error/m-p/538192#M75941</link>
      <description>&lt;P&gt;Thank you, but it is not so useful. The problem is that I extract automatically some data (by script) and any of them can be missing (but I don't know in advance if and which). The script stops when missing column is found and this is the real problem. A good solution could be the try statement, but it doesn't work.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Aug 2022 11:15:18 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-catch-formula-error/m-p/538192#M75941</guid>
      <dc:creator>antonio-domenic</dc:creator>
      <dc:date>2022-08-29T11:15:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to catch formula error</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-catch-formula-error/m-p/538255#M75946</link>
      <description>&lt;P&gt;This might be better to test before you make the formula column. So do an if statement to see if the column exists...if it doesn't, don't make the formula column.&lt;/P&gt;</description>
      <pubDate>Mon, 29 Aug 2022 13:43:12 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-catch-formula-error/m-p/538255#M75946</guid>
      <dc:creator>pauldeen</dc:creator>
      <dc:date>2022-08-29T13:43:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to catch formula error</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-catch-formula-error/m-p/538299#M75947</link>
      <description>&lt;P&gt;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/3008"&gt;@antonio-domenic&lt;/a&gt;&amp;nbsp;, I believe that what you're wanting is to determine, before attempting to create a column based on a formula, whether the table contains the appropriate columns for the given formula?&amp;nbsp; If you're able to determine this before attempting to create the column you'll have less likelihood of running into script-stopping errors.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The good news is that it's pretty simple to determine all of the external names a formula relies on -- which should be only column names of the current data table.&amp;nbsp; (It's best practice to not have a formula rely on a currently in-scope variable that will lose scope at some point in the future, causing the formula to error out).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's some example code with a function that recursively parses expressions (formulas are of course just expressions) for any dangling names.&amp;nbsp; You can then check the column names of the table and apply if the column names satisfy the formula:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default to Here( 1 );

dt= New Table( "Test",
	Add Rows( 1 ),
	New Column( "A",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [2] )
	),
	New Column( "B",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Selected,
		Set Values( [1] )
	)
);

//recursively find all unassigned names within the formula -- we can test these against the column names
get dangling names = Function( {formula},
	{Default Local},
	parse formula = Function( {expr, names = {}, assigns = {}},
		{Default Local},
		If( Contains( Head Name( expr ), "[" ) &amp;amp; Contains( Head Name( expr ), "]" ), // deal with the exceptional case where JMP doesn't arg-parse out this pattern...
			items = Words( Head Name( expr ), "[" );
			Summation( i = 1, N Items( items ),
				Try(
					item = Word( 1, items[i], "]" );
					If( Is Missing( Num( item ) ),
						{n, a} = Recurse( Parse( item ) );
						Insert Into( names, n );
						Insert Into( assigns, a );
					)
				);&lt;BR /&gt;				0
			)
		,
			If( Head Name( expr ) == "Assign", // if a variable is assigned within the formula then we don't care to check it (the formula shouldn't be direclty affecting other columns, it bad practice!)
				Insert Into( assigns, Char( Arg( expr, 1 ) ) )
			,
				Head Name( expr ) != Char( Name Expr( expr ) ), // Need to recursively parse here
				Summation( i = 1, N Arg( expr ),
					{n, a} = Recurse( Arg( expr, i ) );
					Insert Into( names, n );
					Insert Into( assigns, a );
					0
				);
			,
				Insert Into( names, Char( Name Expr( expr ) ) ) // found a name
			);
		);
		Eval List( {Associative Array( names ) &amp;lt;&amp;lt; Get Keys, Associative Array( assigns ) &amp;lt;&amp;lt; Get Keys} )
	);

	{names, assigns} = parse formula( Name Expr( formula ) );
	names = Associative Array( names );
	assigns = Associative Array( assigns );
	names &amp;lt;&amp;lt; Remove( assigns );
	names = names &amp;lt;&amp;lt; Get Keys;
	names
);

formulas = {};
Insert Into( formulas, Expr( :Name("C") - :Name("B") ) );
local var = [1 =&amp;gt; 3, 2 =&amp;gt; 4];
Insert Into( formulas, Eval Expr( 
		var = Expr( local var );
		var[:C][:F] + :D
	);
);
Insert Into( formulas, Expr( :A + :B ) );
Insert Into( formulas, Expr( :A + :B - Sin( :D&lt;/img&gt; + Cos( :E - :Name("flamingo@fish" ) ) ) ) );

For( i = 1, i &amp;lt;= N Items( formulas ), i++,
	table columns = dt &amp;lt;&amp;lt; Get Column Names( "String" );
	Write( "\!N" );
	Show( table columns );
	formula references = get dangling names( formulas[i] );
	refs = {};
	For( j = 1, j &amp;lt;= N Items( formula references ), j++,
		refs[j] = Contains( table columns, formula references[j] ) &amp;gt; 0
	);
	Show( formulas[i] );
	Show( formula references );
	Print( If( Sum( refs ) == N Items( formula references ), "OKAY", "NOT OKAY" ) );
	If( Sum( refs ) == N Items( formula references ), // The table contains all the necessary columns, okay to add the new column!
		Eval( Eval Expr(
			dt &amp;lt;&amp;lt; New Column( "FORMULA", &amp;lt;&amp;lt;Formula( Expr( formulas[i] ) ) )
		) )
	)
)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Let me know if you have any questions!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Jordan,&lt;/P&gt;</description>
      <pubDate>Tue, 30 Aug 2022 19:45:15 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-catch-formula-error/m-p/538299#M75947</guid>
      <dc:creator>ErraticAttack</dc:creator>
      <dc:date>2022-08-30T19:45:15Z</dc:date>
    </item>
  </channel>
</rss>

