cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • JMP 19 is here! See the new features at jmp.com/new.
  • Register to attend Discovery Summit 2025 Online: Early Users Edition, Sept. 24-25.
Choose Language Hide Translation Bar
hogi
Level XIII

robust column reference

After a few minutes coding with JSL, users wonder about strange objects like :sex in 

 

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

x = :sex

 

 

Expert users have some experience with such column references and use them all the time.
But what is :sex ? Is  dt:sex  more robust? Where do I find a documentation ?

Let's collect and share some insights.

my 5 cents:

12 REPLIES 12
hogi
Level XIII

Re: robust column reference

In JSL, one and the same column reference :sex (or dt:sex) can be used in two ways: 

1. as a reference to a column

:sex << get name;
Data Table( "Big Class Families" ) << Distribution(	Nominal Distribution( Column( :sex ) ));

 

 ... and 2 : to retrieve the entry of the current row in this column, like in:

new column("s", formula(:sex) )

which is the short form of 

new column("s", formula (:sex[row()])

// slightly shorter:

new column("s", formula (:sex[])



To create such an object from a string, one has 2 options:

column name = "sex";
col= Column (column name); // for the first one

val = As Column (column name) // for the second one


Actually, there is a very cool 3rd option, which I learned from @jthi :

the universal column reference = Name Expr(As Column(column name))

it can be use like :sex for both approaches. referencing the column and accessing the entry of the current row.

Column() vs As Column() vs datable:column vs dt:As name("column") 

hogi
Level XIII

Re: robust column reference

:sex looks fragile - what happens when the user changes the column name?

Names Default to Here(1);
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

x = Name Expr(As column("name"));
Show(Name Expr(x)); // : name 

:name << set name("The name");

Show(Name Expr(x)); // :The name

wow, great! Not fragile at all!

hogi
Level XIII

Re: robust column reference

And when the user changes the data table?

Names Default to Here(1);
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

x = Name Expr(As column("name"));
Show(Name Expr(x));
x[1] = "Marker";

:name << set name("The name");

dt = Open( "$SAMPLE_DATA/Big Class Families.jmp" );

Show(Name Expr(x));
Show(x[1])

The innocent :sex even knows which data table it actually belongs to. Wonderful!

hogi
Level XIII

Re: robust column reference

Now, let's go from 1 column to 3 columns:

myList1 = {:name,:sex,:age};


A very complicated and useless way to create this list is:

myList2 = Eval List({Name Expr(As column(:name)),Name Expr(As column(:sex)),Name Expr(As column(:age))});

 

hogi_0-1758660151284.png

 

hogi
Level XIII

Re: robust column reference

useless?

you already notice the big difference when you try to execute the above code without a 

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );


The first line does not care that there is no open data table. When you run the second line, JMP will complain:

hogi_1-1758660245484.png


Even better: when the list is created with the second approach, the column references are very robust.
Independent of the current data table, JMP will go to dt and and use the columns from there:

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

myList1 = {:name,:sex,:age};

myList2 = Eval List({Name Expr(As column(:name)),Name Expr(As column(:sex)),Name Expr(As column(:age))});

dt2= Open( "$SAMPLE_DATA/Big Class Families.jmp" );

dt << Select Rows( [1] ) << Delete Rows; // now dt starts with Louise - there is no Katie anymore!

Show(Eval List(myList1), // Katie? why Katie!!!
Eval List(myList2)); // Louise -> works!

hogi_2-1758660353341.png


How does it work?

Show(Type(myList1[1]), Type(myList2[1]))

provides more insight:

hogi_3-1758660470227.png


The simple list (myList1) contains "Names" , *)

the complicated list (myList2) contains "Columns" **)

 

*) When a name is evaluated, JMP follows a specific rule to determine what this name means:
 https://www.jmp.com/support/help/en/19.0/#page/jmp/rules-for-resolving-names.shtml 

Among the roles, JMP will search for a column in the current data table (dt2).

 

**) For a  Column, JMP just has to go to THE data table ("dt") and access the column.
Very robust!

hogi
Level XIII

Re: robust column reference

Name Expr(As column(:col))

is soooo cool!

Byron_JMP
Staff

Re: robust column reference

The Column() works in a lot of situations and its pretty flexible with respect to the format of the column name.

 

Distribution(
	Continuous Distribution( Column( :Random Normal )), 
	Continuous Distribution( Column( "Random Normal" )),
	Continuous Distribution( Column( :"Random Normal"n )),
	Continuous Distribution( Column( "Random Normal"n )),
	 Process Capability( 0 ) )
JMP Systems Engineer, Health and Life Sciences (Pharma)
hogi
Level XIII

Re: robust column reference


@Byron_JMP wrote:

The Column() works in a lot of situations and its pretty flexible with respect to the format of the column name.

 

Distribution(
	Continuous Distribution( Column( :Random Normal )), 
	Continuous Distribution( Column( "Random Normal" )),
	Continuous Distribution( Column( :"Random Normal"n )),
	Continuous Distribution( Column( "Random Normal"n )),
	 Process Capability( 0 ) )



Hi @Byron_JMP , one can have a lot of fun with Column() inside distribution()

 

Names Default to Here(1);
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

Distribution(Continuous Distribution( Column( :height )) );
Distribution(Continuous Distribution( Column( "height")) );

A Column reference, a name - even a String!   Everything works.

... till you try to use a  column that is defined outside of the Distribution command:

col = Column("height");
Show(col); // shows: it's a column
Distribution(Continuous Distribution(col));
Distribution(Continuous Distribution(Eval(col)));
Distribution(Continuous Distribution(Name Expr(col)));

One can try all such esoteric "Let's just try all of them - one of them should work"-Expression Handling-Tricks, none of them works!

Always the same hint, but what does it mean?!?

hogi_1-1758741988633.png

 


The solution is very easy - but it can take hours when you search for it at the wrong spot. 
Find the solution on your own before you open  hogi_0-1758741758488.png

View more...
Column( :height ) inside Continuous Distribution() looks like Column("height") in
col = Column("height");
But it is a named argument - like Formula() in New Column(Formula(...)).

So the "obvious" solution is: keep the name of the named argument and place the column inside:
Distribution(Continuous Distribution(Column(col)));

 

 

hogi
Level XIII

Re: robust column reference

interesting: when asked for lists of columns, JSL functions return lists of names, not lists of  columns!

 

Names Default to Here(1);
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );


colList = Eval List({Name Expr(As column("sex"))});
write(Name Expr(colList[1]), " is a  ", // :sex
Type(Name Expr(colList[1])), "\!n"); // Column -> universal & robust


selectedCols = dt << select columns (:sex) << get selected columns;
write(Name Expr(selectedCols[1])," is a ", // : sex
Type(Name Expr(selectedCols[1])), "\!n"); // Name -> fragile

dt << Group Columns(Item Range( :sex, :height ),"myColGroup");
colsFromColumnGroup = dt << get column group("myColGroup");
Write(Name Expr(colsFromColumnGroup[1]), " is a  ", // :sex
Type(Name Expr(colsFromColumnGroup[1])), "\!n"); // Name -> fragile

hogi_3-1758743390560.png

 

The difference between  Get Column Names() and Get Column References():

ColNames = dt << get column names; // default setting: returns names, not Strings 
Write(Name Expr(ColNames[3]), " is a  ", // sex (more or less the same as :sex)
Type(Name Expr(ColNames[3])), "\!n"); // Name -> fragile


ColRefs = dt << get column references;
Write(Name Expr(ColRefs[3]), " is a  ", // Column("sex"
Type(Name Expr(ColRefs[3])), "\!n"); // Column -> robust, but not :sex, see below

hogi_2-1758743369920.png

 


and now let's play with the column and then ask for the value of  :sex in row 1:

//hide the column
:sex <<  set name("hidden");

// who finds it?
row()=1;
Try(Show(Eval(ColList[1])), print(exception_msg));
Try(Show(Eval(colsFromColumnGroup[1])), print(exception_msg));
Try(Show(Eval(selectedCols[1])), print(exception_msg));
Try(Show(Eval(ColNames[3])), print(exception_msg));
Try(Show(Eval(ColRefs[3])), print(exception_msg));

hogi_0-1758743322998.png

Recommended Articles