BookmarkSubscribeRSS Feed
Choose Language Hide Translation Bar
Highlighted
awoj

New Contributor

Joined:

Mar 20, 2019

JSL won't set column Format

Hi all,

I'm trying to copy the column names, data types, modeling types and formats from one table into a new one, but when I try to set the column format, it never takes and just reverts to "Best". I've been banging my head against this for a while and am completely stumped why it's not working. Below is my code and the resulting output that shows the column format is valid but just doesn't stick. There are no errors reported in the log. Can anyone see what I'm doing wrong? 

 

summaryTable = New Table( "Results Summary" );
colNames = data << Get Column Names( string );
For( i = 1, i <= N Items( colNames ), i++,
	colType = Column( data, colNames[i] ) << Get Data Type;
	colModel = Column( data, colNames[i] ) << Get Modeling Type;
	colFormat = Column( data, colNames[i] ) << Get Format;

	Show( colNames[i] );
	Show( colType );
	Show( colModel );
	Show( colFormat );

	newCol = summaryTable << New Column( colNames[i] );
	newCol << Data Type( colType ) << Modeling Type( colModel );
	If( colType != "Character", 
		newCol << Format( colFormat );
		Show( newCol << Get Format ); 
	);
);
colNames[i] = "Label";
colType = "Character";
colModel = "Nominal";
colFormat = Format("Best");

colNames[i] = "Start_Time";
colType = "Numeric";
colModel = "Continuous";
colFormat = Format("m/d/y h:m:s", 23, 0);
newCol << Get Format = Format("Best", 10);

colNames[i] = "End_Time";
colType = "Numeric";
colModel = "Continuous";
colFormat = Format("m/d/y h:m:s", 23, 0);
newCol << Get Format = Format("Best", 10);

Bonus follow-up question: I can only seem to set the column data and modeling types using the above code where I set the types after the column is initialized. When I try to execute the below line the columns simply don't initialize, why not?

newCol = summaryTable << New Column( colNames[i], colType, colModel );

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
pmroz

Super User

Joined:

Jun 23, 2011

Solution

Re: JSL won't set column Format

This code will do it:

data = New Table( "Test Data", Add Rows( 3 ),
	New Column( "Column 1", Numeric, "Continuous", Format( "Fixed Dec", 8, 0 ),
		Set Values( [1, 2, 3] )
	),
	New Column( "Column 2", Character, "Nominal", Set Values( {"c", "b", "a"} ) ),
	New Column( "Column 3", Numeric, "Continuous", Format( "m/d/y", 10 ),
		Input Format( "m/d/y" ),
		Set Values( [3630873600, 3633552000, 3635971200] )
	)
);

summaryTable = New Table( "Results Summary" );
colNames = data << Get Column Names( string );
For( i = 1, i <= N Items( colNames ), i++,
	colType = Column( data, colNames[i] ) << Get Data Type;
	colModel = Column( data, colNames[i] ) << Get Modeling Type;
	colFormat = char(Column( data, colNames[i] ) << Get Format);

	Show( colNames[i] );
	Show( colType );
	Show( colModel );
	Show( colFormat );

	newCol = summaryTable << New Column( colNames[i] );
	newCol << Data Type( colType ) << Modeling Type( colModel );

	if (coltype != "Character",
		new_expr = "newcol << " || colformat;
		eval(parse(new_expr));
	);
);
7 REPLIES 7
guillaumebugnon

Community Trekker

Joined:

Apr 7, 2015

Re: JSL won't set column Format

Hi,


First, I think that "data" is not definied.

 

 

Clear Globals();

summaryTable = New Table( "Results Summary" );
colNames = data << Get Column Names( string );

Result is 

 

Data Table( "Results Summary" )
//:*/
colNames = data << Get Column Names( string );
/*:

Send Expects Scriptable Object in access or evaluation of 'Send' , data <<  /*###*/Get Column Names( string ) /*###*/

In the following script, error marked by /*###*/
colNames = data <<  /*###*/Get Column Names( string ) /*###*/
{}
markbailey

Staff

Joined:

Jun 23, 2011

Re: JSL won't set column Format

Would it be easier to save a copy of the original data table and open the copy to continue?

Learn it once, use it forever!
pmroz

Super User

Joined:

Jun 23, 2011

Solution

Re: JSL won't set column Format

This code will do it:

data = New Table( "Test Data", Add Rows( 3 ),
	New Column( "Column 1", Numeric, "Continuous", Format( "Fixed Dec", 8, 0 ),
		Set Values( [1, 2, 3] )
	),
	New Column( "Column 2", Character, "Nominal", Set Values( {"c", "b", "a"} ) ),
	New Column( "Column 3", Numeric, "Continuous", Format( "m/d/y", 10 ),
		Input Format( "m/d/y" ),
		Set Values( [3630873600, 3633552000, 3635971200] )
	)
);

summaryTable = New Table( "Results Summary" );
colNames = data << Get Column Names( string );
For( i = 1, i <= N Items( colNames ), i++,
	colType = Column( data, colNames[i] ) << Get Data Type;
	colModel = Column( data, colNames[i] ) << Get Modeling Type;
	colFormat = char(Column( data, colNames[i] ) << Get Format);

	Show( colNames[i] );
	Show( colType );
	Show( colModel );
	Show( colFormat );

	newCol = summaryTable << New Column( colNames[i] );
	newCol << Data Type( colType ) << Modeling Type( colModel );

	if (coltype != "Character",
		new_expr = "newcol << " || colformat;
		eval(parse(new_expr));
	);
);
awoj

New Contributor

Joined:

Mar 20, 2019

Re: JSL won't set column Format

Thanks for the replies. I guess I failed to mention that "data" is an already existing table created from multiple SQL queries and calculations. I would like to assemble a summary table with the same columns as "data" without actually subsetting it. I later fill the rows in a piecewise fashion.

 

@pmroz Your approach works, thanks! I still don't understand why the pointer doesn't work, but I'm so over this I don't care anymore.

 

pmroz

Super User

Joined:

Jun 23, 2011

Re: JSL won't set column Format

Simpler logic that uses subset:

data = New Table( "Test Data", Add Rows( 3 ),
	New Column( "Column 1", Numeric, "Continuous", Format( "Fixed Dec", 8, 0 ),
		Set Values( [1, 2, 3] )
	),
	New Column( "Column 2", Character, "Nominal", Set Values( {"c", "b", "a"} ) ),
	New Column( "Column 3", Numeric, "Continuous", Format( "m/d/y", 10 ),
		Input Format( "m/d/y" ),
		Set Values( [3630873600, 3633552000, 3635971200] )
	)
);
summaryTable = data << Subset( Rows( [1] ));
summarytable << delete rows([1]) << set name( "Results Summary");
ian_jmp

Staff

Joined:

Jun 23, 2011

Re: JSL won't set column Format

Assuming the table isn't huge, and building on Mark's thought, you could alternatively do it this way:

NamesDefaultToHere(1);

dt = Open("$SAMPLE_DATA/Big Class.jmp");
dt2 = Eval(dt << getScript);
dt2 << selectAllRows;
dt2 << deleteRows;
awoj

New Contributor

Joined:

Mar 20, 2019

Re: JSL won't set column Format

Thanks all. I considered copying the table and deleting rows. The data table consists of about 2 dozen columns and can contain potentially hundreds of rows. I figured creating 2 dozen columns iteratively might be more efficient than potentially deleting hundreds of rows. @pmroz's subset approach is clever though, I might just use it.