cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
The Discovery Summit 2025 Call for Content is open! Submit an abstract today to present at our premier analytics conference.
Choose Language Hide Translation Bar
Sysy13
Level II

Use for loop variable in a formula

Trying with this script but variable "l" is not working, only variable "k" is working. Anyway to solve this?

For( k = 40, k <= 54, k += 2,
	x1 = 1;
	x2 = 8;
	y1 = 40;
	y2 = 54;
	m = (y2 - y1) / (x2 - x1);
	c = y2 - (m * x1);
	l = (k - c) / m;
	dt << New Column( "Test" || Char( k ),
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Formula(
			If( :Strobe == 1,
				If(
					:Tested Variable == 0 | :Tested Plane == 1,
						If( Left( Right( :"x"n, Column[l] ) ), 1 ) == "1",
					1, Left( Right( :"x"n, (l) ), 1 ) == "0",
					0,
				), 

			)
		)
	);
)
1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Use for loop variable in a formula

The below script appears to work.  Your supplied script used \\ as comment initiators, where // is what the correct syntax is.

The real working formula really boiled down to using the Substr() function.

The addition of the Eval(Substitute(Expr())) section just changed the value of your l variable into the actual value of l, rather than having the value referenced through the variable.

Names Default To Here( 1 );
dt = Current Data Table();
//k start 40, For each loop will add 2
For( k = 40, k <= 54, k += 2,
	x1 = 1;
	x2 = 8;
	y1 = 40;
	y2 = 54;
	m = (y2 - y1) / (x2 - x1);
	c = y2 - (m * x2);
	l = (k - c) / m;
	
	//l is calculated based on k\
	//Tested variable & Column x & Strobe are the column in my data set
	//If k = 40;, then the readout will be 1st bit from the right of column x;
	//If k = 40;,then the readout will be 8th bit from the right of column x;
	
	// The variable l needs to be fully parsed in the formula, so that if the 
	// value of l changes, it does not change previous formulas
	Eval(
		Substitute(
				Expr(
					dt << New Column( "Test" || Char( k ),
						Numeric,
						"Continuous",
						Format( "Best", 12 ),
						Formula(
							theVal = .;
							If( :Strobe == 1,
								If( :Tested Variable == 0 | :Tested Variable == 1,
									theVal = Num( Substr( :x, _l_ , 1 ) )
								)
							);
							theVal;
						)
					)
				),
			Expr( _l_ ), l
		)
	);
);
Jim

View solution in original post

7 REPLIES 7
txnelson
Super User

Re: Use for loop variable in a formula

A couple of issues

The 

Column[ l ]

you specified is incorrect syntax.  Column is a function (), not a matrix [] therefore it needs to be specified as

Column( l )

Next, the formula will have an issue with the value you are assigning.  Given your formula, there are cases where no value will be assigned.  Since a JMP formula returns the last value assigned, you need to explicitly assign a value to the formula for every case.  In my version of your formula, I am using a variable  X to assign your logic to.  I initialize the value to a missing value, and then X is assigned by your If() logic.  I then assure that the calculated value of X is the last value processed, by simply adding 

x;

as the last statement in the formula.

For( k = 40, k <= 54, k += 2,
	x1 = 1;
	x2 = 8;
	y1 = 40;
	y2 = 54;
	m = (y2 - y1) / (x2 - x1);
	c = y2 - (m * x1);
	l = (k - c) / m;
	dt << New Column( "Test" || Char( k ),
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Formula(
			x = .;
			If( :Strobe == 1,
				If(
					:Tested Variable == 0 | :Tested Plane == 1,
						If( Left( Right( :"x"n, Column(l) ) ),
							1
						) == "1",
					x = 1, 
					Left( Right( :"x"n, (l) ), 1 ) == "0",
					x = 0,
				), 

			);
			x;
		)
	);
)

Finally, when entering JSL into a Discussion Question, please use the txnelson_0-1702341399738.png icon so the JSL is easier to read by the Community Members

 

Jim
Sysy13
Level II

Re: Use for loop variable in a formula

Ok sure sure, will use the icon for next time. Btw the script is not working. Maybe I should further elaborate what are the problems I faced

I have a column with 8bit binary number, for example x="00101101"

 

Then when my variable k is different, the bit read out from column x should be different

 

 

\\k start 40, For each loop will add 2
For( k = 40, k <= 54, k += 2,
	x1 = 1;
	x2 = 8;
	y1 = 40;
	y2 = 54;
	m = (y2 - y1) / (x2 - x1);
	c = y2 - (m * x2);
	l = (k - c) / m;
\\l is calculated based on k\
\\Tested variable & Column x & Strobe are the column in my data set
\\If k=40, then the readout will be 1st bit from the right of column x;
\\If k=40, then the readout will be 8th bit from the right of column x;
	dt << New Column( "Test" || Char( k ),
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Formula(
			If( :Strobe == 1,
				If(
					:Tested Variable == 0 | :Tested Variable == 1,
						If(  Right( :"x"n, Column(l) ) ) == "1",
					1, Right( :"x"n, (l) )== "0",
					0,
				), 

			)
		)
	);
)

The issue I faced is jmp cannot recognize what is the bit that I want to read out no matter I put Column(l), l  in the for loop

txnelson
Super User

Re: Use for loop variable in a formula

It would be helpful to if you could attach a sample data table.

With your further explanation, try changing 

If(  Right( :"x"n, Column(l) ) ) == "1"

to

If(  Right( :"x"n, as Column( "Column" || char(l) )== "1"
Jim
Sysy13
Level II

Re: Use for loop variable in a formula

Hi Jim, here's data table, I put what I expect to see in data table. Column data describe what is the "l" value and which value of "x" it is coming from

txnelson
Super User

Re: Use for loop variable in a formula

The below script appears to work.  Your supplied script used \\ as comment initiators, where // is what the correct syntax is.

The real working formula really boiled down to using the Substr() function.

The addition of the Eval(Substitute(Expr())) section just changed the value of your l variable into the actual value of l, rather than having the value referenced through the variable.

Names Default To Here( 1 );
dt = Current Data Table();
//k start 40, For each loop will add 2
For( k = 40, k <= 54, k += 2,
	x1 = 1;
	x2 = 8;
	y1 = 40;
	y2 = 54;
	m = (y2 - y1) / (x2 - x1);
	c = y2 - (m * x2);
	l = (k - c) / m;
	
	//l is calculated based on k\
	//Tested variable & Column x & Strobe are the column in my data set
	//If k = 40;, then the readout will be 1st bit from the right of column x;
	//If k = 40;,then the readout will be 8th bit from the right of column x;
	
	// The variable l needs to be fully parsed in the formula, so that if the 
	// value of l changes, it does not change previous formulas
	Eval(
		Substitute(
				Expr(
					dt << New Column( "Test" || Char( k ),
						Numeric,
						"Continuous",
						Format( "Best", 12 ),
						Formula(
							theVal = .;
							If( :Strobe == 1,
								If( :Tested Variable == 0 | :Tested Variable == 1,
									theVal = Num( Substr( :x, _l_ , 1 ) )
								)
							);
							theVal;
						)
					)
				),
			Expr( _l_ ), l
		)
	);
);
Jim
Sysy13
Level II

Re: Use for loop variable in a formula

Thx Jim, I think it is working but the print out will be slightly mismatch. It works well when adding ",1" behind  "_l_" in the formula. I attached my full jsl script here

Names Default To Here( 1 );
dt = Current Data Table();
//k start 40, For each loop will add 2
For( k = 40, k <= 54, k += 2,
	x1 = 1;
	x2 = 8;
	y1 = 40;
	y2 = 54;
	m = (y2 - y1) / (x2 - x1);
	c = y2 - (m * x2);
	l = (k - c) / m;
	
	//l is calculated based on k\
	//Tested variable & Column x & Strobe are the column in my data set
	//If k = 40;, then the readout will be 1st bit from the right of column x;
	//If k = 40;,then the readout will be 8th bit from the right of column x;
	
	// The variable l needs to be fully parsed in the formula, so that if the 
	// value of l changes, it does not change previous formulas
	Eval(
		Substitute(
				Expr(
					dt << New Column( "Test" || Char( k ),
						Numeric,
						"Continuous",
						Format( "Best", 12 ),
						Formula(
							theVal = .;
							If( :Strobe == 1,
								If( :Tested Variable == 0 | :Tested Variable == 1,
									theVal = Num( Substr( :x, _l_ ,1) )
								)
							);
							theVal;
						)
					)
				),
			Expr( _l_ ), l
		)
	);
);
txnelson
Super User

Re: Use for loop variable in a formula

You are correct, that is my error.  I will correct my entry.

Jim