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%3EIterieren%20durch%20zwei%20Datentabellen%20unterschiedlicher%20Gr%C3%B6%C3%9Fe%20und%20Vergleichen%20nach%20%C3%BCbereinstimmenden%20Werten%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%3EIch%20habe%20folgendes%20Problem%3A%3C%2FP%3E%3CP%3E-%20Ich%20habe%20zwei%20Datentabellen%20in%20separaten%20JMP-Dateien%2C%20von%20denen%20jede%20eine%20Spalte%20hat.%3C%2FP%3E%3CP%3E-%20Eine%20Tabelle%20ist%208%20Zeilen%20lang%2C%20w%C3%A4hrend%20die%20andere%206440%20Zeilen%20lang%20ist.%3C%2FP%3E%3CP%3E-%20Mein%20Ziel%20ist%20es%2C%20jeden%20der%20Werte%20in%20der%208-Zeilen-Tabelle%20durchzugehen%20und%20sie%20mit%20jedem%20Wert%20in%20der%206440-Zeilen-Tabelle%20zu%20vergleichen%2C%20um%20zu%20sehen%2C%20ob%20sie%20%C3%BCbereinstimmen.%3C%2FP%3E%3CP%3E-%20Ich%20habe%20mich%20nur%20gefragt%2C%20wie%20ich%20das%20machen%20k%C3%B6nnte%2C%20weil%20ich%20beide%20Tabellen%20durchlaufen%20m%C3%B6chte%2C%20aber%20sie%20haben%20unterschiedliche%20Gr%C3%B6%C3%9Fen.%20Ich%20dachte%20daran%2C%20eine%20verschachtelte%20for-Schleife%20zu%20verwenden%2C%20bin%20mir%20aber%20nicht%20sicher.%3C%2FP%3E%3CP%3EJede%20Hilfe%20w%C3%A4re%20willkommen.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-60227%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EBetreff%3A%20Durch%20zwei%20Datentabellen%20unterschiedlicher%20Gr%C3%B6%C3%9Fe%20iterieren%20und%20auf%20%C3%BCbereinstimmende%20Werte%20vergleichen%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%3EIch%20habe%20in%20meinem%20vorherigen%20Pseudo-Codebeispiel%20einen%20kleinen%20Fehler%20gemacht%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%3EMuss%20in%20der%20Logik%20umgekehrt%20werden%2C%20damit%20es%20funktioniert%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%3EHier%20ist%20ein%20funktionierendes%20Beispiel%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%3EBetreff%3A%20Durch%20zwei%20Datentabellen%20unterschiedlicher%20Gr%C3%B6%C3%9Fe%20iterieren%20und%20auf%20%C3%BCbereinstimmende%20Werte%20vergleichen%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-60222%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EDanke%20f%C3%BCr%20die%20schnelle%20Antwort.%3C%2FP%3E%3CP%3EF%C3%BCr%20den%20folgenden%20Teil%3A%3C%2FP%3E%3CPRE%3EdtLong%20%3A%20Compare%20Column%20%3C%2FPRE%3E%3CP%3EK%C3%B6nnen%20Sie%20erkl%C3%A4ren%2C%20wie%20ich%20auf%20alle%20Werte%20in%20einer%20Spalte%20zugreifen%20kann%2C%20ohne%20auf%20jeden%20Index%2FIndex%20der%20Spalte%20verweisen%20zu%20m%C3%BCssen%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-59922%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EBetreff%3A%20Durch%20zwei%20Datentabellen%20unterschiedlicher%20Gr%C3%B6%C3%9Fe%20iterieren%20und%20auf%20%C3%BCbereinstimmende%20Werte%20vergleichen%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-59922%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3ESo%20w%C3%BCrde%20ich%20es%20verarbeiten.......%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%3EBetreff%3A%20Durch%20zwei%20Datentabellen%20unterschiedlicher%20Gr%C3%B6%C3%9Fe%20iterieren%20und%20auf%20%C3%BCbereinstimmende%20Werte%20vergleichen%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-59914%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EDanke%20f%C3%BCr%20deine%20Antwort.%3C%2FP%3E%3CP%3EWie%20w%C3%BCrden%20Sie%20vorschlagen%2C%20das%20Problem%20anzugehen%2C%20wenn%20ich%20%C3%BCberpr%C3%BCfen%20wollte%2C%20ob%20der%20Wert%20in%20der%208-Zeilen-Tabelle%20ist%3CEM%3E%20gr%C3%B6%C3%9Fer%3C%2FEM%3E%20als%20der%20Wert%20in%20der%206440-Zeilen-Tabelle%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%3EBetreff%3A%20Durch%20zwei%20Datentabellen%20unterschiedlicher%20Gr%C3%B6%C3%9Fe%20iterieren%20und%20auf%20%C3%BCbereinstimmende%20Werte%20vergleichen%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-59912%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EIch%20glaube%2C%20der%20einfachste%20Weg%2C%20dies%20zu%20tun%2C%20w%C3%A4re%2C%20JMP%20die%20Arbeit%20f%C3%BCr%20Sie%20erledigen%20zu%20lassen.Wenn%20Sie%20die%20Tabellen%20zu%20einer%20neuen%20Tabelle%20zusammenf%C3%BCgen%2C%20k%C3%B6nnen%20Sie%20JMP%20eine%20neue%20Spalte%20mit%20dem%20Namen%20Match%20flag%20erstellen%20lassen%2C%20die%20Ihnen%20mitteilt%2C%20ob%20eine%20%C3%9Cbereinstimmung%20gefunden%20wurde%20oder%20nicht%3C%2FP%3E%0A%3CP%3ETische%3D%3D%26gt%3BBeitreten%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