<?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: Column reference in custom function in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Column-reference-in-custom-function/m-p/53325#M30186</link>
    <description>&lt;P&gt;Thanks for&amp;nbsp;your reply. I realized afterwards there are some other problems like x cannot be local, but this can be fixed.&lt;/P&gt;&lt;P&gt;But I couldn't figure out how to pass a column reference in the function call. I had a similar problem some time ago and solved it passing column names as strings. As in the example below that works:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;names default to here(1);
Add Custom Functions(
	{New Custom Function(
		"NNE",
		"N Categories",
		Function( {Categories, ByColumn},{Default local},
			If( Row() == 1,
				Summarize( here:x = by( column(ByColumn), column(Categories) ) );
			);
			N Items( Loc( here:x[1], column(ByColumn)[row()] ) );
		),
		&amp;lt;&amp;lt;Description( "Determine number of unique categories of a column by/within each By Column" ),
		&amp;lt;&amp;lt;Prototype( "N Categories( :Categories, :By column )" ),
		&amp;lt;&amp;lt;Formula Category( "NNE" ),
		&amp;lt;&amp;lt;Scripting Index Category( "" ),
		&amp;lt;&amp;lt;Result Type( {"Any"} ),
		&amp;lt;&amp;lt;Parameter( {"String"}, "Category Column name" ),
		&amp;lt;&amp;lt;Parameter( {"String"}, "By Column name" ),
		&amp;lt;&amp;lt;Example(expr(dt = Open( "$SAMPLE_DATA/Air Traffic.jmp" );	
dt&amp;lt;&amp;lt;new column("Different Airlines at Airport",formula(NNE:N Categories("Airline","Airport")));

));
	)}
);

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;It&amp;nbsp;can be called like this in a column formula:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 441px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/9896i5FFB0A32D275C7CC/image-dimensions/441x66?v=v2" width="441" height="66" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;But it requires strings instead of column references. I can rephrase my question: Is it somehow possible to change the above so that the N Categories function accepts column references instead of strings?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 17 Mar 2018 22:39:17 GMT</pubDate>
    <dc:creator>thomasz</dc:creator>
    <dc:date>2018-03-17T22:39:17Z</dc:date>
    <item>
      <title>Column reference in custom function</title>
      <link>https://community.jmp.com/t5/Discussions/Column-reference-in-custom-function/m-p/53291#M30171</link>
      <description>&lt;P&gt;Having this table:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;New Table( "test table",
	Add Rows( 9 ),
	New Column( "By column",
		Character( 1 ),
		"Nominal",
		Set Selected,
		Set Values( {"a", "a", "a", "b", "b", "b", "c", "c", "c"} )
	),
	New Column( "Categories",
		Character( 1 ),
		"Nominal",
		Set Values( {"t", "t", "r", "y", "t", "r", "r", "r", "r"} )
	),
	New Column( "N Categories",
		Expression,
		"None",
		Formula(
			If( Row() == 1,
				Summarize( x = by( :By column, :Categories ) ),
				Empty()
			);
			N Items( Loc( x[1], :By column ) );
		),
		Set Display Width( 109 )
	),
	Set Row States( [1, 0, 0, 0, 0, 0, 0, 0, 0] )
)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I would like to create a custom function&amp;nbsp;doing the same thing as the formula in the N Categories column.&lt;/P&gt;&lt;P&gt;Converting it into a custom function script in the formula editor gives me something like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Add Custom Functions(
	{New Custom Function(
		"custom",
		"N Categories",
		Function( {Categories,By column},
			{Default Local},
			If( Row() == 1,
				Summarize( x = by( By column, Categories ) ),
				Empty()
			);
			N Items( Loc( x[1], By column ) );
		),
		&amp;lt;&amp;lt;Description( "" ),
		&amp;lt;&amp;lt;Prototype( "" ),
		&amp;lt;&amp;lt;Formula Category( "" ),
		&amp;lt;&amp;lt;Scripting Index Category( "" ),
		&amp;lt;&amp;lt;Result Type( {"Any"} ),
		&amp;lt;&amp;lt;Parameter( {"Any"}, "" ),
		&amp;lt;&amp;lt;Parameter( {"Any"}, "" )
	)}
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But now the parameters, prior being column references just become parameters containing the values of the rows.&lt;/P&gt;&lt;P&gt;So my question is:&amp;nbsp;Is it possible to create a custom function that treats the parameters as column references?&lt;/P&gt;&lt;P&gt;I could make it work by treating the parameters as column names and transfer strings instead, but it would not be the same!&lt;/P&gt;</description>
      <pubDate>Fri, 16 Mar 2018 19:57:46 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Column-reference-in-custom-function/m-p/53291#M30171</guid>
      <dc:creator>thomasz</dc:creator>
      <dc:date>2018-03-16T19:57:46Z</dc:date>
    </item>
    <item>
      <title>Re: Column reference in custom function</title>
      <link>https://community.jmp.com/t5/Discussions/Column-reference-in-custom-function/m-p/53323#M30185</link>
      <description>&lt;P&gt;1. &lt;STRONG&gt;For all of the Community Members that are confused and looking for "Add Custom Functions()", this is a feature that is being added to JMP 14.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;2.&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/3341"&gt;@thomasz&lt;/a&gt;, I don't think you can expect the function you are building to be identical to the formula.&amp;nbsp; Your formula is not a generic formula, it is a hard coded formula, unlike what you are attempting to do with the function.&amp;nbsp; So there are naturally going to have to be differences. I think you need to think in terms of what would the formula look like, if it was a generic function, and then develop the formula as such.&amp;nbsp; See below for a similar generic formula that I put together.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="custom function.PNG" style="width: 999px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/9895iCDE1ED20DDE67A36/image-size/large?v=v2&amp;amp;px=999" role="button" title="custom function.PNG" alt="custom function.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Mar 2018 11:08:45 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Column-reference-in-custom-function/m-p/53323#M30185</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2018-03-17T11:08:45Z</dc:date>
    </item>
    <item>
      <title>Re: Column reference in custom function</title>
      <link>https://community.jmp.com/t5/Discussions/Column-reference-in-custom-function/m-p/53325#M30186</link>
      <description>&lt;P&gt;Thanks for&amp;nbsp;your reply. I realized afterwards there are some other problems like x cannot be local, but this can be fixed.&lt;/P&gt;&lt;P&gt;But I couldn't figure out how to pass a column reference in the function call. I had a similar problem some time ago and solved it passing column names as strings. As in the example below that works:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;names default to here(1);
Add Custom Functions(
	{New Custom Function(
		"NNE",
		"N Categories",
		Function( {Categories, ByColumn},{Default local},
			If( Row() == 1,
				Summarize( here:x = by( column(ByColumn), column(Categories) ) );
			);
			N Items( Loc( here:x[1], column(ByColumn)[row()] ) );
		),
		&amp;lt;&amp;lt;Description( "Determine number of unique categories of a column by/within each By Column" ),
		&amp;lt;&amp;lt;Prototype( "N Categories( :Categories, :By column )" ),
		&amp;lt;&amp;lt;Formula Category( "NNE" ),
		&amp;lt;&amp;lt;Scripting Index Category( "" ),
		&amp;lt;&amp;lt;Result Type( {"Any"} ),
		&amp;lt;&amp;lt;Parameter( {"String"}, "Category Column name" ),
		&amp;lt;&amp;lt;Parameter( {"String"}, "By Column name" ),
		&amp;lt;&amp;lt;Example(expr(dt = Open( "$SAMPLE_DATA/Air Traffic.jmp" );	
dt&amp;lt;&amp;lt;new column("Different Airlines at Airport",formula(NNE:N Categories("Airline","Airport")));

));
	)}
);

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;It&amp;nbsp;can be called like this in a column formula:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 441px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/9896i5FFB0A32D275C7CC/image-dimensions/441x66?v=v2" width="441" height="66" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;But it requires strings instead of column references. I can rephrase my question: Is it somehow possible to change the above so that the N Categories function accepts column references instead of strings?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Mar 2018 22:39:17 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Column-reference-in-custom-function/m-p/53325#M30186</guid>
      <dc:creator>thomasz</dc:creator>
      <dc:date>2018-03-17T22:39:17Z</dc:date>
    </item>
    <item>
      <title>Re: Column reference in custom function</title>
      <link>https://community.jmp.com/t5/Discussions/Column-reference-in-custom-function/m-p/265085#M51645</link>
      <description>&lt;P&gt;I'd like to re-pose this question, as&amp;nbsp;I too have struggled with this issue for -- it seems like forever.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone have a reliable way of writing a custom function (user function) such that it accepts a column reference (e.g. :Column 1) as a parameter?&lt;/P&gt;&lt;P&gt;My workaround has always been to pass the column name as a string and then, inside the function, use Column() or As Column(), depending on circumstances, to generate the reference.&lt;/P&gt;&lt;P&gt;But if I want to write custom functions that work cleanly in column formulas for naïve users who aren't expecting this workaround, it would be nice to be able to pass column reference instead or column name (just as one does, or so it seems, for native functions).&lt;/P&gt;&lt;P&gt;John&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2020 17:39:42 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Column-reference-in-custom-function/m-p/265085#M51645</guid>
      <dc:creator>john_madden</dc:creator>
      <dc:date>2020-05-07T17:39:42Z</dc:date>
    </item>
    <item>
      <title>Re: Column reference in custom function</title>
      <link>https://community.jmp.com/t5/Discussions/Column-reference-in-custom-function/m-p/265212#M51668</link>
      <description>&lt;P&gt;I'll just say, the best I have so far been able to come up with -- and it isn't really what I want -- is to enclose the column parameter in a Name Expr() instruction. For example, given a data table with a column named ":Column 1" this works:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Current Data Table();
Add Custom Functions(
	New Custom Function(
		"x",
		"Testf",
		Function( {col},
			{i},
			For Each Row( dt, Print( col ) );
		)
	)
);
x:Testf(Name Expr(dt:Column 1));&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;but you can't do this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = Current Data Table();
Add Custom Functions(
	New Custom Function(
		"x",
		"Testf",
		Function( {Name Expr(col)}, // JSL interpreter gives error for this syntax
			{i},
			For Each Row( dt, Print( col ) );
		)
	)
);
x:Testf(dt:Column 1); // hypothetical, as the function definition is in error&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 May 2020 13:32:10 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Column-reference-in-custom-function/m-p/265212#M51668</guid>
      <dc:creator>john_madden</dc:creator>
      <dc:date>2020-05-08T13:32:10Z</dc:date>
    </item>
    <item>
      <title>Re: Column reference in custom function</title>
      <link>https://community.jmp.com/t5/Discussions/Column-reference-in-custom-function/m-p/694524#M87961</link>
      <description>&lt;P&gt;Seems that I got stuck with the same question:&lt;BR /&gt;&lt;LI-MESSAGE title="Custom Function - how to reference the column" uid="691701" url="https://community.jmp.com/t5/Discussions/Custom-Function-how-to-reference-the-column/m-p/691701#U691701" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-forum-thread lia-fa-icon lia-fa-forum lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;... even the same application case.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Concerning count unique, there is a wish list entry waiting for Kudos:&lt;BR /&gt;&lt;LI-MESSAGE title="new JSL function: Col N Categories" uid="592610" url="https://community.jmp.com/t5/JMP-Wish-List/new-JSL-function-Col-N-Categories/m-p/592610#U592610" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-idea-thread lia-fa-icon lia-fa-idea lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Concerning the issue with &lt;EM&gt;&lt;STRONG&gt;referencing a column&lt;/STRONG&gt;&lt;/EM&gt;, there is a wish - actually related, but different subject:&lt;BR /&gt;&lt;LI-MESSAGE title="Option or function to evaluate a custom column formula at once, or cache values between rows" uid="513082" url="https://community.jmp.com/t5/JMP-Wish-List/Option-or-function-to-evaluate-a-custom-column-formula-at-once/m-p/513082#U513082" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-idea-thread lia-fa-icon lia-fa-idea lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;&lt;BR /&gt;... but nevertheless, it mentions the issue with referencing a column in the last sentence:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="hogi_0-1699219690695.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/58342i64ECA7A315165553/image-size/medium?v=v2&amp;amp;px=400" role="button" title="hogi_0-1699219690695.png" alt="hogi_0-1699219690695.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 05 Nov 2023 21:29:03 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Column-reference-in-custom-function/m-p/694524#M87961</guid>
      <dc:creator>hogi</dc:creator>
      <dc:date>2023-11-05T21:29:03Z</dc:date>
    </item>
    <item>
      <title>Re: Column reference in custom function</title>
      <link>https://community.jmp.com/t5/Discussions/Column-reference-in-custom-function/m-p/694933#M88010</link>
      <description>&lt;P&gt;Concerning Plan B,&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;referencing a column, &lt;/STRONG&gt;&lt;/EM&gt;I just got this&amp;nbsp;reply from Jmp Support TS-00063463&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="hogi_0-1699302254919.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/58390iBD1694454FADDA19/image-size/medium?v=v2&amp;amp;px=400" role="button" title="hogi_0-1699302254919.png" alt="hogi_0-1699302254919.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 20:25:33 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Column-reference-in-custom-function/m-p/694933#M88010</guid>
      <dc:creator>hogi</dc:creator>
      <dc:date>2023-11-06T20:25:33Z</dc:date>
    </item>
    <item>
      <title>Re: Column reference in custom function</title>
      <link>https://community.jmp.com/t5/Discussions/Column-reference-in-custom-function/m-p/695100#M88024</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/681"&gt;@john_madden&lt;/a&gt;&amp;nbsp;wrote:&lt;P&gt;But if I want to write custom functions that work cleanly in column formulas for naïve users who aren't expecting this workaround&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Besides &lt;EM&gt;naive users&lt;/EM&gt;, it's also Jmp itself: "transform functions" where the available workaround solutions fail.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Nov 2023 07:21:10 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Column-reference-in-custom-function/m-p/695100#M88024</guid>
      <dc:creator>hogi</dc:creator>
      <dc:date>2023-11-07T07:21:10Z</dc:date>
    </item>
  </channel>
</rss>

