Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- JMP User Community
- :
- Discussions
- :
- Discussions
- :
- If/Then scripting

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Dec 7, 2018 6:37 AM
(719 views)

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!

2 ACCEPTED SOLUTIONS

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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

4 REPLIES 4

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: If/Then scripting

YES! this is exactly what I was looking for!

Thank you!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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

Uday