Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- JMP User Community
- :
- Discussions
- :
- Column reference in custom function

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Column reference in custom function

Mar 16, 2018 9:35 AM
(1407 views)

Having this table:

```
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] )
)
```

I would like to create a custom function doing the same thing as the formula in the N Categories column.

Converting it into a custom function script in the formula editor gives me something like this:

```
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 ) );
),
<<Description( "" ),
<<Prototype( "" ),
<<Formula Category( "" ),
<<Scripting Index Category( "" ),
<<Result Type( {"Any"} ),
<<Parameter( {"Any"}, "" ),
<<Parameter( {"Any"}, "" )
)}
);
```

But now the parameters, prior being column references just become parameters containing the values of the rows.

So my question is: Is it possible to create a custom function that treats the parameters as column references?

I could make it work by treating the parameters as column names and transfer strings instead, but it would not be the same!

2 REPLIES 2

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: Column reference in custom function

1. **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.**

2. @thomasz, I don't think you can expect the function you are building to be identical to the formula. Your formula is not a generic formula, it is a hard coded formula, unlike what you are attempting to do with the function. 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. See below for a similar generic formula that I put together.

Jim

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: Column reference in custom function

Thanks for your reply. I realized afterwards there are some other problems like x cannot be local, but this can be fixed.

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:

```
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()] ) );
),
<<Description( "Determine number of unique categories of a column by/within each By Column" ),
<<Prototype( "N Categories( :Categories, :By column )" ),
<<Formula Category( "NNE" ),
<<Scripting Index Category( "" ),
<<Result Type( {"Any"} ),
<<Parameter( {"String"}, "Category Column name" ),
<<Parameter( {"String"}, "By Column name" ),
<<Example(expr(dt = Open( "$SAMPLE_DATA/Air Traffic.jmp" );
dt<<new column("Different Airlines at Airport",formula(NNE:N Categories("Airline","Airport")));
));
)}
);
```

It can be called like this in a column formula:

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?