cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Discussions

Solve problems, and share tips and tricks with other JMP users.
%3CLINGO-SUB%20id%3D%22lingo-sub-59911%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EIterating%20Through%20two%20Data%20Tables%20of%20Different%20Sizes%20%26amp%3B%20Comparing%20for%20Matching%20Values%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-59911%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3EI%20have%20the%20following%20problem%3A%3C%2FP%3E%3CP%3E-%20I%20have%20two%20data%20tables%20in%20seperate%20jmp%20files%2C%20each%20of%20which%20has%20one%20column.%3C%2FP%3E%3CP%3E-%20One%20table%20is%208%20rows%20long%20whilst%20the%20other%20is%206440%20rows%20long.%3C%2FP%3E%3CP%3E-%20%26nbsp%3BMy%20aim%20is%20to%20go%20through%20each%20of%20the%20values%20in%20the%208-row%20table%20and%20compare%20them%20to%20every%20value%20in%20the%206440-row%20table%20to%20see%20if%20they%20match.%3C%2FP%3E%3CP%3E-%20I%20was%20just%20wondering%20how%20I%20could%20go%20about%20doing%20this%20because%20I%20want%20to%20iterate%20through%20both%20tables%20but%20they%20are%20different%20sizes.%20I%20was%20thinking%20of%20using%20a%20nested%20for%20loop%20but%20I'm%20not%20too%20sure.%3C%2FP%3E%3CP%3EAny%20help%20would%20be%20appreciated.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-60227%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Iterating%20Through%20two%20Data%20Tables%20of%20Different%20Sizes%20%26amp%3B%20Comparing%20for%20Matching%20Values%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-60227%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3E%3CSPAN%3EI%20made%20a%20slight%20error%20in%20my%20previous%20pseudo%20code%20example%2C%3C%2FSPAN%3E%3C%2FP%3E%0A%3CPRE%3EdtShort%3ACompare%20Column%5Bi%5D%20%26gt%3B%20dtLong%3ACompare%20Column%3CCODE%20class%3D%22%20language-jsl%22%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%3ENeeds%20to%20be%20reversed%20in%20logic%20for%20it%20to%20work%3C%2FSPAN%3E%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3E%20dtLong%3ACompare%20Column%20%26gt%3B%20dtShort%3ACompare%20Column%5Bi%5D%20%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%3EHere%20is%20a%20working%20example%3C%2FSPAN%3E%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3ENames%20Default%20To%20Here(%201%20)%3B%0AdtLong%20%3D%20Open(%20%22%24SAMPLE_DATA%2FBig%20Class.jmp%22%20)%3B%0A%2F%2F%20Create%20the%20short%20data%20table%0AdtShort%20%3D%20New%20Table(%20%22Short%22%2C%0A%20New%20Column(%20%22Target%20Age%22%2C%20values(%20%7B12%2C%2016%7D%20)%20)%2C%0A%20New%20Column(%20%22Match%22%2C%20character%2C%20values(%20%7B%22Teenager%22%2C%20%22Young%20Adult%22%7D%20)%20)%0A)%3B%0A%0A%2F%2F%20Sort%20the%20data%20for%20illustration%20of%20data%20not%20being%0A%2F%2F%20in%20age%20order%0AdtLong%20%26lt%3B%26lt%3B%20sort(%20by(%20%3AName%20)%2C%20replace%20table(%201%20)%20)%3B%0A%0A%2F%2F%20Create%20a%20couple%20of%20new%20columns%20in%20the%20data%20table%0AdtLong%20%26lt%3B%26lt%3B%20New%20Column(%20%22Kind%20Of%20Match%22%2C%20character%20)%3B%0AdtLong%20%26lt%3B%26lt%3B%20New%20Column(%20%22Count%22%20)%3B%0A%0A%2F%2F%20Process%20the%20data%0AFor(%20i%20%3D%201%2C%20i%20%26lt%3B%3D%20N%20Rows(%20dtShort%20)%2C%20i%2B%2B%2C%20%0A%20%2F%2F%20Find%20all%20rows%20that%20match%20the%20Where%20Clause%0A%20theRowsFound%20%3D%20dtLong%20%26lt%3B%26lt%3B%20get%20rows%20where(%20dtLong%3AAge%20%26gt%3B%20dtShort%3ATarget%20Age%5Bi%5D%20)%3B%0A%20%2F%2F%20If%20some%20rows%20were%20found%20process%0A%20If(%20N%20Rows(%20theRowsFound%20)%20%26gt%3B%200%2C%20%0A%20%20%2F%2F%20Set%20a%20value%20in%20the%20dtLong%20data%20table%20with%20one%20statement%0A%20%20%2F%2F%20that%20sets%20values%20in%20multiple%20rows%0A%20%20dtLong%3AKind%20Of%20Match%5BtheRowsFound%5D%20%3D%20dtShort%3AMatch%5Bi%5D%3B%0A%20%20%2F%2F%20Now%20set%20values%20row%20by%20row%20for%20individual%20rows%20that%20were%20found%0A%20%20theCount%20%3D%201%3B%0A%20%20For(%20k%20%3D%201%2C%20k%20%26lt%3B%3D%20N%20Items(%20theRowsFound%20)%2C%20K%2B%2B%2C%0A%20%20%20dtLong%3ACount%5BtheRowsFound%5Bk%5D%5D%20%3D%20theCount%3B%0A%20%20%20theCount%20%3D%20theCount%20%2B%201%3B%0A%20%20)%3B%0A%20)%3B%0A)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-60222%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Iterating%20Through%20two%20Data%20Tables%20of%20Different%20Sizes%20%26amp%3B%20Comparing%20for%20Matching%20Values%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-60222%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EThanks%20for%20the%20quick%20reply.%26nbsp%3B%3C%2FP%3E%3CP%3EFor%20the%20following%20part%3A%26nbsp%3B%3C%2FP%3E%3CPRE%3EdtLong%20%3A%20Compare%20Column%20%3C%2FPRE%3E%3CP%3EDo%20you%20mind%20explaining%20how%20I%20could%20access%20all%20the%20values%20in%20a%20column%20without%20having%20to%20reference%20to%20each%20subscript%2Findex%20of%20the%20column%3F%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-59922%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Iterating%20Through%20two%20Data%20Tables%20of%20Different%20Sizes%20%26amp%3B%20Comparing%20for%20Matching%20Values%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-59922%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EHere%20is%20how%20I%20would%20process%20it.......%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3E%2F%2F%20This%20is%20pseudo%20code%2C%20but%20it%20will%20give%20you%20an%20idea%20of%20the%20flow%0A%0AdtShort%20%3D%20Open(%20%22%3CYOUR%20short%3D%22%22%20data%3D%22https%3A%2F%2Fcommunity.jmp.com%2F%22%20table%3D%22%22%3E%22%20)%3B%0AdtLong%20%3D%20Open(%20%22%3CYOUR%20long%3D%22%22%20data%3D%22https%3A%2F%2Fcommunity.jmp.com%2F%22%20table%3D%22%22%3E%22%20)%3B%0A%0AFor(%20i%20%3D%201%2C%20i%20%26lt%3B%3D%20N%20Rows(%20dtShort%20)%2C%20i%2B%2B%2C%0A%20theRowsFound%20%3D%20dtLong%20%26lt%3B%26lt%3B%20get%20rows%20where(%20dtShort%3ACompare%20Column%5Bi%5D%20%26gt%3B%20dtLong%3ACompare%20Column%20)%3B%0A%20If(%20N%20Rows(%20theRowsFound%20)%20%26gt%3B%200%2C%20%0A%20%20%2F%2F%20Do%20your%20processing%0A%20)%3B%0A)%3B%3C%2FYOUR%3E%3C%2FYOUR%3E%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-59914%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Iterating%20Through%20two%20Data%20Tables%20of%20Different%20Sizes%20%26amp%3B%20Comparing%20for%20Matching%20Values%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-59914%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EThanks%20for%20your%20reply.%3C%2FP%3E%3CP%3EHow%20would%20you%20suggest%20going%20about%20the%20problem%20if%20I%20wanted%20to%20check%20if%20the%20value%20in%20the%208-row%20table%20is%26nbsp%3B%3CEM%3Egreater%20%3C%2FEM%3Ethan%20the%20value%20in%20the%206440-row%20table%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-59912%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Iterating%20Through%20two%20Data%20Tables%20of%20Different%20Sizes%20%26amp%3B%20Comparing%20for%20Matching%20Values%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-59912%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EI%20believe%20the%20easiest%20way%20to%20do%20this%20would%20be%20to%20let%20JMP%20do%20the%20work%20for%20you.%26nbsp%3B%20If%20you%20Join%20the%20tables%20into%20a%20new%20table%2C%20you%20can%20have%20JMP%20create%20a%20new%20column%20called%20Match%20flag%2C%20which%20will%20tell%20you%20if%20a%20match%20was%20found%20or%20not%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BTables%3D%3D%26gt%3BJoin%3C%2FP%3E%3C%2FLINGO-BODY%3E
Choose Language Hide Translation Bar
Jaz
Jaz
Level IV

Iterating Through two Data Tables of Different Sizes & Comparing for Matching Values

Hi,

I have the following problem:

- I have two data tables in seperate jmp files, each of which has one column.

- One table is 8 rows long whilst the other is 6440 rows long.

-  My aim is to go through each of the values in the 8-row table and compare them to every value in the 6440-row table to see if they match.

- I was just wondering how I could go about doing this because I want to iterate through both tables but they are different sizes. I was thinking of using a nested for loop but I'm not too sure.

Any help would be appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Iterating Through two Data Tables of Different Sizes & Comparing for Matching Values

I made a slight error in my previous pseudo code example,

dtShort:Compare Column[i] > dtLong:Compare Column

Needs to be reversed in logic for it to work

 dtLong:Compare Column > dtShort:Compare Column[i] 

Here is a working example

Names Default To Here( 1 );
dtLong = Open( "$SAMPLE_DATA/Big Class.jmp" );
// Create the short data table
dtShort = New Table( "Short",
	New Column( "Target Age", values( {12, 16} ) ),
	New Column( "Match", character, values( {"Teenager", "Young Adult"} ) )
);

// Sort the data for illustration of data not being
// in age order
dtLong << sort( by( :Name ), replace table( 1 ) );

// Create a couple of new columns in the data table
dtLong << New Column( "Kind Of Match", character );
dtLong << New Column( "Count" );

// Process the data
For( i = 1, i <= N Rows( dtShort ), i++, 
	// Find all rows that match the Where Clause
	theRowsFound = dtLong << get rows where( dtLong:Age > dtShort:Target Age[i] );
	// If some rows were found process
	If( N Rows( theRowsFound ) > 0, 
		// Set a value in the dtLong data table with one statement
		// that sets values in multiple rows
		dtLong:Kind Of Match[theRowsFound] = dtShort:Match[i];
		// Now set values row by row for individual rows that were found
		theCount = 1;
		For( k = 1, k <= N Items( theRowsFound ), K++,
			dtLong:Count[theRowsFound[k]] = theCount;
			theCount = theCount + 1;
		);
	);
);
Jim

View solution in original post

5 REPLIES 5
txnelson
Super User

Re: Iterating Through two Data Tables of Different Sizes & Comparing for Matching Values

I believe the easiest way to do this would be to let JMP do the work for you.  If you Join the tables into a new table, you can have JMP create a new column called Match flag, which will tell you if a match was found or not

     Tables==>Join

Jim
Jaz
Jaz
Level IV

Re: Iterating Through two Data Tables of Different Sizes & Comparing for Matching Values

Thanks for your reply.

How would you suggest going about the problem if I wanted to check if the value in the 8-row table is greater than the value in the 6440-row table?

txnelson
Super User

Re: Iterating Through two Data Tables of Different Sizes & Comparing for Matching Values

Here is how I would process it.......

// This is pseudo code, but it will give you an idea of the flow

dtShort = Open( "<your short data table>" );
dtLong = Open( "<your long data table>" );

For( i = 1, i <= N Rows( dtShort ), i++,
	theRowsFound = dtLong << get rows where( dtShort:Compare Column[i] > dtLong:Compare Column );
	If( N Rows( theRowsFound ) > 0, 
		// Do your processing
	);
);
Jim
Jaz
Jaz
Level IV

Re: Iterating Through two Data Tables of Different Sizes & Comparing for Matching Values

Thanks for the quick reply. 

For the following part: 

dtLong : Compare Column 

Do you mind explaining how I could access all the values in a column without having to reference to each subscript/index of the column? 

txnelson
Super User

Re: Iterating Through two Data Tables of Different Sizes & Comparing for Matching Values

I made a slight error in my previous pseudo code example,

dtShort:Compare Column[i] > dtLong:Compare Column

Needs to be reversed in logic for it to work

 dtLong:Compare Column > dtShort:Compare Column[i] 

Here is a working example

Names Default To Here( 1 );
dtLong = Open( "$SAMPLE_DATA/Big Class.jmp" );
// Create the short data table
dtShort = New Table( "Short",
	New Column( "Target Age", values( {12, 16} ) ),
	New Column( "Match", character, values( {"Teenager", "Young Adult"} ) )
);

// Sort the data for illustration of data not being
// in age order
dtLong << sort( by( :Name ), replace table( 1 ) );

// Create a couple of new columns in the data table
dtLong << New Column( "Kind Of Match", character );
dtLong << New Column( "Count" );

// Process the data
For( i = 1, i <= N Rows( dtShort ), i++, 
	// Find all rows that match the Where Clause
	theRowsFound = dtLong << get rows where( dtLong:Age > dtShort:Target Age[i] );
	// If some rows were found process
	If( N Rows( theRowsFound ) > 0, 
		// Set a value in the dtLong data table with one statement
		// that sets values in multiple rows
		dtLong:Kind Of Match[theRowsFound] = dtShort:Match[i];
		// Now set values row by row for individual rows that were found
		theCount = 1;
		For( k = 1, k <= N Items( theRowsFound ), K++,
			dtLong:Count[theRowsFound[k]] = theCount;
			theCount = theCount + 1;
		);
	);
);
Jim

Recommended Articles