cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
Pertussic
Level II

A Better Way to Do a For Loop with Two Lists?

Noob here.  I have a nested for loop that uses two lists that selects specific diagnostic codes from multiple diagnosis columns.  It works, but I was wondering if there's a better way to code it.

 

ColumnList = {"DIAGNOSIS_1", "DIAGNOSIS_2", "DIAGNOSIS_3"};
ValueList = {"E871","O480","J449"};*/
For( i = 1, i <= N Items( ColumnList ), i++, 
	ColumnItem = ColumnList[i];
	Eval(
		Eval Expr(
			As Constant( col = As Name( Expr( ColumnItem ) ) );
			For( j = 1, j <= N Items( ValueList ), j++, 
				ValueItem = ValueList[j];
				all_ip << select where( As Column( col ) == ValueItem, Current Selection( "extend" ) );
			);
		)
	);
);

----------
It’s not enough to know the results. You have to know what they mean.
1 ACCEPTED SOLUTION

Accepted Solutions
Craige_Hales
Super User

Re: A Better Way to Do a For Loop with Two Lists?

Maybe this is what you are trying to do.

 

dt = Open( "$sample_data/big class.jmp" );
ColumnList = {"height", "weight", "age"};
ValueList = {13, 128, 60};
For Each( {col}, ColumnList,
	For Each( {ValueItem}, ValueList,
		dt << select where( As Column( dt, col ) == ValueItem, Current Selection( "extend" ) )
	)
);

or even this

 

dt = Open( "$sample_data/big class.jmp" );
ColumnList = {:height, :weight, :age};
ValueList = {13, 128, 60};
For Each( {col}, ColumnList,
	For Each( {ValueItem}, ValueList, dt << select where( col == ValueItem, Current Selection( "extend" ) ) )
);

(It might make sense with your data, but with big class it doesn't make a lot of sense to select rows where height or weight or age are 13, or where height or weight or age are 128, or where height or weight or age are 60. Just using it to try to match what I think you mean.)

Craige

View solution in original post

8 REPLIES 8
Craige_Hales
Super User

Re: A Better Way to Do a For Loop with Two Lists?

Maybe this is what you are trying to do.

 

dt = Open( "$sample_data/big class.jmp" );
ColumnList = {"height", "weight", "age"};
ValueList = {13, 128, 60};
For Each( {col}, ColumnList,
	For Each( {ValueItem}, ValueList,
		dt << select where( As Column( dt, col ) == ValueItem, Current Selection( "extend" ) )
	)
);

or even this

 

dt = Open( "$sample_data/big class.jmp" );
ColumnList = {:height, :weight, :age};
ValueList = {13, 128, 60};
For Each( {col}, ColumnList,
	For Each( {ValueItem}, ValueList, dt << select where( col == ValueItem, Current Selection( "extend" ) ) )
);

(It might make sense with your data, but with big class it doesn't make a lot of sense to select rows where height or weight or age are 13, or where height or weight or age are 128, or where height or weight or age are 60. Just using it to try to match what I think you mean.)

Craige
Craige_Hales
Super User

Re: A Better Way to Do a For Loop with Two Lists?

Or even this

dt = Open( "$sample_data/big class.jmp" );
ValueList = {13, 128, 60};
dt << select where( Contains( ValueList, :height ) | Contains( ValueList, :weight ) | Contains( ValueList, :age ) );

Just imagine that height, weight, and age were all the same units, similar to the diagnosis columns. I think this is what you want if the number of diagnosis columns will always be three.

Craige
vince_faller
Super User (Alumni)

Re: A Better Way to Do a For Loop with Two Lists?

I'd guess you're right about that is what they want to do.  Adding onto it, even if it were dynamic you could do something like. 

 

Names default to here( 1 );
dt = open("$SAMPLE_DATA\Big Class.jmp");
col_dict = ["height"=>{13, 128, 60}, 
	"weight"=>{95, 123, 79, 105}, 
	"age"=>{17}
];
//create an orexpression 
or_expr = Expr(OR());
for(key = col_dict<<First, !isempty(key), key = col_dict<<next(key), 
	// for each column just add another contains(values, col) statement
	insert into(or_expr, EvalExpr(Contains(Expr(col_dict[key]), Column(Expr(key))[])));
);

final_expr = EvalExpr(dt << SelectWhere(Expr(nameexpr(or_expr))));
print(nameexpr(final_expr)); // just to show the final output it's actually running
Eval(final_expr);
Vince Faller - Predictum
Pertussic
Level II

Re: A Better Way to Do a For Loop with Two Lists?

Is For Each() only available in Pro?  I just have Basic and it doesn't work.  Or did I miss an update somewhere?

 

----------
It’s not enough to know the results. You have to know what they mean.
Craige_Hales
Super User

Re: A Better Way to Do a For Loop with Two Lists?

regular JMP, since 16.

Craige

Re: A Better Way to Do a For Loop with Two Lists?

Per Scripting Index in JMP 17: added in JMP 16 (JMP or JMP Pro)

 

for.PNG

Pertussic
Level II

Re: A Better Way to Do a For Loop with Two Lists?

And that'd do it.  I'm still at 15.  I could have sworn I updated.  COVID's kept me busy.

----------
It’s not enough to know the results. You have to know what they mean.
Pertussic
Level II

Re: A Better Way to Do a For Loop with Two Lists?

Sorry for the delay; I had to update to 17.  And your code works perfectly!  Thank you.

----------
It’s not enough to know the results. You have to know what they mean.