BookmarkSubscribe
Choose Language Hide Translation Bar
kwmcm
Community Trekker

If/Then scripting

Hi group, I'm hoping someone out there can help!

 

I'm looking for a piece of coding to help with some if/then filtering. 

In the attached data table, there are various lot numbers with multiple SampleID's, of different "flavors".

 

The goal is this:

If the LOT# is the first instance, then it will be selected. If the next row is the same, it'll skip it, and continue skipping until it sees a new LOT#. 

That new first instance of the next LOT# will be selected. And continuing on. 

I'd then like those selected rows to be subsetted into a new data table. 

 

I'm hoping someone knows if this is possible. JMP can do anything, so I'm sure it is, I just don't know how. 

 

Thanks so much!

 

0 Kudos
2 ACCEPTED SOLUTIONS

Accepted Solutions
mwechtal
Community Trekker

Re: If/Then scripting

It first needs to be sorted by LOT#, but you have that. So the code is:

dt = Current Data Table();
dt << (select where( :LOT# != Lag( LOT# ) ));
dt << subset( selected rows( 1 ), selected columns only( 0 ) );

The Lag function looks at the value from the previous observation.

0 Kudos
txnelson
Super User

Re: If/Then scripting

If I am understanding your question, and if your lots are located all together, then this code will work

Names Default To Here( 1 );

dt = Current Data Table();
dt << select where( :name( "LOT#" )[Row()] != Lag( :name( "LOT#" )[Row()] ) );

dtNew = dt << subset( selected columns( 0 ), selected rows( 1 ) );
Jim
0 Kudos
4 REPLIES 4
mwechtal
Community Trekker

Re: If/Then scripting

It first needs to be sorted by LOT#, but you have that. So the code is:

dt = Current Data Table();
dt << (select where( :LOT# != Lag( LOT# ) ));
dt << subset( selected rows( 1 ), selected columns only( 0 ) );

The Lag function looks at the value from the previous observation.

0 Kudos
txnelson
Super User

Re: If/Then scripting

If I am understanding your question, and if your lots are located all together, then this code will work

Names Default To Here( 1 );

dt = Current Data Table();
dt << select where( :name( "LOT#" )[Row()] != Lag( :name( "LOT#" )[Row()] ) );

dtNew = dt << subset( selected columns( 0 ), selected rows( 1 ) );
Jim
0 Kudos
Highlighted
kwmcm
Community Trekker

Re: If/Then scripting

YES! this is exactly what I was looking for!

 

Thank you!

0 Kudos
uday_guntupalli
Community Trekker

Re: If/Then scripting

@kwmcm
       Maybe something like this 

dt = Current Data Table(); 

dt << Sort(By(:LOT#),Order(Ascending),Replace Table(1));

// Get Lot # 
LotNums = dt:LOT# << Get Values; 

// Get Unique Lot #
UniqueLotNums = Associative Array(LotNums) << Get Keys; 

// Define List and new column values
SaveList = []; 

// Find first occurence 
for(i = 1, i <= N Items(UniqueLotNums), i++,
		SR = dt << Select Where(:LOT# == UniqueLotNums[i]) << Get Selected Rows; 
		dt << Clear Select ; 
		SaveList = SaveList || SR[1]; 
   );

// New Column 
NewCol = As List(J(N Rows(dt),1,"0")); 
NewCol[SaveList,0] = LotNums[SaveList];
dt << New Column("test",character,nominal,<< Set values(NewCol));
Best
Uday
0 Kudos