cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Register for our Discovery Summit 2024 conference, Oct. 21-24, where you’ll learn, connect, and be inspired.
Choose Language Hide Translation Bar
UserID16644
Level V

How to create a loop and add column in subset tables and add value based on specific column value?

Hi all, I have a table subset

I have two questions.

1. How can create a loop that adds column "Ref" to every subset table? 

2. Also, is it possible that the value of the created column will be based on the highest number of N Rows with a specific value?

 

For example, the new column to be added will be Ref. Column Ref's value will be based on Column Country, wherein it should add 1 to the highest N Rows Value of America only.

CountryN Rows Ref
Argentina4 
America0 
America51
Brazil2 
Brazil4 
Canada2 

 

TIA

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: How to add column in subset tables and add value based on specific column value?

Here is an example of one way to do the looping.  The real issue is how to get the list of tables to loop across.  This example assumes that 

     Tables=>Subset

with a Subset By was used

names default to here(1);
dt =
// Open Data Table: Big Class.jmp
// → Data Table( "Big Class" )
Open( "$SAMPLE_DATA/Big Class.jmp" );

dtSubsetList = dt << Subset(
	By( :sex ),
	All rows,
	Selected columns only( 0 ),
	columns( :name, :age, :height, :weight )
);

for each( {table}, dtSubsetList,
	table << new column( "Ref");
	wait(0);
	table:ref[table << get rows where( :age == col max(:age))]=1;
);
Jim

View solution in original post

5 REPLIES 5
txnelson
Super User

Re: How to add column in subset tables and add value based on specific column value?

I am not sure what your definition of as "column subset" is.  Is what you are referring to, the data tables created from

     Tables=>Subset

when  Subset By is checked

txnelson_0-1722993661568.png

 

and a separate table for each level of the variable(s) selected is made?

Jim
UserID16644
Level V

Re: How to add column in subset tables and add value based on specific column value?

After I have subset the table, how can I create a loop to add a column "Ref" on all the subsetted tables? 

 

*Will rephrase the question

txnelson
Super User

Re: How to add column in subset tables and add value based on specific column value?

Here is an example of one way to do the looping.  The real issue is how to get the list of tables to loop across.  This example assumes that 

     Tables=>Subset

with a Subset By was used

names default to here(1);
dt =
// Open Data Table: Big Class.jmp
// → Data Table( "Big Class" )
Open( "$SAMPLE_DATA/Big Class.jmp" );

dtSubsetList = dt << Subset(
	By( :sex ),
	All rows,
	Selected columns only( 0 ),
	columns( :name, :age, :height, :weight )
);

for each( {table}, dtSubsetList,
	table << new column( "Ref");
	wait(0);
	table:ref[table << get rows where( :age == col max(:age))]=1;
);
Jim
UserID16644
Level V

Re: How to add column in subset tables and add value based on specific column value?

What if my data looks like this? The column Country is in character type, how can I get its highest N Rows? Assuming that I am getting the highest N Rows from America. From the example you have given, I think it is in numeric data type

CountryN Rows Ref
Argentina4 
America0 
America51
Brazil2 
Brazil4 
Canada2 
txnelson
Super User

Re: How to add column in subset tables and add value based on specific column value?

I assume something similar to this will be what you need

table:ref[table << get rows where( :Country == "America" & :N Rows == col max(:N Rows, :Country))]=1;

I suggest you take the time to read the Scripting Guide, to learn JSL.  If you are not familiar with programming and at least one programming language, I suggest you search the internet for a free course on "Learn how to Program".  

Jim