BookmarkSubscribeRSS Feed
Choose Language Hide Translation Bar
Highlighted
PS

Community Trekker

Joined:

Jan 2, 2018

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

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
txnelson

Super User

Joined:

Jun 22, 2012

Solution

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

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
ih

Community Trekker

Joined:

Sep 30, 2016

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.

txnelson

Super User

Joined:

Jun 22, 2012

Solution

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

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
PS

Community Trekker

Joined:

Jan 2, 2018

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

This works great, thanks