<?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: How to pass columns as arguments for a function in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/How-to-pass-columns-as-arguments-for-a-function/m-p/762451#M94175</link>
    <description>&lt;P&gt;Thanks a lot, exactly what I needed. Also the elseif is so much better! Thanks for sharing&lt;/P&gt;</description>
    <pubDate>Tue, 04 Jun 2024 10:57:38 GMT</pubDate>
    <dc:creator>Agustin</dc:creator>
    <dc:date>2024-06-04T10:57:38Z</dc:date>
    <item>
      <title>How to pass columns as arguments for a function</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-pass-columns-as-arguments-for-a-function/m-p/762449#M94173</link>
      <description>&lt;P&gt;I'm trying to create a function that creates a new column and sets a formula:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;my_function = Function( {name, col1, col2},
	dt &amp;lt;&amp;lt; New Column( name, Character);
	Column(dt,name) &amp;lt;&amp;lt; Set Formula(
	If(
		:col1 == "Positive" &amp;amp; :col2 ==
		"TRUE",
		"True Positive",
		If(
			:col1 == "Negative" &amp;amp; :col2== "TRUE",
			"False Positive",
			If(
				:col1 == "Negative" &amp;amp; :col2 == "FALSE",
				"True Negative",
				If(
					:col1 == "Positive" &amp;amp; :col2 == "FALSE",
					"False Negative",
					"?"
				)
			)
		)
	)
);
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But when I look at the formula created it passes on col1 rather than the string behind it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Calling the function as:&lt;/P&gt;&lt;P&gt;my_function("New Column", Column("column_1"), Column("column_2"));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jun 2024 10:07:20 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-pass-columns-as-arguments-for-a-function/m-p/762449#M94173</guid>
      <dc:creator>Agustin</dc:creator>
      <dc:date>2024-06-04T10:07:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to pass columns as arguments for a function</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-pass-columns-as-arguments-for-a-function/m-p/762450#M94174</link>
      <description>&lt;P&gt;I always pass in columns with names (and table reference). And if you need formula use&amp;nbsp;&lt;LI-MESSAGE title="Insert one expression into another using Eval Insert, Eval Expr, Parse, and Substitute" uid="48998" url="https://community.jmp.com/t5/JSL-Cookbook-Archived/Insert-one-expression-into-another-using-Eval-Insert-Eval-Expr/m-p/48998#U48998" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-tkb-thread lia-fa-icon lia-fa-tkb lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;methods to build it (I prefer Eval(EvalExpr()) but in your case I would use Eval(Substitute()). Here is one option how you could handle this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Big Class.jmp");


my_function = Function({dt, name, col1, col2},
	new_col = Eval(Substitute(
		Expr(dt &amp;lt;&amp;lt; New Column(name, Character, Formula(
			If(_col1_ == "Positive" &amp;amp; _col2_ == "TRUE",
				"True Positive",
				If(_col1_ == "Negative" &amp;amp; _col2_== "TRUE",
					"False Positive",
					If(_col1_ == "Negative" &amp;amp; _col2_ == "FALSE",
						"True Negative",
						If(_col1_== "Positive" &amp;amp; _col2_ == "FALSE",
								"False Negative",
								"?"
						)
					)
				)
			)
		))),
		Expr(_col1_), Name Expr(AsColumn(dt, col1)),
		Expr(_col2_), Name Expr(AsColumn(dt, col2))
	));
	
	return(new_col)
);

col = my_function(dt, "test", "name", "sex");&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also JMP does have option to use elseif (even though it isn't written) and it might work in your case instead if nesting multiple if-statements&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Big Class.jmp");


my_function = Function({dt, name, col1, col2},
	new_col = Eval(Substitute(
		Expr(dt &amp;lt;&amp;lt; New Column(name, Character, Formula(
			If(_col1_ == "Positive" &amp;amp; _col2_ == "TRUE",
				"True Positive",
			_col1_ == "Negative" &amp;amp; _col2_== "TRUE",
				"False Positive",
			_col1_ == "Negative" &amp;amp; _col2_ == "FALSE",
				"True Negative",
			_col1_== "Positive" &amp;amp; _col2_ == "FALSE",
				"False Negative"
			,
				"?"
			),
		))),
		Expr(_col1_), Name Expr(AsColumn(dt, col1)),
		Expr(_col2_), Name Expr(AsColumn(dt, col2))
	));
	
	return(new_col)
);

col = my_function(dt, "test", "name", "sex");&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;</description>
      <pubDate>Tue, 04 Jun 2024 10:30:08 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-pass-columns-as-arguments-for-a-function/m-p/762450#M94174</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2024-06-04T10:30:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to pass columns as arguments for a function</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-pass-columns-as-arguments-for-a-function/m-p/762451#M94175</link>
      <description>&lt;P&gt;Thanks a lot, exactly what I needed. Also the elseif is so much better! Thanks for sharing&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jun 2024 10:57:38 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-pass-columns-as-arguments-for-a-function/m-p/762451#M94175</guid>
      <dc:creator>Agustin</dc:creator>
      <dc:date>2024-06-04T10:57:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to pass columns as arguments for a function</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-pass-columns-as-arguments-for-a-function/m-p/762452#M94176</link>
      <description>&lt;P&gt;If you prefer to use column (aka :age) instead of Strings ("age") to reference the columns,&amp;nbsp;there are 2 problems to be solved:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;a manually generated function evaluates it's arguments.&lt;BR /&gt;So if you use a column as a function argument, Jmp will evaluate it&amp;nbsp; (i.e. return the value of the current row *)&lt;BR /&gt;→&lt;U&gt; to prevent it:&lt;/U&gt;&lt;BR /&gt;&lt;STRONG&gt;wrap the column with &lt;FONT face="courier new,courier"&gt;Name Expr()&lt;BR /&gt;&lt;BR /&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/LI&gt;&lt;LI&gt;In most of the cases Jmp doesn't replace variables (like&amp;nbsp; "col1") with their value (the respective column),&lt;BR /&gt;e.g. in new column() or &amp;lt;&amp;lt; set formula&lt;BR /&gt;→&amp;nbsp; &lt;U&gt;to force Jmp to do so:&lt;/U&gt;&lt;BR /&gt;use &lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;Eval(Eval Expr( .... Expr(&lt;/STRONG&gt;xyz&lt;STRONG&gt;)&lt;/STRONG&gt; ...&lt;STRONG&gt;)&lt;/STRONG&gt;&lt;/FONT&gt; to evalute xyz before evaluating the remaining part.&lt;BR /&gt;&lt;BR /&gt;2b) to prevent Jmp from &lt;EM&gt;over-evaluating&lt;/EM&gt; the column reference (like in *), use &lt;FONT face="courier new,courier"&gt;Name Expr()&lt;/FONT&gt;again&lt;FONT face="courier new,courier"&gt;,&lt;BR /&gt;&lt;/FONT&gt;so, &lt;U&gt;in combination&lt;/U&gt;, use&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;Expr(Name Expr(colX))&lt;BR /&gt;&lt;/FONT&gt;... wherever you want to use the column&amp;nbsp;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in total:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Big Class.jmp");


my_function = Function({name, col1, col2},
	new_col = Eval(Eval Expr(New Column(name, Character, Formula(Expr(Name Expr(col1)) /* #2b */  - Expr(Name Expr(col2))/* #2b */ )))); // #2
);

col = my_function("diff", Name Expr(:height), Name Expr(:weight));&amp;nbsp;//#1&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jun 2024 17:46:40 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-pass-columns-as-arguments-for-a-function/m-p/762452#M94176</guid>
      <dc:creator>hogi</dc:creator>
      <dc:date>2024-06-04T17:46:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to pass columns as arguments for a function</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-pass-columns-as-arguments-for-a-function/m-p/762455#M94177</link>
      <description>&lt;P&gt;Here is one way to handle this&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="txnelson_0-1717499322110.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/64850i5DB44FE2DBA6EB14/image-size/medium?v=v2&amp;amp;px=400" role="button" title="txnelson_0-1717499322110.png" alt="txnelson_0-1717499322110.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = New Table( "Example",
	Add Rows( 5 ),
	New Column( "a",
		Character,
		"Nominal",
		Set Values( {"Positive", "Positive", "Negative", "Negative", "unknown"} )
	),
	New Column( "b",
		Character,
		"Nominal",
		Set Values( {"TRUE", "FALSE", "TRUE", "FALSE", ""} )
	)
);

my_function = Function( {name, col1, col2},
	dt &amp;lt;&amp;lt; New Column( name, Character );
	Eval(
		Eval Expr(
			Column( dt, name ) &amp;lt;&amp;lt; Set Formula(
				If(
					as Column( Expr( col1 ) ) == "Positive" &amp;amp; as Column( Expr( col2 ) )
					 == "TRUE", "True Positive",
					as Column( Expr( col1 ) ) == "Negative" &amp;amp; as Column( Expr( col2 ) )
					 == "TRUE", "False Positive",
					as Column( Expr( col1 ) ) == "Negative" &amp;amp; as Column( Expr( col2 ) )
					 == "FALSE", "True Negative",
					as Column( Expr( col1 ) ) == "Positive" &amp;amp; as Column( Expr( col2 ) )
					 == "FALSE", "False Negative",
					"?"
				)
			)
		)
	);
);

my_function( "new column", "a", "b" );&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Jun 2024 11:09:03 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-pass-columns-as-arguments-for-a-function/m-p/762455#M94177</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2024-06-04T11:09:03Z</dc:date>
    </item>
  </channel>
</rss>

