cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
Choose Language Hide Translation Bar

Compare Rows by Group

Hi,

I'm trying to write a script comparing rows by group but am having trouble figuring out how to do this    In the table below, I'm trying to select rows that meet both of the following conditions within a group (i.e. by "Subject Number" column):

  • Value in "Target # Days from Previous" column is greater than the row with a value of zero (in example below highlighted rows 71 through 82 have values greater than value for row 83)
  • Value in "Visit Sequence" column is less than the value in this column for the row where "Target # Days from Previous" is equal to zero (in example below highlighted rows 71 though 82 have values less than value for row 83)

Basically I'm trying to compare rows within a group with another row within the same group and do this for the entire table.  Does anyone know how I can do this?

10925_pastedImage_4.png

Thanks,

Christine

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Compare Rows by Group

Christine,

Here is a script that does what you are asking for.  I tested it on the small snippet of data you provided, and it appears to accomplish what you want. 

Names Default To Here( 1 ); 

dt = Current Data Table();

// Create a New column to track found groups

dt << New Column( "Grouping", set each value( . ) );

countGroup = 0;

// Step through all data 

For( i = 1, i <= N Rows( dt ) - 1, i++,

       // Look ahead to see if triger criteria has been met

       // and if it has, then read backwards until it fails 

       If( dt:Name( "Target # Days from Previous" ) > dt:Name( "Target # Days from Previous" )[i + 1] & dt:Visit Sequence < dt :Visit Sequence[i + 1],

              // Trigger has been found, so read backwards until not true

       countGroup++;

       compareStart = i;

              While( dt:Name( "Target # Days from Previous" )[compareStart] > dt:Name( "Target # Days from Previous" )[i + 1] & dt:Visit Sequence[ compareStart] < dt:Visit Sequence[i + 1],

          dt:Grouping[compareStart] = countGroup;

                    Selected( Row State( compareStart ) ) = 1;

          compareStart--;

       );

    )

);

 

Jim

View solution in original post

3 REPLIES 3
ian_jmp
Staff

Re: Compare Rows by Group

Not sure I have understood what you are looking for.

Can 'Target # Days from Previous' be an integer less than zero? If not, then don't all rows for a given subject apart from one satisfy your first condition?


Maybe you can post a table with the three columns you need to manipulate (Subject, Target and Visit Sequence? Use the 'Advanced Editor' then 'Attach'.

txnelson
Super User

Re: Compare Rows by Group

Christine,

Here is a script that does what you are asking for.  I tested it on the small snippet of data you provided, and it appears to accomplish what you want. 

Names Default To Here( 1 ); 

dt = Current Data Table();

// Create a New column to track found groups

dt << New Column( "Grouping", set each value( . ) );

countGroup = 0;

// Step through all data 

For( i = 1, i <= N Rows( dt ) - 1, i++,

       // Look ahead to see if triger criteria has been met

       // and if it has, then read backwards until it fails 

       If( dt:Name( "Target # Days from Previous" ) > dt:Name( "Target # Days from Previous" )[i + 1] & dt:Visit Sequence < dt :Visit Sequence[i + 1],

              // Trigger has been found, so read backwards until not true

       countGroup++;

       compareStart = i;

              While( dt:Name( "Target # Days from Previous" )[compareStart] > dt:Name( "Target # Days from Previous" )[i + 1] & dt:Visit Sequence[ compareStart] < dt:Visit Sequence[i + 1],

          dt:Grouping[compareStart] = countGroup;

                    Selected( Row State( compareStart ) ) = 1;

          compareStart--;

       );

    )

);

 

Jim
Ky_Stone
Level I

Re: Compare Rows by Group

Another approach is to simply create a new variable and recode by the groups that you want.