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

Update Table Matching Columns Based on List

I'm using this simple update function to update a table based on the column matching below

 

dt << Update(
	With( dt_summary ),
	Match Columns( :DIE = :DIE, :RU = :RU, :NAME = :NAME, :TEST = :TEST )	
);

However, sometime the input table has DIE, RU, NAME or TEST columns missing, so I would like to update this column matching criteria depending on the input table. I tried several methods that didn't work, any suggestions? 

 

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Update Table Matching Columns Based on List

Here is a simple example of one way to handle this.  Using the Big Class data table, the code will work if the age column is present or not present

names default to here(1);
dt = 
// Open Data Table: Big Class.jmp
// → Data Table( "Big Class" )
Open( "$SAMPLE_DATA/Big Class.jmp" );

// Subset data table
// → Data Table( "Subset of Big Class" )
dt2 = dt << Subset( All rows, columns( :name, :age, :weight ) );

// Delete columns
dt << Delete Columns( :weight );

colList = {};
If( Try( dt:name << get name ) == "name" & Try( dt2:name << get name ) == "name",
	Insert Into( colList, Expr( :Name == :Name ) )
);
If( Try( dt:age << get name ) == "age" & Try( dt2:age << get name ) == "age",
	Insert Into( colList, Expr( :age == :age ) )
);


dt << Update(
	With( dt2 ),
	Match Columns( colList )
);
Jim

View solution in original post

4 REPLIES 4
mmarchandFSLR
Level VI

Re: Update Table Matching Columns Based on List

Here's an ugly solution.  I don't like using Eval( Parse(....) )

Names Default To Here( 1 );
update_exp = Expr(
	dt << Update( With( dt_summary ), _matchy_ )
);
match_exp = Expr( Match Columns() );
For Each( {v, i}, {"DIE", "RU", "NAME", "TEST"},
	If( dt_summary << Has Column( v ),
		Eval( Parse( "Insert Into( match_exp, Expr( \!"" || v || "\!" = \!"" || v || "\!" ) );" ) )
	)
);
Eval( Substitute( update_exp, Expr( _matchy_ ), Name Expr( match_exp ) ) );
txnelson
Super User

Re: Update Table Matching Columns Based on List

Here is a simple example of one way to handle this.  Using the Big Class data table, the code will work if the age column is present or not present

names default to here(1);
dt = 
// Open Data Table: Big Class.jmp
// → Data Table( "Big Class" )
Open( "$SAMPLE_DATA/Big Class.jmp" );

// Subset data table
// → Data Table( "Subset of Big Class" )
dt2 = dt << Subset( All rows, columns( :name, :age, :weight ) );

// Delete columns
dt << Delete Columns( :weight );

colList = {};
If( Try( dt:name << get name ) == "name" & Try( dt2:name << get name ) == "name",
	Insert Into( colList, Expr( :Name == :Name ) )
);
If( Try( dt:age << get name ) == "age" & Try( dt2:age << get name ) == "age",
	Insert Into( colList, Expr( :age == :age ) )
);


dt << Update(
	With( dt2 ),
	Match Columns( colList )
);
Jim

Re: Update Table Matching Columns Based on List

Thank you, this worked just as expected!

jthi
Super User

Re: Update Table Matching Columns Based on List

I haven't verified if this works correctly but you can have a list of "possible update columns" and compare that to both tables to build the final update list. This example requires JMP19 due to Set Intersection. In earlier versions of JMP you could use associative arrays instead

Names Default To Here(1);

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

dt2 << Delete Columns("sex");
dt2 << Delete Rows(1::40::2);


possible_updatecols = {"name", "age", "sex"};

dt1cols = dt1 << Get Column Names("String");
dt2cols = dt2 << Get Column Names("String");

updatecols = Set Intersection(possible_updatecols, dt1cols);
updatecols = Set Intersection(updatecols, dt2cols); 

dt1 << Update(
	With(dt2),
	Match(updatecols)
);

If the column names are always the same I think you can just use the list of columns with the Match.

-Jarmo

Recommended Articles