<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to create a new column in a data table from a list of column values using conditional statements? in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/How-to-create-a-new-column-in-a-data-table-from-a-list-of-column/m-p/758248#M93850</link>
    <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let's say these are the two numeric columns:&lt;/P&gt;&lt;P&gt;numeric_col_1 = {100, 200, 250, 700}&lt;/P&gt;&lt;P&gt;numeric_col_2 = {200, 250, 500, 100}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And the threshold is 100.&lt;/P&gt;&lt;P&gt;Now the labeled columns pseudocode I want to implement is as follows:&lt;/P&gt;&lt;P&gt;labeled_col = (if numeric_col &amp;gt; 100, "A", "B");&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So the labeled columns will now show:&lt;/P&gt;&lt;P&gt;labeled_col_1 = {"B", "A", "A", "A"};&lt;/P&gt;&lt;P&gt;labeled_col_2 = {"A", "A", "A", "B"};&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is some example code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;concat_dt = datatable("concatenated");&lt;BR /&gt;new_val_list = {"num_col1", "num_col2"};&lt;BR /&gt;name_list = {"col1", "col2"};&lt;BR /&gt;stop = nitems(new_val_list);
for (i = 1, i &amp;lt;=stop, i++,
	col_name = name_list[i] || "_Label_A_B";
	show(col_name);
	concat_dt &amp;lt;&amp;lt; New Column( col_name,
		Character,
		"Nominal",
		Formula( If( Column(concat_dt, new_val_list[i]) &amp;gt; 200, "A", "B" ) )
	)	
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But the above code only shows empty columns even though they have been created with their respective column names in the data table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 21 May 2024 20:52:44 GMT</pubDate>
    <dc:creator>rk2</dc:creator>
    <dc:date>2024-05-21T20:52:44Z</dc:date>
    <item>
      <title>How to create a new column in a data table from a list of column values using conditional statements?</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-create-a-new-column-in-a-data-table-from-a-list-of-column/m-p/758248#M93850</link>
      <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let's say these are the two numeric columns:&lt;/P&gt;&lt;P&gt;numeric_col_1 = {100, 200, 250, 700}&lt;/P&gt;&lt;P&gt;numeric_col_2 = {200, 250, 500, 100}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And the threshold is 100.&lt;/P&gt;&lt;P&gt;Now the labeled columns pseudocode I want to implement is as follows:&lt;/P&gt;&lt;P&gt;labeled_col = (if numeric_col &amp;gt; 100, "A", "B");&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So the labeled columns will now show:&lt;/P&gt;&lt;P&gt;labeled_col_1 = {"B", "A", "A", "A"};&lt;/P&gt;&lt;P&gt;labeled_col_2 = {"A", "A", "A", "B"};&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is some example code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;concat_dt = datatable("concatenated");&lt;BR /&gt;new_val_list = {"num_col1", "num_col2"};&lt;BR /&gt;name_list = {"col1", "col2"};&lt;BR /&gt;stop = nitems(new_val_list);
for (i = 1, i &amp;lt;=stop, i++,
	col_name = name_list[i] || "_Label_A_B";
	show(col_name);
	concat_dt &amp;lt;&amp;lt; New Column( col_name,
		Character,
		"Nominal",
		Formula( If( Column(concat_dt, new_val_list[i]) &amp;gt; 200, "A", "B" ) )
	)	
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But the above code only shows empty columns even though they have been created with their respective column names in the data table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 May 2024 20:52:44 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-create-a-new-column-in-a-data-table-from-a-list-of-column/m-p/758248#M93850</guid>
      <dc:creator>rk2</dc:creator>
      <dc:date>2024-05-21T20:52:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a new column in a data table from a list of column values using conditional statements?</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-create-a-new-column-in-a-data-table-from-a-list-of-column/m-p/758343#M93851</link>
      <description>&lt;P&gt;Use As Column() instead of Column()&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;concat_dt = datatable("concatenated");new_val_list = {"num_col1", "num_col2"};name_list = {"col1", "col2"};stop = nitems(new_val_list);
for (i = 1, i &amp;lt;=stop, i++,
	col_name = name_list[i] || "_Label_A_B";
	show(col_name);
	concat_dt &amp;lt;&amp;lt; New Column( col_name,
		Character,
		"Nominal",
		Formula( If( As Column(concat_dt, new_val_list[i]) &amp;gt; 200, "A", "B" ) )
	)	
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, I believe this is a better solution&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
concat_dt = Data Table( "concatenated" );
name_list = {"col1", "col2"};

For Each( {col, i}, name_list,
	concat_dt &amp;lt;&amp;lt; New Column( "num_" || col || "_Label_A_B" ,
		Character,
		"Nominal",
		Set Each Value( If( As Column( concat_dt, col ) &amp;gt; 200, "A", "B" ) )
	);
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 May 2024 21:25:47 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-create-a-new-column-in-a-data-table-from-a-list-of-column/m-p/758343#M93851</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2024-05-21T21:25:47Z</dc:date>
    </item>
  </channel>
</rss>

