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

Loop to find max value for each unique values

Hi,

 

I am trying to iterate over each values of X and Y and save the max rn value in the MaxValue column. 

 

Here is what I tried but something doesn't seem correct. Any advice?

Names Default To Here( 1 );

dt = Current Data Table();

dt << begin data table();

// Loop through rows
For( i = 1, i <= N Rows( dt ), i++,
	currentBarcode = dt:Barcode[i];
   // Find the location of the maximum rn value for the current Barcode
	maxRNRow = Max( dt:rn[dt:Barcode[i] == currentBarcode] );
   // Check if the maxRNRow is not 0 (meaning there is at least one matching Barcode)
	If( maxRNRow != 0, 
       // Update "MaxValue" column with the max rn value for the current Barcode
		dt:MaxValue[i] = dt:rn[maxRNRow]
	);
);
dt << end data table();

Jackie__0-1702402546406.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
mmarchandTSI
Level V

Re: Loop to find max value for each unique values

Try this instead.

 

Col Max( :rn, :Y, :X )

 

edit

 

To be clearer, set the formula for :MaxValue to

 

Col Max( :rn, :Barcode )

 

View solution in original post

5 REPLIES 5
mmarchandTSI
Level V

Re: Loop to find max value for each unique values

Try this instead.

 

Col Max( :rn, :Y, :X )

 

edit

 

To be clearer, set the formula for :MaxValue to

 

Col Max( :rn, :Barcode )

 

Jackie_
Level VI

Re: Loop to find max value for each unique values

@mmarchandTSI Col max works only with :Barcode column. The issue is some table don't have Barcode column so am trying to see if I can use X and Y Coordinates to assign the max rn value in

jthi
Super User

Re: Loop to find max value for each unique values

You can add more than one byvar

Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
dt << New Column("Maximum Value for Each Age and Sex Group",
	Formula(Col Maximum(:height, :age, :sex))
);

jthi_0-1702578708979.png

Col Max() 

-Jarmo
mmarchandTSI
Level V

Re: Loop to find max value for each unique values

Right, so the first formula I posted will work for what you're doing, as @jthi alluded to.

jthi
Super User

Re: Loop to find max value for each unique values

No loops needed here. You can do what @mmarchandTSI suggested and use Col Max with byvar, you could use Summarize to calculate the maximum values and then create new column using those or you could create summary table and join (or rather update) that to your table with the max value. Also the the begin/end messages are

dt << Begin Data Update;
dt << End Data Update;
-Jarmo