Name quote a strange column nameI'm trying to insert a column name into a fit model expression. For that I need to escape strange characters in column names to do that we use the :"name"n convention. So how do I go from a string column name to this format?
colListY = {"Plating Thickness (m\!"\!")"};
//This doesn't work
newcolumnref = eval insert("\[:"^colListY[1]^"n]\");
//Looking for this end result
:"Plating Thickness (m\!"\!")"nWhich I can then insert anywhere, for example:
insert into(expr(Y()), newcolumnref);
Anybody know of a robust way to do this?
Data Exploration and VisualizationRe: Name quote a strange column name@Mark_Bailey
Ah, so easy! great!!!
I was surprised to hear from @pauldeen :
https://community.jmp.com/t5/Discussions/Name-quote-a-strange-column-name/m-p/415735/highlight/true#M66485
that
when building the fit model script the column() format cannot be used:</CODE><HR /></BLOCKQUOTE><P> </P><PRE><CODE class=" language-jsl">Fit Model(
//Y( :"Plating Thickness (m\!"\!")"n ), <- works
Y(column("Plating Thickness (m\!"\!"")), //<- does not workOK, if it's like this, what would be the easiest solution to get the column without column() ...
Now I was surprised to see (by running your code) that it actually worked.
And went back to the old sniplet - and it didn't work.
Woah!
The puzzle gets solved if you put both lines of code exactly below each other:

-> So fortunately, no fancy non-Column() coding required for Fit Model.
Re: Name quote a strange column nameNames Default To Here( 1 );
dt = New Table( "plating",
New Column( "Plating Thickness (m\!"\!")", Set Values( [1, 2, 3, 4] ) ),
New Column( "X", Set Values( [12, 26, 33, 39] ) )
);
col = Column( dt, "Plating Thickness (m\!"\!")" );
Fit Model(
Y( col ), // <- works :)
Effects( :X ),
Personality( "Standard Least Squares" ),
Run
);
Re: Name quote a strange column name
dt = New Table( "plating",
Add Rows( 4 ),
New Column( "Plating Thickness (m\!"\!")",Set Values( [1, 2, 3, 4] )),
New Column( "X",Set Values( [12, 26, 33, 39] ))
);
colListY = {"Plating Thickness (m\!"\!")"};<BR /><BR /><BR />
newcolumnref = Name Expr(AsColumn(colListY[1])); // -> :"String"n<BR />
Fit Model(
//Y( :"Plating Thickness (m\!"\!")"n ), <- works
//Y(column("Plating Thickness (m\!"\!"")), //<- does not work
Y(newcolumnref), // <- works :)
Effects(:X),
Personality( "Standard Least Squares" )
);from
https://community.jmp.com/t5/Discussions/Need-help-with-expressions-column-references-formulas-and-column/m-p/490220/highlight/true#M73282
Re: Name quote a strange column namecolListY = {"Plating Thickness (m\!"\!")"};
dt = Open( "$sample_data/big class.jmp" );
dt:age << setname( colListY[1] );
Eval(
Eval Expr(
dt << New Column( "test",
Numeric,
"Continuous",
Format( "Fixed Dec", 12, 2 ),
Formula( Column( Expr( colListY[1] ) )[] ),
Set Display Width( 78 )
)
)
);The empty [ ] tells JMP to evaluate the column for the current row.
Building column formulas like this probably hides the underlying column dependencies from JMP's formula evaluator; see discussion. If you have formula columns depending on other formula columns, JMP may not be able to peer into this indirection to figure out what to evaluate first.
Re: Name quote a strange column nameThat results in a column formula error for me
Re: Name quote a strange column name(without trying it, so might be wrong)
Formula( column(expr(colListY[1])) - :height ),
I think you want the expr inside the column. That way the column function is evaluated later, using the string captured earlier.
Re: Name quote a strange column name@XanGregg Column formulas don't work this way and the special quoting methods does work:
colListY = {"Plating Thickness (m\!"\!")"};
dt = Open( "$sample_data/big class.jmp" );
dt:age << setname( colListY[1] );
eval(eval expr(dt << New Column( "test",
Numeric,
"Continuous",
Format( "Fixed Dec", 12, 2 ),
Formula( expr(column(colListY[1])) - :height ),
Set Display Width( 78 )
)));It produces a function column in which the formula is:
Column( "Plating Thickness (m\!"\!")" ) - /*###*/:height/*###*/
which does not evaluate.
This does:
WrapColumns = Function({ColName},{default local},
ReturnList = {};
If(is list(ColName),
For(i=1, i<=n items(ColName), i++,
insert into(ReturnList, parse(":" || Log Capture( Print( ColName[i] ) ) || "n"))
);
,
insert into(ReturnList, parse(":" || Log Capture( Print( ColName ) ) || "n"))
);
Return(ReturnList);
);
eval(eval expr(dt << New Column( "test2",
Numeric,
"Continuous",
Format( "Fixed Dec", 12, 2 ),
Formula( expr(WrapColumns(colListY)[1]) - :height ),
Set Display Width( 78 )
)));Do you know a way to fix this with the column() system?
Re: Name quote a strange column nameAfter carefull testing and going back through a few of my scripts I have not been able to make it fail again. Thanks for checking!
Re: Name quote a strange column nameThis is a great question!
Re: Name quote a strange column nameThis is a great question!
Re: Name quote a strange column name@XanGregg Now that I'm testing it with your code it works here as well. Let me investigate and see if I can share the failing example more completely...
Re: Name quote a strange column nameJust for giggles, I took the code back to JMP 11.2.1 and it works great.
Re: Name quote a strange column nameCan you elaborate on the "does not work" part? I tried versions back to JMP 15 and the following seems to work. Maybe I'm missing part of the issue.
Data Table( "Big Class" ):weight << Set Name( "weight\!"\!"" );
Fit Model(
Y( Column( "weight\!"\!"" ) ),
Effects( :age, :sex, :height ),
Personality( "Standard Least Squares" ),
Run()
);
Re: Name quote a strange column nameTo explain for any one watching: the print function does two things: it escapes the internal quotation marks (and other special cases) and it adds external quotation marks. That produces a valid string, and Paul's code then adds a : in front and an n afterward and parses the result into a JSL column name constant. There might, or might not, be an actual column that the column name constant can attach to when used in a script. The column does not need to exist when Paul's code runs. Without the print() function, I'm not sure what function I'd use to get the escaping done (the rules are too complicated to write my own.) Because the original name contained embedded quotation marks, it needs the escaping before being turned into a column name constant.
If the data table is open, you can use the column() function in much the same way to get a column object that (I'm pretty sure) can be used in all the same places that the column name constant can appear. That column object refers to a real column, at least until the table is closed. Launching a platform with an expired column object or a column name constant that doesn't match a table column will produce an expected error message.
@XanGregg
Re: Name quote a strange column nameUpdated with Craige's feedback, thanks for the 1/i!
WrapColumns = Function({ColName},{default local},
ReturnList = {};
If(is list(ColName),
For(i=1, i<=n items(ColName), i++,
insert into(ReturnList, parse(":" || Log Capture( Print( ColName[i] ) ) || "n"))
);
,
insert into(ReturnList, parse(":" || Log Capture( Print( ColName ) ) || "n"))
);
Return(ReturnList);
);Re: Name quote a strange column nameIt is too complicated. You can probably replace the regex() with trim() because that is all it is doing. Originally I thought it was going to remove the outer quotation marks that print() leaves behind. (edit: and there is no need to trim at all for the input to parse!)
You might have [1] where you need [i] in your example.
Re: Name quote a strange column nameThanks Craige, It seems like this is more complicated than it should be but it works 
For anybody trying to do this, I put Craige's solution in a function:
WrapColumns = Function({ColName},{default local},
ReturnList = {};
If(is list(ColName),
For(i=1, i<=n items(ColName), i++,
insert into(ReturnList, parse(":" || Regex( Log Capture( Print( ColName[1] ) ), "^\s*(.*)\s*$", "\1" ) || "n");)
);
,
insert into(ReturnList, parse(":" || Regex( Log Capture( Print( ColName ) ), "^\s*(.*)\s*$", "\1" ) || "n");)
);
Return(ReturnList);
);Re: Name quote a strange column nameMore notes:
I explicitly referenced the table in the column function. Might make a difference if multiple tables are open.
If you are concatenating strings, this might do it; you can always use the :"height"n even if not required (height, for example.)
colreftxt = ":" || Regex( Log Capture( Print( colListY[1] ) ), "^\s*(.*)\s*$", "\1" ) || "n";
write(colreftxt);
:"Plating Thickness (m\!"\!")"n
</CODE>which might be escaped right. Expression manipulation will probably be easier than parsing text assembled like this.</P><P> </P><P> </P></LINGO-BODY><LINGO-SUB id="lingo-sub-415759" slang="en-US" mode="NONE">Re: Name quote a strange column name</LINGO-SUB><LINGO-BODY id="lingo-body-415759" slang="en-US" mode="NONE"><P>try this combination of the two answers:</P><PRE><CODE class=" language-jsl">colListY = {"Plating Thickness (m\!"\!")"};
dt = Open( "$sample_data/big class.jmp" );
dt:age << setname( colListY[1] );
script = Expr(
Fit Model(
Y( F1 ),
Effects( :height ),
Personality( "Standard Least Squares" ),
Run( F1 << {Plot Actual by Predicted( 1 ), Plot Residual by Predicted( 1 ), Plot Effect Leverage( 1 )} )
)
);
Substitute Into( script, Expr( F1 ), Column( dt, colListY[1] ) );
Write( Name Expr( script ) );
/*
Fit Model(
Y( Column( "Plating Thickness (m\!"\!")" ) ),
Effects( :height ),
Personality( "Standard Least Squares" ),
Run(
Column( "Plating Thickness (m\!"\!")" ) << {Plot Actual by Predicted( 1 ),
Plot Residual by Predicted( 1 ), Plot Effect Leverage( 1 )}
)
)
*/
Eval( script );
run with column substitutedrun with column substitutedrun with column substituted
Re: Name quote a strange column name@txnelson The problem is that when building the fit model script the column() format cannot be used:
Fit Model(
//Y( :"Plating Thickness (m\!"\!")"n ), <- works
Y(column("Plating Thickness (m\!"\!"")), //<- does not work
Effects(
:"Shift-temp"n, :Position, :Subgroup, :"Shift-temp"n * :Position,
:"Shift-temp"n * :Subgroup, :Position * :Subgroup
),
Personality( "Standard Least Squares" )
);@Craige_Hales the users selects the columns with a Col list box() and I pass a << get items message to it. That gives me the list of column names as a string. I could locate those column names in the table and find the column number but when assembling the effects expression this method will not allow me to build the interactions as shown above.
Is there a way to do expression concatenation to go from string to col reference with name escape?
Re: Name quote a strange column nameIf you can avoid getting the name of the column as a string, and get the column object instead, it might be easier. Below, cols is a list of column objects, not their names.
colListY = {"Plating Thickness (m\!"\!")"};
dt = Open( "$sample_data/big class.jmp" );
dt:age << setname( colListY[1] );
cols = dt << getcolumnnames;// don't use the string keyword
/* {name, "Plating Thickness (m\!"\!")"n, sex, height, weight} */
script = Expr(
Fit Model(
Y( :weight ),
Effects( F1 ),
Personality( "Standard Least Squares" ),
Run(
:weight << {Plot Actual by Predicted( 1 ), Plot Residual by Predicted( 1 ),
Plot Effect Leverage( 1 )}
)
)
);
Substitute Into( script, Expr( F1 ), cols[2] );
Write( Name Expr( script ) );
/*
Fit Model(
Y( :weight ),
Effects( "Plating Thickness (m\!"\!")"n ),
Personality( "Standard Least Squares" ),
Run(
:weight << {Plot Actual by Predicted( 1 ), Plot Residual by Predicted( 1 ),
Plot Effect Leverage( 1 )}
)
);
*/Re: Name quote a strange column nameYou can use the Column, or As Column functions to provide protection to the complex column name
colListY = {"Plating Thickness (m\!"\!")"};
newcolumnref = eval insert("column(\!"^colListY[1]^\!")");