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
Gab_K
Level I

Script for multiple data tables/ Workflow

Hi everyone, 

 

I am relatively new to JMP and especially to scripting in JSL. I am looking for a way to apply a existing script to all currently open data tables (imported via multiple files import). Even though I researched through the abundance of community threads about this topic, I was not able to piece together a working solution.

 

Okay, so I have the following working JSL code (note: embedded in a workflow, if relevant):

// Specify the dataset and the suffix to search for in column headers
dt = Current Data Table();
// Define suffix of interest for replacement
targetSuffix = "TimeAbs"; 

// Get list of column names from the current data table
colNames = dt << Get Column Names( "string" );

// Loop through all column names and check if the target suffix is present
For( i = 1, i <= N Items( colNames ), i++,
    If( Contains( colNames[i], targetSuffix ),
        // If the target suffix is found, change the column header to "Date"
        Column( dt, colNames[i] ) << Set Name( "Date" )
    )
);

The current function of the workflow is the replacement of a column name in the currently open data table, however this name can vary between files, which is why I decided to only search for a fixed suffix. 

I would now like to expand this script to apply to multiple data tables I have currently open. I have seen solutions on similar tasks based on For or For Each Loops and also with table scripts, but as I said I am quite new to JSL and failed to succeed as of yet. 

Furthermore, this needs to be quite generic, because the file names and column names can vary quite drastically. 

The ultimate goal is then to concatenate these table, but might be out of scope for a single question. 

 

Thank you very much for any advice or ideas

1 REPLY 1
jthi
Super User

Re: Script for multiple data tables/ Workflow

You can get list of table with Get Data Table List() and then loop over that

Names Default To Here(1);

targetSuffix = "TimeAbs";

For Each({dt_cur}, Get Data Table List(),
	colNames = dt_cur << Get Column Names("String");
	For Each({colname}, colnames,
		If(Contains(colname, targetSuffix),
			Column(dt_cur, colname) << Set Name("Date")
		)
	);	
);

You could also convert the inside of your loop into a function or expression

Names Default To Here(1);

change_table_name = function({dt, targetsuffix = "TimeAbs", newname = "Date"}, {Default Local},
	colnames = dt << Get Column Names("String");
	For Each({colname}, colnames,
		If(Contains(colname, targetsuffix),
			Column(dt, colname) << Set Name(newname)
		)
	);
);

For Each({dt_cur}, Get Data Table List(),
	change_table_name(dt_cur);
);

-Jarmo