cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar

How can I SUM specific columns in a Data Table if they exist?

I am trying to write a script to sum specific columns if they are present in the data table. Lets call them Col A, Col B, Col C, Col D. The problem is the data table does not always have all four columns which results in an error. I would like to write a script that can add Col A, Col B, Col C, and Col D that are present in the data table. Obviously I could just update the code for the ones present each time but I'm trying to find an automated way that can handle the various data tables without erroring.

 

Thanks!

2 REPLIES 2
StarfruitBob
Level VI

Re: How can I SUM specific columns in a Data Table if they exist?

Hello @DegreesOfGen19,

 

Is this similar to what you're looking for?

 

Names default to here(1);

dt = current data table();

// your columns of interest
col_list = { "Col A", "Col B", "Col C", "Col D" };

// creates list of column names from the target data table
dt_col_list = dt << Get column names( string );

for( i = 1, i <= N Items( col_list ), i++,
	if( contains( dt_col_list, col_list[i] ),
			mysum = colsum( column( col_list[i] ) );
			show( mysum )		
	)
);

 

Edit: changed dt << Get column names; to dt << Get column names( string );  This makes the column names returned each a string, which is what the list of user-defined columns are.  My bad for not catching that initially.

Learning every day!
jthi
Super User

Re: How can I SUM specific columns in a Data Table if they exist?

Below are few examples what you could do. There are more simple (easier to understand) looking ways of filtering the found columns, but associative arrays are good for this type of use case

Names Default To Here(1);

dt = New Table("Untitled 3",
	Add Rows(1),
	Compress File When Saved(1),
	New Column("Column 1", Numeric, "Continuous", Set Values([1,2,3,.])),
	New Column("Column 2", Numeric, "Continuous", Set Values([2,3,4,5])),
	New Column("Column 3", Numeric, "Continuous", Set Values([3,4,5,.]))
);

col_list = {"Column 1", "Column 2", "Column 3", "Column 4"};
table_col_list = dt << Get Column Names("String");

// keep only columns which are found from data table
cols_to_keep = Associative Array(col_list);
cols_to_keep << Intersect(Associative Array(table_col_list));
cols_to_keep = cols_to_keep << get keys;

// if you don't need a formula, data table subscripting is quite easy solution
dt << New Column("SumForCols", Numeric, Continuous, << Set Each Value(
	Sum(dt[Row(), cols_to_keep]);
));

// if you do need a formula, you can build one with expressions (requires JMP16+ due to For Each usage)
// https://community.jmp.com/t5/JSL-Cookbook/Insert-one-expression-into-another-using-Eval-Insert-Eval-Expr/ta-p/48998
f_expr = Expr(Sum());
For Each({col_name}, cols_to_keep,
	Insert Into(f_expr, Name Expr(AsColumn(col_name)));
);
show(name expr(f_expr));
Eval(EvalExpr(
	dt << New Column("SumForColsFormula", Numeric, Continuous, Formula(
		Expr(name expr(f_expr));
	))	
));

First I calculate which columns exist in the data table from the wanted list by using associative arrays and intersect. Then there are two options, using << set each value and data table subscripting to calculate the sum or building formula expression and using that

-Jarmo