cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Register to attend Discovery Summit 2025 Online: Early Users Edition, Sept. 24-25.
  • New JMP features coming to desktops everywhere this September. Sign up to learn more at jmp.com/launch.
Choose Language Hide Translation Bar

Assign value to cell in For Each Row loop, after its already been assigned a value in the same loop

I have a For Each Row function working on a data table. It goes through a series of If statements evaluating values in some of the columns and then based on the findings it assigns a value to a :Correct_Process column.

 

Currently I have it setup like this.

For Each Row(
	dt, 
	If( :Module == "M05" & :Measurement == "Conductivity", 
		If(:Actual > m5_cond_hi | :Actual < m5_cond_low,
			:Correct_Process = "Incorrect",
			:Correct_Process = "Correct"
		);
		If( splits_detected == 1 & Contains( col_name_list, "PMC_FreeForm" ),
			If( :PMC_FreeForm == "DI" | Contains(:PMC_FreeForm, "Mod 5 pump off"), 
				If( :Actual != 0,
					:Correct_Process = "Incorrect",
					:Correct_Process = "Correct"
				);
			);
		);
		If( splits_detected == 1 & Contains( col_name_list, "CPMC_Conductivity" ), 
			num_cond = Num(:CPMC_Conductivity);
			lim_hi = num_cond + 0.6;
			lim_low = num_cond - 0.6;
			If( :Actual > lim_hi | :Actual < lim_low,
				show("Evaluated Incorrect");
				:Correct_Process = "Incorrect",
				show("Evaluated Correct");
				:Correct_Process = "Correct"
			);
		);

When I run the script, if the value of :CPMC_Conductivity is "13.2" and the :Actual is 13.5, the :Correct_Process column is still assigned a value of "Incorrect". Interestingly, the log shows "Evaluated Correct" due to the show() statement when I would expect it to which means the If statements are being evaluated correctly, but the :Correct_Process is not assigned "Correct".

I am assuming its because I am assigning the :Correct_Process column by default with the first nested If statement then trying to overwrite with a more specific check in the third nested if statement. But I have similar checks within this group after this one that seem to be able to successfully overwrite in other conditions (such as the second nested if statement) so I am wondering what I am missing here. 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Assign value to cell in For Each Row loop, after its already been assigned a value in the same loop

The 3 If() functions under the first If() function will be run independently from each other.  That is, each of them will be evaluated for each row, if the first If() function is True.

I would use the JSL debugger and step through the code looking for when the value is changing.  It will show you where the codes logic is not working the way you want it to.

 

If you can provide a sample data table, the Community might be able to dig deeper into the problem

Jim

View solution in original post

4 REPLIES 4
jthi
Super User

Re: Assign value to cell in For Each Row loop, after its already been assigned a value in the same loop

Is there a reason to overwrite same cell value multiple times within the loop?

-Jarmo
jthi
Super User

Re: Assign value to cell in For Each Row loop, after its already been assigned a value in the same loop

Also, can you provide example dataset?

 

Quickly checking this works fine

Names Default To Here(1); 

dt = open("$SAMPLE_DATA/Big Class.jmp");

For Each Row(dt,
	If(:height > 60,
		Write("\!N", Row(), " 1: If");
		:sex = "A";
	,
		Write("\!N", Row(), " 1: Else");
		:sex = "B";
	);
	If(:weight > 70,
		Write("\!N", Row(), " 2: If");
		:sex = "AA";
	,
		Write("\!N", Row(), " 2: Else");
		:sex = "BB";
	);	
);

You also might want to utilize << Begin Data Update and << End Data Update

 

-Jarmo

Re: Assign value to cell in For Each Row loop, after its already been assigned a value in the same loop

No, working from someone else's script and trying to restructure that but was confused how this result was coming about. Your question sparked an idea though and I dug deeper and found this part of the script is working as intended, it was just being overwritten again later on in the script. @txnelson I used to debugger to locate the third overwrite so thank you for the suggestion!

txnelson
Super User

Re: Assign value to cell in For Each Row loop, after its already been assigned a value in the same loop

The 3 If() functions under the first If() function will be run independently from each other.  That is, each of them will be evaluated for each row, if the first If() function is True.

I would use the JSL debugger and step through the code looking for when the value is changing.  It will show you where the codes logic is not working the way you want it to.

 

If you can provide a sample data table, the Community might be able to dig deeper into the problem

Jim

Recommended Articles