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
- :
- Using a list of columns for input to Col-functions

Topic Options

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

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

Sep 2, 2014 10:37 PM
(5524 views)

I am trying to create a script to generate new columns based on user input. The input comes from a dialog box, and is in the form of a list of column names, e.g.:

Cols={:Data,:By 1,:By 2};

I want top create a new column "n" with a formula containing the Col Number formula:

New Column("n",formula(Col Number(:Data,:By 1,:By 2)));

However I am stuck at getting the formula:

Col Number({:Data,:By 1,:By 2})

that is *with* curly brackets. No error is returned, but the formula won't evaluate.

How do I use the list of columns as input for a Col-function?

The number of columns must be allowed to vary.

BR

Jesper

Jesper

1 ACCEPTED SOLUTION

Accepted Solutions

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

Alternatively, instead of building an Expression as above, the list content can be incorporated into a parseable string. In this example, the curly brackets are removed using substitute.

Cols = **{**:Data, :By 1, :By 2**}**;

s = "New Column( \!"n\!", formula( Col Number(" || Substitute**(** Char**(** Cols **)**, "{", "", "}", "" **)** || ") ) )";

Eval**(** Parse**(** s **)** **)**;

The string approach is straightforward and do usually work, but code can be frustrating to debug if the strings grow large.

3 REPLIES 3

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

Re: Using a list of columns for input to Col-functions

Here is one way to do it:

Cols = **{**:Data, :By 1, :By 2**}**;

f = Expr**(** Col Number**()** **)**;

For**(** i = **1**, i <= N Items**(** Cols **)**, i++,

Insert Into**(** f, Cols**[**i**]** **)**

**)**;

New Column**(** "n", formula**(** Name Expr**(** f **)** **)** **)**;

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

Alternatively, instead of building an Expression as above, the list content can be incorporated into a parseable string. In this example, the curly brackets are removed using substitute.

Cols = **{**:Data, :By 1, :By 2**}**;

s = "New Column( \!"n\!", formula( Col Number(" || Substitute**(** Char**(** Cols **)**, "{", "", "}", "" **)** || ") ) )";

Eval**(** Parse**(** s **)** **)**;

The string approach is straightforward and do usually work, but code can be frustrating to debug if the strings grow large.

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

Re: Using a list of columns for input to Col-functions

Thank you very much Your first suggestion is the more elegant one and solved my original problem, but your second suggestion solved a second problem of mine, I didn't yet pose.

BR

Jesper

Jesper