cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
] />

Discussions

Solve problems, and share tips and tricks with other JMP users.
Choose Language Hide Translation Bar
EOwl
Level II

Return the Name of a referenced column

I'm writing a helper function that renames the a column group if it contains a particular column name.

Names Default To Here( 1 );

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

/*//Use if already existing
dt = Data Table ("Cities");
*/

dt << group columns( "xy", {:X, :y} );
dt << group columns( "pollutants", :Ozone :: :Lead );

match_col = "X";
new_name = "coordinates";

groups_list = dt << get column groups names;
show(groups_list);

For Each({grp, ig}, groups_list,
	cols = dt << get column group( grp );
	show(cols);
	For Each ({column, ic}, cols,
		show(column);
		colname = column << Get Name;
		If(colname == match_col,
			try(dt << rename column group( grp, new_name );)
		)
	);
);

This works fine with basic grouped columns, but falls over if there's an inherited group from a Link Reference:

Names Default To Here( 1 );

dt = Open( "$SAMPLE_DATA/Cities.jmp" );
dt_state = Open( "$SAMPLE_DATA/State Abbreviations.jmp" );

//Need more than 1 column
dt_state << New Column( "Dummy", Numeric, "Continuous", Format( "Best", 12 ));

//Link Tables
dt_state:Abbreviations << Set Property( "Link ID", 1 );
dt:State << Set Property("Link Reference", Reference Table(dt_state));

/*//Use if already existing
dt = Data Table ("Cities");
dt_state = Data Table( "State Abbreviations" );
*/

dt << group columns( "xy", {:X, :y} );
dt << group columns( "pollutants", :Ozone :: :Lead );

match_col = "X";
new_name = "coordinates";

groups_list = dt << get column groups names;
show(groups_list);

For Each({grp, ig}, groups_list,
	cols = dt << get column group( grp );
	show(cols);
	For Each ({column, ic}, cols,
		show(column);
		colname = column << Get Name;
		If(colname == match_col,
			try(dt << rename column group( grp, new_name );)
		)
	);
);

Firstly, the << Get Name function errors on :Ozone, which worked fine in the simpler example.

Even if that error is resolved, the linked column looks different in the Log:

column = :Y;
column = Referenced Column("U.S. States[State]", Reference(Column(:State), Reference(Column(:U.S. States))));

How do extract the text name? i.e "U.S. States[State]"

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: Return the Name of a referenced column

Most likely you are running into issues as JMP is referring to wrong data table. My suggestion is to ALWAYS use explicit column references -> convert your column (references) always to strings and then use different formats as needed. In your case something like this

 

Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Cities.jmp");
dt_state = Open("$SAMPLE_DATA/State Abbreviations.jmp");

dt_state << New Column("Dummy", Numeric, "Continuous", Format("Best", 12));

dt_state:Abbreviations << Set Property("Link ID", 1);
dt:State << Set Property("Link Reference", Reference Table(dt_state));

dt << group columns("xy", {:X, :y});
dt << group columns("pollutants", :Ozone :: :Lead);

match_col = "X";
new_name = "coordinates";

groups_list = dt << get column groups names;

For Each({grp, ig}, groups_list,
	cols = Transform Each({colweirdness}, dt << get column group(grp), AsColumn(dt, colweirdness) << get name);
	For Each({colname, ic}, cols,
		If(colname == match_col,
			try(dt << rename column group(grp, new_name));
		)
	);	
);

 

-Jarmo

View solution in original post

1 REPLY 1
jthi
Super User

Re: Return the Name of a referenced column

Most likely you are running into issues as JMP is referring to wrong data table. My suggestion is to ALWAYS use explicit column references -> convert your column (references) always to strings and then use different formats as needed. In your case something like this

 

Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Cities.jmp");
dt_state = Open("$SAMPLE_DATA/State Abbreviations.jmp");

dt_state << New Column("Dummy", Numeric, "Continuous", Format("Best", 12));

dt_state:Abbreviations << Set Property("Link ID", 1);
dt:State << Set Property("Link Reference", Reference Table(dt_state));

dt << group columns("xy", {:X, :y});
dt << group columns("pollutants", :Ozone :: :Lead);

match_col = "X";
new_name = "coordinates";

groups_list = dt << get column groups names;

For Each({grp, ig}, groups_list,
	cols = Transform Each({colweirdness}, dt << get column group(grp), AsColumn(dt, colweirdness) << get name);
	For Each({colname, ic}, cols,
		If(colname == match_col,
			try(dt << rename column group(grp, new_name));
		)
	);	
);

 

-Jarmo

Recommended Articles