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
CMG
CMG
Level II

How do I save a subset of tables only?

Let's say we have a table with 3 columns (example below). I create a subset of tables by the "city code" column that will include all 3 columns. 

 

City codeRegistration NumberRegistration Date
032032053764820201026
160160008408220201020
281282116256620201020
325325048850020201024
420420061884320201101
511511068017420201027
720720005473720200825

 

I would like to save only the subset files as text files as follows: city code value.txt

 

I found the following jsl script on the forum which was very helpful in saving the files as text files. The issue is that the files are saved as city code = value.txt

The main data table is also saved a text file, which I don't want.

 

 

//Save as txt file

openDTs = {};

For( i = 1, i <= N Table(), i++,

     Insert Into( openDTs, Data Table( i ) )

);

For( i = 1, i <= N Items( openDTs ), i++,

     one_name = openDTS[i] << get name;

     file_path = one_name || ".txt";

     openDTS[i] << Save(file_path);

);

 

I am new to both JMP and scripting, so any ideas are very much appreciated.

2 ACCEPTED SOLUTIONS

Accepted Solutions
txnelson
Super User

Re: How do I save a subset of tables only?

Below is a script that will create the Example data table you provided.  It will then create the subsets, and save each subset as a .txt file.

All that needs to be done to run this example, is to change the line of code

saveFolder = "C:\xxxxxxxxxxxxx\";

to what the actual path to the folder to save the files into, and then run the code

Names Default To Here( 1 );

// Create the example data table
New Table( "Example",
	Add Rows( 7 ),
	New Column( "City code", Numeric, "Continuous", Format( "Best", 12 ), Set Selected, Set Values( [32, 160, 281, 325, 420, 511, 720] ) ),
	New Column( "Registration Number",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [320537648, 1600084082, 2821162566, 3250488500, 4200618843, 5110680174, 7200054737] )
	),
	New Column( "Registration Date",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [20201026, 20201020, 20201020, 20201024, 20201101, 20201027, 20200825] )
	)
);


// Here is the real code.....just change the saveFolder =    to the actual folder you want to
// save the files to
dt = Current Data Table();

// Create the subsets
dtSub = dt << Subset( By( :City code ), 
	All rows, Selected columns only( 0 ), 
	columns( :Registration Number, :Registration Date ) );

saveFolder = "C:\xxxxxxxxxxxxx\";

For( i = 1, i <= N Items( dtSub ), i++,
	// Given the structure of the data tables created with a Subset using a By() clause
	//      City Code = 32
	// the Word() function retuns the "32" and it is added as the name of the file to save
	dtSub[i] << Save( saveFolder || Word( -1, dtSub[i] << get name, "=" ) || ".txt" )
);

Jim

View solution in original post

CMG
CMG
Level II

Re: How do I save a subset of tables only?

@txnelson Thank you so much! Worked like a charm.

View solution in original post

2 REPLIES 2
txnelson
Super User

Re: How do I save a subset of tables only?

Below is a script that will create the Example data table you provided.  It will then create the subsets, and save each subset as a .txt file.

All that needs to be done to run this example, is to change the line of code

saveFolder = "C:\xxxxxxxxxxxxx\";

to what the actual path to the folder to save the files into, and then run the code

Names Default To Here( 1 );

// Create the example data table
New Table( "Example",
	Add Rows( 7 ),
	New Column( "City code", Numeric, "Continuous", Format( "Best", 12 ), Set Selected, Set Values( [32, 160, 281, 325, 420, 511, 720] ) ),
	New Column( "Registration Number",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [320537648, 1600084082, 2821162566, 3250488500, 4200618843, 5110680174, 7200054737] )
	),
	New Column( "Registration Date",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [20201026, 20201020, 20201020, 20201024, 20201101, 20201027, 20200825] )
	)
);


// Here is the real code.....just change the saveFolder =    to the actual folder you want to
// save the files to
dt = Current Data Table();

// Create the subsets
dtSub = dt << Subset( By( :City code ), 
	All rows, Selected columns only( 0 ), 
	columns( :Registration Number, :Registration Date ) );

saveFolder = "C:\xxxxxxxxxxxxx\";

For( i = 1, i <= N Items( dtSub ), i++,
	// Given the structure of the data tables created with a Subset using a By() clause
	//      City Code = 32
	// the Word() function retuns the "32" and it is added as the name of the file to save
	dtSub[i] << Save( saveFolder || Word( -1, dtSub[i] << get name, "=" ) || ".txt" )
);

Jim
CMG
CMG
Level II

Re: How do I save a subset of tables only?

@txnelson Thank you so much! Worked like a charm.