Share your ideas for the JMP Scripting Unsession at Discovery Summit by September 17th. We hope to see you there!
Choose Language Hide Translation Bar
Highlighted
gallardet
Level III

Error in loop to standarize numeric columns

Hi. I have written a script to create new numerical columns from the formula to standardize but I get an error when evaluating the formula. What do I do wrong?
Thank you.

The script:

 

Names Default To Here( 1 );
dt = Current Data Table();
numericColNames = dt << get column names( string, numeric );
For( i = 1, i <= N Items( numericColNames ), i++,
New Column( Char( numericColNames[i] ) || "_Stand", 
Numeric, 
Continuous, 
Formula( Col Standardize( :numericColNames[i] ) ) )
);

 

The error:

 

Name Unresolved: numericColNames 1 times At rows: {1} Operation: numericColNames, :numericColNames

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted
txnelson
Super User

Re: Error in loop to standarize numeric columns

A column name like 1900 can confuse JMP, because it may not know if it should treat it as a number or a column name.  So one needs to specifically indicate that it is a column name by using the :Name() function...see the reworked script below

Names Default To Here( 1 );
dt = Current Data Table();
numericColNames = dt << get column names( string, numeric );
numericColNames = {"1900"}
For( i = 1, i <= N Items( numericColNames ), i++,
	Eval(
		Substitute(
				Expr(
					New Column( Char( numericColNames[i] ) || "_Stand",
						Numeric,
						Continuous,
						Formula( Col Standardize( __col__ ) )
					)
				),
			Expr( __col__ ), Parse( ":Name(\!"" || numericColNames[i] || "\!")" )
		)
	)
);
Jim

View solution in original post

Highlighted
gallardet
Level III

Re: Error in loop to standarize numeric columns

Sir, you seem to have an answer for everything. Thanks again for your contribution!

View solution in original post

4 REPLIES 4
Highlighted
txnelson
Super User

Re: Error in loop to standarize numeric columns

When you specify a formula value, it will not do any evaluation of the JSL for the formula.  Rather, it will just move the code into the formula.  So, what is being placed into the formula is " Col Standardize( :numericColNames[i] )", not,  "Col Standardize( :YourColumnName )".  So when the formula is executed, it will look for the value of "i", which by the time it executes, it has already moved beyone your subscript range for Colnames.  So what needs to be done is to place the exact JSL syntax for the formula, into the formula.

Names Default To Here( 1 );
dt = Current Data Table();
numericColNames = dt << get column names( string, numeric );
For( i = 1, i <= N Items( numericColNames ), i++,
	Eval(
		Substitute(
				Expr(
					New Column( Char( numericColNames[i] ) || "_Stand",
						Numeric,
						Continuous,
						Formula( Col Standardize( __col__ ) )
					)
				),
			Expr( __col__ ), Parse( ":" || numericColNames[i] )
		)
	)
);
Jim
Highlighted
gallardet
Level III

Re: Error in loop to standarize numeric columns

Dear Sir, I have a small table to try. It consists of a column with names of municipalities a column (1900) with integer values corresponding to the population in 1900 and another column expressed in percentages.  When I try the scritp I find that, although the instruction "numericColNames = dt << get column names( string, numeric );"  recognizes the col 1900 and "Percentage Canvi" as numerics when creating the two new columns, the one corresponding to 1900 leaves it with missing values.

 
Highlighted
txnelson
Super User

Re: Error in loop to standarize numeric columns

A column name like 1900 can confuse JMP, because it may not know if it should treat it as a number or a column name.  So one needs to specifically indicate that it is a column name by using the :Name() function...see the reworked script below

Names Default To Here( 1 );
dt = Current Data Table();
numericColNames = dt << get column names( string, numeric );
numericColNames = {"1900"}
For( i = 1, i <= N Items( numericColNames ), i++,
	Eval(
		Substitute(
				Expr(
					New Column( Char( numericColNames[i] ) || "_Stand",
						Numeric,
						Continuous,
						Formula( Col Standardize( __col__ ) )
					)
				),
			Expr( __col__ ), Parse( ":Name(\!"" || numericColNames[i] || "\!")" )
		)
	)
);
Jim

View solution in original post

Highlighted
gallardet
Level III

Re: Error in loop to standarize numeric columns

Sir, you seem to have an answer for everything. Thanks again for your contribution!

View solution in original post

Article Labels

    There are no labels assigned to this post.