cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
pcarroll1
Level IV

Inconsistent acceptable syntax

AllCols = dt << Get Column Names();
AllColNames = dt << Get Column Names(string);
for(i=3, i<=n items(AllCols), i++, 
	colcol = AllCols[i];
	cname = AllColNames[i];
	nnm1 = Col Number(colcol);
	Fstr = "num(:\!""||cname||"\!"n)";
	Fexpr = parse(Fstr);
	newcol = dt << new column("Temp", numeric, formula(eval(evalexpr(Fexpr))));
	nnm2 = Col Number(newcol);
	if(nnm2 == nnm1, 
		newcol << delete formula();
		dt << Delete Columns(colcol);
		newcol << set name(cname),  //else
		dt << Delete Columns(newcol);
	);
);

I am using JMP18 on Windows.  When I write code that I want to use often, I like to make a toolbar button for it.  But sometimes I find that code that is acceptable to run from an open script window is not acceptable when run from a toolbar icon.  Here is an example, where I am looping through columns and changing them from character to numeric if all entries are numeric.  When running from a toolbar the objection is for the line:

newcol = dt << new column("Temp", numeric, formula(eval(evalexpr(Fexpr))));

This syntax is acceptable when I run from a script window!

Why would the jsl interpreter be different in each situation?

What syntax for this would be universally acceptable?

For very large datasets and columns this can be slow.  Is there a better way to do this?  

14 REPLIES 14
Craige_Hales
Super User

Re: Inconsistent acceptable syntax

What error message did you get? I think the script window has a current data table but the toolbar script may not. Maybe just adding

currentDatatable(dt);

at the start of the script would be enough.

You could 

print(currentDatatable());

at the start of the script to find out what the toolbar script knows.

 

 

Craige
pcarroll1
Level IV

Re: Inconsistent acceptable syntax

Thanks Craige.

The error message is:

Name Unresolved: 2PCS_X(um) at row 1 in access or evaluation of '2PCS_X(um)' , "2PCS_X(um)"n /*###*/

"2PCS_X(um)" is the name of the first column to be evaluated.

So the expression for the first column is supposed to be:

newcol = dtTOSA << new column("Temp", numeric, formula(Num( :"2PCS_X(um)"n )));

pcarroll1
Level IV

Re: Inconsistent acceptable syntax

Also, in the actual script I don't use dt as a variable.  I use a much more descriptive variable, like dtTOSA. (see above)  Everytime I want to address a particular data table I always explicitly state it to avoid confusion, as I did in this statement.  I don't see how the interpreter could think I'm referring to a different table.  

jthi
Super User

Re: Inconsistent acceptable syntax

Toolbars might be adding Names Default To Here(1) (which I say you should always have) to your script

jthi_0-1731602218857.png

which can then potentially show some errors which wouldn't be visible if everything is in global scope (but still they are usually errors). I build my formulas using either Eval(EvalExpr()) or Eval(Substitute())

Eval(EvalExpr(
	newcol = dt << New Column("Temp", numeric, formula(Num(Expr(NameExpr(AsColumn(dt, i))))));
));

For large datasets you might have to consider sampling for something like this.There are also non-formula solutions which might be faster but it might also depend on your data.

 

For some reason I haven't published this to JMP community (or atleast I couldn't find it), Character Columns to Numeric (github.com) , you can take a look at the code to see how I have performed this task from https://github.com/jthi-jmp-tools/character-columns-to-numeric/blob/main/bin/bin/character_columns_t.... I have since redone this script and if I remember correctly I do use a bit different method nowadays.

-Jarmo
hogi
Level XII

Re: Inconsistent acceptable syntax

toolbar icons know the  current data table():
The problem is rather the inverse one.

Please  define "dt=current data table()" before the 

dt << Get Column Names();

 

 

Craige_Hales
Super User

Re: Inconsistent acceptable syntax

Maybe, but the error message suggests JMP is not looking in the right place for "2PCS_X(um)"n. I'm not sure what context the toolbar script can use.

Craige
pcarroll1
Level IV

Re: Inconsistent acceptable syntax

Craige,

   I used your suggestion of using a print statement and found that it never got to the line I thought was the problem.

newcol = dt << new column("Temp", numeric, formula(eval(evalexpr(Fexpr))));

The problem was at this line. 

nnm1 = Col Number(colcol);

So I changed it to this and that worked.

nnm1 = dtTOSA << Col Number(colcol);

 I found that I also needed to remind JMP again what the current table was later in the code and I had to use {} in Delete Columns before it worked. For some reason JMP doesn't care if those curly brackets are there if run from a script window.

The code snippet is now this.  By the way this is a snippet of a longer code that also creates this and another table.  

AllCols = dtTOSA << Get Column Names();
AllColNames = dtTOSA << Get Column Names(string);
currentDatatable(dtTOSA);
for(i=3, i<=n items(AllCols), i++, 
	colcol = AllCols[i];
	cname = AllColNames[i];
	nnm1 = dtTOSA << Col Number(colcol);
	Fstr = "num(:\!""||cname||"\!"n)";
	Fexpr = parse(Fstr);
	newcol = dtTOSA << new column("Temp", numeric, set formula(eval(evalexpr(Fexpr))));
	nnm2 = dtTOSA << Col Number(newcol);
	if(nnm2 == nnm1, 
		currentDatatable(dtTOSA);
		newcol << delete formula();
		dtTOSA << Delete Columns({colcol});
		wait(0);
		newcol << set name(cname),  //else
		dtTOSA << Delete Columns({newcol});
	);
);

However, now there is a different problem.  Even though, I delete the old column before renaming the new column, JMP puts an unwanted 2 after the column name!  Again it does not do this running from a script window. I tried including a wait(0) statement but that didn't help.

How can this be fixed? 

 

pcarroll1
Level IV

Re: Inconsistent acceptable syntax

Actually, it is not performing the delete column operation. 

dtTOSA << Delete Columns({colcol});

So it's keeping the original character column and the new numeric column.

pcarroll1
Level IV

Re: Inconsistent acceptable syntax

Strangely, if it goes to the "else" condition that delete statement works fine.