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
- :
- Creating new columns with a For loop and giving each column its own name and for...

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

Highlighted

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

Jan 4, 2018 8:26 AM
(2456 views)

Hello everyone,

I have a large data table with values and column headers test1,test2,test3,test4,...,test800. Some of these values are negative, and I would like to take all the columns I define in the list "Datalist_log_2" and create a new column called "test1_pos" with the absolute values. (For each test).

```
Datalist_log_2 = {test1, test2, test5, test319};
wmax = N Items( Datalist_log_2 );
For ( w = 1, w <=wmax, w++,
Temp12 = Char(Datalist_log_2[w]);
NewColName = Temp12||"_pos"; //not sure if I need to do this extra round, but it works. Will figure that out myself.
NewColumn(NewColName, Numeric, Continuous, Formula(Abs(Datalist_log_2[w])))
);
```

The problem seems to be that the Formula() is trying to build the absolute value of the variable "test1" in the first iteration, but of course I don't want sqrt(test1*test1). Instead I would like the newly created column to get the absolute values of the reference column with the name "test1".

Thanks a lot for your help

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

Below is a modification of your current code, using the same methodology as I passed to you in your previous post "*Combining For loop and Dispatch() command in a variability chart" *. If you are not able to interpret what is going on, I suggest that you take a look in the Scripting Guide for explanation.

Help==>Books==>Scripting Guide

```
Datalist_log_2 = {test1, test2, test3};
For( w = 1, w <= N Items( Datalist_log_2 ), w++,
Eval(
Substitute(
Expr(
New Column( Char( Datalist_log_2[w] ) || "_pos",
Numeric,
Continuous,
Formula( Abs( __Col__ ) )
)
),
Expr( __col__ ), Parse( ":" || Char( Datalist_log_2[w] ) )
)
)
);
```

Jim

3 REPLIES 3

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

Re: Creating new columns with a For loop and giving each column its own name and formula

How timely, check out this JSL Cookbook article which does something very similar. Just specify your own list of columns to change, then modify the new column name and formula.

I suggest selecting the columns and using the Current Data Table() << Get Selected Columns message to write the list to the log, which you can then copy and paste into your code.

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

Below is a modification of your current code, using the same methodology as I passed to you in your previous post "*Combining For loop and Dispatch() command in a variability chart" *. If you are not able to interpret what is going on, I suggest that you take a look in the Scripting Guide for explanation.

Help==>Books==>Scripting Guide

```
Datalist_log_2 = {test1, test2, test3};
For( w = 1, w <= N Items( Datalist_log_2 ), w++,
Eval(
Substitute(
Expr(
New Column( Char( Datalist_log_2[w] ) || "_pos",
Numeric,
Continuous,
Formula( Abs( __Col__ ) )
)
),
Expr( __col__ ), Parse( ":" || Char( Datalist_log_2[w] ) )
)
)
);
```

Jim

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

Re: Creating new columns with a For loop and giving each column its own name and formula

This works great, thanks