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
Raquel
Level III

Apply script to multiple data tables

I have a script where I open a txt file in JMP as a data table and then move the column names listed in row 7 to the column headers. How can I apply this to multiple data tables? My current script is not working. I have JMP16. Thanks.

 
dt_list = Multiple File Import(); //additional known text here to open files

table_script = expr( 
//Move the column names listed in row 7 to the column headers
dt = Current Data Table();
dt << clear select;
dt << Clear Column Selection();
lastcol = N Col( dt );
header = 7;
  For( i = 1, i <= lastcol, i++,
   Column( i ) << set Name( Column(i)[header] )
  );
 );
 
Eval( 
	Eval Expr(
		For Each( {value, index},
			Expr( dt_list[index] << table_script )
		)
	)
);
1 ACCEPTED SOLUTION

Accepted Solutions
Raquel
Level III

Re: Apply script to multiple data tables

Thank you so much! Your new script worked to make edits to all data tables that were opened. However, I found that my script was not operating as intended and was attaching a "7" to all column headers instead of moving row 7 to being the column headers. It previously worked on a single data table, but in this case, I needed to change my code to actually read the table value in the specified location. Here is my working code:

 

Names Default To Here( 1 );
dt_list = Multiple File Import();

table_script = Expr( 
//Move the column names listed in row 7 to the column headers
	dt = Current Data Table();
	dt << clear select;
	dt << Clear Column Selection();
	lastcol = N Col( dt );
	header = 7;
	For( i = 1, i <= lastcol, i++,
	tableVal = Column( Current Data Table(), i)[ header ];
		Column( i ) << set Name( tableVal )
	);
);
 
For Each( {table}, dt_list,
	Current Data Table( table );
	table_script;
);

 

 

View solution in original post

4 REPLIES 4
txnelson
Super User

Re: Apply script to multiple data tables

Here is how I would set it up

Names Default To Here( 1 );
dt_list = Multiple File Import();

table_script = Function( {dt},
	{defaultlocal}, 
//Move the column names listed in row 7 to the column headers

	dt << clear select;
	dt << Clear Column Selection();
	lastcol = N Col( dt );
	header = 7;
	For( i = 1, i <= lastcol, i++,
		Column( i ) << set Name( Column( i )[header] )
	);
);
 
For Each( {value}, dt_list, table_script( value ) );
Jim
Raquel
Level III

Re: Apply script to multiple data tables

Thanks for your reply! I tried your script, but it is only running the table_script function on the last data table that is opened. Do you have any idea what might be the problem? Thank you.

txnelson
Super User

Re: Apply script to multiple data tables

I replicated the finding that you indicated, and I was not able to correct the issue using the approach that I was taking.  So I went back to your original code, and simplified the approach and was successful in running a complete example.

Names Default To Here( 1 );

rc = Create Directory( "$TEMP/sub1" );

dt = Open( "$SAMPLE_DATA/big class.jmp" );
Close( dt, save( "$TEMP/sub1/MFI big class.jmp" ) );
dt = Open( "$SAMPLE_DATA/body fat.jmp" );
Close( dt, save( "$TEMP/sub1/MFI body fat.jmp" ) );
dt = Open( "$SAMPLE_DATA/candy bars.jmp" );
Close( dt, save( "$TEMP/sub1/MFI candy bars.jmp" ) );

Show( Files In Directory( "$TEMP/sub1" ) );

dt_list = Multiple File Import(
	<<Set Folder( "$TEMP\sub1\" ),
	<<Set Stack Mode( "Table Per File" )
) << Import Data;


table_script = Expr( 
//Move the column names listed in row 7 to the column headers
	dt = Current Data Table();
	dt << clear select;
	dt << Clear Column Selection();
	lastcol = N Col( dt );
	header = 7;
	For( i = 1, i <= lastcol, i++,
		Column( i ) << set Name( (Column( i ) << get name) || Char( header ) )
	);
);
 
For Each( {table}, dt_list,
	Current Data Table( table );
	table_script;
);
Jim
Raquel
Level III

Re: Apply script to multiple data tables

Thank you so much! Your new script worked to make edits to all data tables that were opened. However, I found that my script was not operating as intended and was attaching a "7" to all column headers instead of moving row 7 to being the column headers. It previously worked on a single data table, but in this case, I needed to change my code to actually read the table value in the specified location. Here is my working code:

 

Names Default To Here( 1 );
dt_list = Multiple File Import();

table_script = Expr( 
//Move the column names listed in row 7 to the column headers
	dt = Current Data Table();
	dt << clear select;
	dt << Clear Column Selection();
	lastcol = N Col( dt );
	header = 7;
	For( i = 1, i <= lastcol, i++,
	tableVal = Column( Current Data Table(), i)[ header ];
		Column( i ) << set Name( tableVal )
	);
);
 
For Each( {table}, dt_list,
	Current Data Table( table );
	table_script;
);