cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
Choose Language Hide Translation Bar
PS
PS
Level III

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

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

View solution in original post

3 REPLIES 3
ih
Super User (Alumni) ih
Super User (Alumni)

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

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
PS
Level III

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

This works great, thanks