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
- :
- Create and populates new columns based categories in an existing column

- 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

May 18, 2020 1:01 PM
(540 views)

Hello,

I'm trying to build a quick query in which a categorical column (test_article) is used to create a series of column which contains only a subset of the information. If the script works as intended, the table would end up with 2 new columns (PBS vs ...).

test_article | measurement | PBS vs 709547 | PBS vs 709139 |

PBS | 1.23 | PBS | PBS |

PBS | 1.456 | PBS | PBS |

709547 | 4.56 | 709547 | EXCLUDE |

709139 | 5.02 | EXCLUDE | 709139 |

709139 | 6.02 | EXCLUDE | 709139 |

709547 | 4.57 | 709547 | EXCLUDE |

I've tried the following script:

```
NamesDefaultToHere(1);
dt=current data table();
row = Associative Array( :test_article ) << Get Keys;
nc=N items (row);
For( i = 1, i <= nc, i++,
new column("PBS vs "|| row[i],"Character", "Nominal", Formula(If( :test_article == "PBS", :test_article,
If( :test_article == row[i],
"test",
"EXCLUDE"
)
))));
```

Somehow, it does not populate any values in the new columns with the exception of 'PBS'.

Any suggestions?

Thanks a lot

Sebastien

2 ACCEPTED SOLUTIONS

Accepted Solutions

Highlighted

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

The primary issue you are having is that within the definition of a formula, one can not have items that need to be evaluated. JMP thinks everything within the formula definition is to be evaluated only when the formula is executed.

Therefore, the formula that is passed into the New Column definition, must be complete in it's syntax. The modification of your code below works with your sample data table.

```
Names Default To Here( 1 );
dt = Current Data Table();
row = Associative Array( :test_article ) << Get Keys;
nc = N Items( row );
For( i = 1, i <= nc, i++,
Eval(
Parse(
"New Column( \!"PBS vs " || row[i] ||
"\!",
Character,
Nominal,
Formula(
if (Row() == 1, theTest = \!""
|| row[i] ||
"\!");
If( :test_article == \!"PBS\!",
zip=:test_article,
If( :test_article == theTest,
zip=\!"test\!",
zip=\!"EXCLUDE\!"
)
);
zip
)
)
);"
)
)
);
```

Jim

Highlighted

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

You need to get the value of row[i] into the formula. JMP doesn't evaluate expressions inside the Formula() part of the New Column() message.

You can use Substitute() to get the actual value of row[i] into the formula.

```
Names Default To Here( 1 );
dt = Current Data Table();
row = Associative Array( :test_article ) << Get Keys;
nc = N Items( row );
For( i = 1, i <= nc, i++,
Eval(
Substitute(
Expr(
New Column( "PBS vs " || row[i],
"Character",
"Nominal",
Formula( If( :test_article == "PBS" | :test_article == ri, :test_article, "EXCLUDE" ) )
)
),
Expr( ri ), Eval( row[i] )
)
)
);
```

-Jeff

4 REPLIES 4

Highlighted

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

The primary issue you are having is that within the definition of a formula, one can not have items that need to be evaluated. JMP thinks everything within the formula definition is to be evaluated only when the formula is executed.

Therefore, the formula that is passed into the New Column definition, must be complete in it's syntax. The modification of your code below works with your sample data table.

```
Names Default To Here( 1 );
dt = Current Data Table();
row = Associative Array( :test_article ) << Get Keys;
nc = N Items( row );
For( i = 1, i <= nc, i++,
Eval(
Parse(
"New Column( \!"PBS vs " || row[i] ||
"\!",
Character,
Nominal,
Formula(
if (Row() == 1, theTest = \!""
|| row[i] ||
"\!");
If( :test_article == \!"PBS\!",
zip=:test_article,
If( :test_article == theTest,
zip=\!"test\!",
zip=\!"EXCLUDE\!"
)
);
zip
)
)
);"
)
)
);
```

Jim

Highlighted
##

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

Re: Create and populates new columns based categories in an existing column

Works like a charm!

Thanks

Thanks

Highlighted

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

You need to get the value of row[i] into the formula. JMP doesn't evaluate expressions inside the Formula() part of the New Column() message.

You can use Substitute() to get the actual value of row[i] into the formula.

```
Names Default To Here( 1 );
dt = Current Data Table();
row = Associative Array( :test_article ) << Get Keys;
nc = N Items( row );
For( i = 1, i <= nc, i++,
Eval(
Substitute(
Expr(
New Column( "PBS vs " || row[i],
"Character",
"Nominal",
Formula( If( :test_article == "PBS" | :test_article == ri, :test_article, "EXCLUDE" ) )
)
),
Expr( ri ), Eval( row[i] )
)
)
);
```

-Jeff

Highlighted
##

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

Re: Create and populates new columns based categories in an existing column

Also works like a charm.

Thanks a lot

Thanks a lot

Article Labels

There are no labels assigned to this post.