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
- :
- Determining if a column exists

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
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Feb 6, 2013 9:48 AM
(5593 views)

I have a JMP script that creates a new column with a formula in it. I'm trying to find out how to detect if the column already exists so that I can skip creating it, and avoid generating extra columns. I'm sure it's there, but I'm not finding it in the literature that I have.

Thanks.

Mike

1 ACCEPTED SOLUTION

Accepted Solutions

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

Feb 6, 2013 11:03 AM
(9024 views)

Solution

dt = open**(**"$sample_data\Big Class.jmp"**)**;

col_name_list = dt << get column names**(**string**)**;

new_column = "BMI";

// English BMI Formula

// BMI = ( Weight in Pounds / ( Height in inches x Height in inches ) ) x 703

if **(**!contains**(**col_name_list, new_column**)**,

dt <<New Column**(**"BMI", numeric, continuous,

formula**(****703** * :weight / :height / :height**)**

**)**;

**)**;

10 REPLIES

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

Feb 6, 2013 11:03 AM
(9025 views)

dt = open**(**"$sample_data\Big Class.jmp"**)**;

col_name_list = dt << get column names**(**string**)**;

new_column = "BMI";

// English BMI Formula

// BMI = ( Weight in Pounds / ( Height in inches x Height in inches ) ) x 703

if **(**!contains**(**col_name_list, new_column**)**,

dt <<New Column**(**"BMI", numeric, continuous,

formula**(****703** * :weight / :height / :height**)**

**)**;

**)**;

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

Feb 6, 2013 11:13 AM
(4514 views)

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

Feb 9, 2013 3:42 PM
(4514 views)

There is no single function that I am aware of. But *Is missing()* in combination with *Is Scriptable()* can do the trick.

The column also need to be enclosed by a Try() statement to avoid the script from stopping if the column would not exist.

```
dt = Open( "$sample_data\Big Class.jmp" );
new_column = "BMI";
If( Is Missing( Is Scriptable( Try( Column( new_column ) ) ) ),
dt << New Column( new_column, numeric, continuous, formula( 703 * :weight / :height / :height ) )
);
```

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

Feb 13, 2013 8:01 AM
(4514 views)

Also handy to know. I'm always glad to have another tool to add to the toolbox.

Thanks.

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

Apr 26, 2018 4:24 AM
(422 views)

Only your example worked for me, because the other solution is case sensitive. I had to use yours, since I could not be sure that the case of my string characters are correct.

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

Apr 26, 2018 12:57 PM
(395 views)

Here's a version of my logic that's case-insensitive.

```
dt = open("$sample_data\Big Class.jmp");
col_name_list = dt << get column names(string);
// Convert all column names to uppercase for case-insensitive search
for (i = 1, i <= nitems(col_name_list), i++,
col_name_list[i] = uppercase(col_name_list[i]);
);
new_column = "BMI";
// English BMI Formula BMI = ( Weight in Pounds / ( Height in inches^2 ) ) x 703
if (!contains(col_name_list, uppercase(new_column)),
dt <<New Column("BMI", numeric, continuous,
formula(703 * :weight / :height / :height)
);
);
```

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

Feb 9, 2013 2:43 PM
(4514 views)

I am not certain if this is true in all recent versions of JMP. I make this comment only to highlight an opportunity to improve the robustness of the code.

'Contains' returns the position of the item, which is fine to use in an 'if' statement, since zero will be interpreted as false and a positive value will be interpreted true. When preceding a contains with a not '!', the condition works when the item is not present or when present as the first item; however, when the item isn't first ( a value greater than 1 ), the not '!' can fail to convert the condition to false. This can be avoided by using a comparison '>0' with the 'contains'. See below for syntax.

```
dt = Open( "$sample_data\Big Class.jmp" );
col_name_list = dt << get column names( string );
new_column = "BMI";
// English BMI Formula
// BMI = ( Weight in Pounds / ( Height in inches x Height in inches ) ) x 703
If( !(Contains( col_name_list, new_column ) > 0),
dt << New Column( "BMI", numeric, continuous, formula( 703 * :weight / :height / :height ) )
);
```

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

Feb 13, 2013 7:58 AM
(4514 views)

That's very handy to know. I'll have to watch out for that.

Thanks

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

Feb 13, 2013 10:40 AM
(4514 views)

Hello wiebepo,

I tried different combinations and negating a positive integer, no matter how large, results in a 0. So my code will work (in JMP 9 and 10). Having said that I appreciate the heads up - this approach might not work in all languages. It certainly bears further testing.

```
b = 2000000000000;
!b;
```

The log shows:

**0**

Regards,

PMroz