cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Don’t miss your chance to experience Discovery Summit Europe at our best available rate! Early bird registration through 31 Oct.
  • Graph Builder Toolbar streamlines interactive graphing. Creates shortcuts on Report and GraphBuilder Helpers toolbars. Download and install the extension.
  • JMP will suspend normal business operations for our Rest and Recharge Day on Friday, October 2, 2026.
    Regular business hours will resume on Monday, October 5, 2026.

Discussions

Solve problems, and share tips and tricks with other JMP users.
Choose Language Hide Translation Bar
rk2
rk2
Level I

How to create a new column in a data table from a list of column values using conditional statements?

I want to check values in two numeric columns within a data table. Now I have these two numeric columns' names in a list. I use a for loop to iterate through the list of these chosen numeric column names, then i want to check against a threshold value (say 100). Then I want to create a new column and put labels accordingly - either label "A" or "B" - depending on the given columns in the data table.

 

Let's say these are the two numeric columns:

numeric_col_1 = {100, 200, 250, 700}

numeric_col_2 = {200, 250, 500, 100}

 

And the threshold is 100.

Now the labeled columns pseudocode I want to implement is as follows:

labeled_col = (if numeric_col > 100, "A", "B");

 

So the labeled columns will now show:

labeled_col_1 = {"B", "A", "A", "A"};

labeled_col_2 = {"A", "A", "A", "B"};

 

Here is some example code:

 

concat_dt = datatable("concatenated");
new_val_list = {"num_col1", "num_col2"};
name_list = {"col1", "col2"};
stop = nitems(new_val_list); for (i = 1, i <=stop, i++, col_name = name_list[i] || "_Label_A_B"; show(col_name); concat_dt << New Column( col_name, Character, "Nominal", Formula( If( Column(concat_dt, new_val_list[i]) > 200, "A", "B" ) ) ) );

But the above code only shows empty columns even though they have been created with their respective column names in the data table.

 

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: How to create a new column in a data table from a list of column values using conditional statements?

Use As Column() instead of Column()

concat_dt = datatable("concatenated");new_val_list = {"num_col1", "num_col2"};name_list = {"col1", "col2"};stop = nitems(new_val_list);
for (i = 1, i <=stop, i++,
	col_name = name_list[i] || "_Label_A_B";
	show(col_name);
	concat_dt << New Column( col_name,
		Character,
		"Nominal",
		Formula( If( As Column(concat_dt, new_val_list[i]) > 200, "A", "B" ) )
	)	
);

However, I believe this is a better solution

Names Default To Here( 1 );
concat_dt = Data Table( "concatenated" );
name_list = {"col1", "col2"};

For Each( {col, i}, name_list,
	concat_dt << New Column( "num_" || col || "_Label_A_B" ,
		Character,
		"Nominal",
		Set Each Value( If( As Column( concat_dt, col ) > 200, "A", "B" ) )
	);
);
Jim

View solution in original post

1 REPLY 1
txnelson
Super User

Re: How to create a new column in a data table from a list of column values using conditional statements?

Use As Column() instead of Column()

concat_dt = datatable("concatenated");new_val_list = {"num_col1", "num_col2"};name_list = {"col1", "col2"};stop = nitems(new_val_list);
for (i = 1, i <=stop, i++,
	col_name = name_list[i] || "_Label_A_B";
	show(col_name);
	concat_dt << New Column( col_name,
		Character,
		"Nominal",
		Formula( If( As Column(concat_dt, new_val_list[i]) > 200, "A", "B" ) )
	)	
);

However, I believe this is a better solution

Names Default To Here( 1 );
concat_dt = Data Table( "concatenated" );
name_list = {"col1", "col2"};

For Each( {col, i}, name_list,
	concat_dt << New Column( "num_" || col || "_Label_A_B" ,
		Character,
		"Nominal",
		Set Each Value( If( As Column( concat_dt, col ) > 200, "A", "B" ) )
	);
);
Jim

Recommended Articles