<?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 Re: Recode multiple columns with list in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Recode-multiple-columns-with-list/m-p/482799#M72712</link>
    <description>&lt;P&gt;I always like using column names and then figuring out (or at least try to...) how to use them when I need to manipulate columns / get values from columns. Usually it is one of As Column(), Column() or Eval(), but sometimes you have to get more creative.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here are few snippets that might come handy from time to time:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");

// set name which would usually break
Column(dt, "name") &amp;lt;&amp;lt; Set Name("@na-me");

str_col_list = dt &amp;lt;&amp;lt; Get Column Names("String");
show(str_col_list);
column_list = dt &amp;lt;&amp;lt; Get Column Reference(str_col_list);
show(column_list);
ascolumn_list = Transform Each({col_name}, str_col_list,
	Name Expr(As Column(dt, col_name))
);
show(ascolumn_list);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also see &lt;LI-MESSAGE title="Deprecating the Name() parser directive in JMP 16" uid="374401" url="https://community.jmp.com/t5/JMPer-Cable/Deprecating-the-Name-parser-directive-in-JMP-16/m-p/374401#U374401" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-blog-thread lia-fa-icon lia-fa-blog lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp; and &lt;A href="https://www.jmp.com/support/help/en/16.0/?os=win&amp;amp;source=application&amp;amp;utm_source=helpmenu&amp;amp;utm_medium=application#page/jmp/names.shtml" target="_self"&gt;Names (jmp.com)&lt;/A&gt; regarding Name("col name")&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 28 Apr 2022 15:01:12 GMT</pubDate>
    <dc:creator>jthi</dc:creator>
    <dc:date>2022-04-28T15:01:12Z</dc:date>
    <item>
      <title>Recode multiple columns with list</title>
      <link>https://community.jmp.com/t5/Discussions/Recode-multiple-columns-with-list/m-p/482482#M72687</link>
      <description>&lt;P&gt;I want to recode multiple columns using a conditional formula by going through each column with a For Each function. I am not able to recode successfully with the following code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;//Create list of columns to be recoded 
ColNames = {dt:Name( "col 1" ), dt:Name( "col 2" ), dt:Name( "col 3" ), dt:Name( "col 4" )};
 
For Each( {listitem}, ColNames,
	Recode(
		listitem,
		{Lowercase( _rcNow ), If( Contains( _rcNow, "too much" ),
			"1000",
			_rcNow
		), Regex( _rcNow, "\D*(\d+\.?\d*)\D*", "\1", GLOBALREPLACE )}
	)
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV&gt;
&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;My hunch is that the body of the For Each clause is incorrect -- that I have to somehow call out the columns more clearly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data table as "dt" is established earlier in the code. For the record, I don't get an error when I use the script but the columns are not recoded properly. Thanks.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 16:57:49 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Recode-multiple-columns-with-list/m-p/482482#M72687</guid>
      <dc:creator>MargaretWM</dc:creator>
      <dc:date>2023-06-09T16:57:49Z</dc:date>
    </item>
    <item>
      <title>Re: Recode multiple columns with list</title>
      <link>https://community.jmp.com/t5/Discussions/Recode-multiple-columns-with-list/m-p/482503#M72688</link>
      <description>&lt;P&gt;Maybe you just need to use Recode Column instead of Recode and then tinker a bit on how to reference on columns. &lt;/P&gt;
&lt;P&gt;This seems to be working, at least it replaces too much with 1000 in col 1:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

dt = New Table("Untitled 2",
	Add Rows(1),
	New Column("col 1", Character, "Nominal", Set Values({"too much"})),
	New Column("col 2", Character, "Nominal", Set Values({"a"})),
	New Column("col 3", Character, "Nominal", Set Values({"c"})),
	New Column("col 4", Character, "Nominal", Set Values({"d"}))
);

    //Create list of columns to be recoded 
ColNames = {"col 1"};
For Each({listitem}, ColNames,
	dt &amp;lt;&amp;lt; Recode Column(
		//As Column(dt, listitem),
		AsColumn(listitem),
		{Lowercase(_rcNow), If(Contains(_rcNow, "too much"),
			"1000",
			_rcNow
		), Regex(_rcNow, "\D*(\d+\.?\d*)\D*", "\1", GLOBALREPLACE)},
		Update Properties(1),
		Target Column(Column(listitem))
	)
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Apr 2022 19:34:49 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Recode-multiple-columns-with-list/m-p/482503#M72688</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2022-04-27T19:34:49Z</dc:date>
    </item>
    <item>
      <title>Re: Recode multiple columns with list</title>
      <link>https://community.jmp.com/t5/Discussions/Recode-multiple-columns-with-list/m-p/482564#M72690</link>
      <description>&lt;P&gt;I adapted the code here for my purposes (several columns in the list) but it still does not work:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;ColNames = {dt:Name("col 1"), dt:Name("col 2"),
 dt:Name("col 3"), dt:Name("col4")};
 
 For Each({listitem}, ColNames, 
	dt&amp;lt;&amp;lt;Recode Column(
	AsColumn(listitem),
	{Lowercase( _rcNow ), If( Contains( _rcNow, "too much" ),
		"1000",
		_rcNow
	), Regex( _rcNow, "\D*(\d+\.?\d*)\D*", "\1", GLOBALREPLACE )},
	Update Properties(1),
	Target Column(Column(SPcol))
));
 &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Were you suggesting I go column by column to recode?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Apr 2022 20:27:03 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Recode-multiple-columns-with-list/m-p/482564#M72690</guid>
      <dc:creator>MargaretWM</dc:creator>
      <dc:date>2022-04-27T20:27:03Z</dc:date>
    </item>
    <item>
      <title>Re: Recode multiple columns with list</title>
      <link>https://community.jmp.com/t5/Discussions/Recode-multiple-columns-with-list/m-p/482600#M72695</link>
      <description>&lt;P&gt;I'm not sure which formats for columns Recode Columns requires. You can see that we are using different syntaxes for ColNames (I'm using list of column names, and you are using old way to reference columns (Name("col name"), it has been replaced by "col name"n), also we are using different columns in Target Column.&lt;/P&gt;
&lt;P&gt;If run the example I gave with this data table:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jthi_1-1651120525132.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/42075iD5CFEF3856EA46A6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jthi_1-1651120525132.png" alt="jthi_1-1651120525132.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I end up with this&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jthi_2-1651120534847.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/42076iDC816706F71A6756/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jthi_2-1651120534847.png" alt="jthi_2-1651120534847.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is updated example with new table and more columns added to ColNames list&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

dt = New Table("Untitled 2",
	Add Rows(3),
	Compress File When Saved(1),
	New Column("col 1", Character, "Nominal", Set Values({"1000", "too much", "too much"})),
	New Column("col 2",
		Character,
		"Nominal",
		Set Values({"too much", "", ""}),
		Set Display Width(65)
	),
	New Column("col 3", Character, "Nominal", Set Values({"c", "", "too much"})),
	New Column("col 4", Character, "Nominal", Set Values({"d", "", ""}))
);
wait(1);
    //Create list of columns to be recoded 
ColNames = {"col 1", "col 2", "col 3", "col 4"};
For Each({listitem}, ColNames,
	dt &amp;lt;&amp;lt; Recode Column(
		//As Column(dt, listitem),
		As Column(listitem),
		{Lowercase(_rcNow), If(Contains(_rcNow, "too much"),
			"1000",
			_rcNow
		), Regex(_rcNow, "\D*(\d+\.?\d*)\D*", "\1", GLOBALREPLACE)},
		Update Properties(1),
		Target Column(Column(listitem))
	)
);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Apr 2022 04:36:02 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Recode-multiple-columns-with-list/m-p/482600#M72695</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2022-04-28T04:36:02Z</dc:date>
    </item>
    <item>
      <title>Re: Recode multiple columns with list</title>
      <link>https://community.jmp.com/t5/Discussions/Recode-multiple-columns-with-list/m-p/482798#M72711</link>
      <description>&lt;P&gt;Thanks for your help! Jordan Hiller helped me understand your original reply. I don't understand why using the list of columns as strings works while using a list of actual columns does not work, but I follow your solution now anyway. Helpful to see how column names are called out now too. Good reminder. Thanks again.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Apr 2022 14:47:49 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Recode-multiple-columns-with-list/m-p/482798#M72711</guid>
      <dc:creator>MargaretWM</dc:creator>
      <dc:date>2022-04-28T14:47:49Z</dc:date>
    </item>
    <item>
      <title>Re: Recode multiple columns with list</title>
      <link>https://community.jmp.com/t5/Discussions/Recode-multiple-columns-with-list/m-p/482799#M72712</link>
      <description>&lt;P&gt;I always like using column names and then figuring out (or at least try to...) how to use them when I need to manipulate columns / get values from columns. Usually it is one of As Column(), Column() or Eval(), but sometimes you have to get more creative.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here are few snippets that might come handy from time to time:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");

// set name which would usually break
Column(dt, "name") &amp;lt;&amp;lt; Set Name("@na-me");

str_col_list = dt &amp;lt;&amp;lt; Get Column Names("String");
show(str_col_list);
column_list = dt &amp;lt;&amp;lt; Get Column Reference(str_col_list);
show(column_list);
ascolumn_list = Transform Each({col_name}, str_col_list,
	Name Expr(As Column(dt, col_name))
);
show(ascolumn_list);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also see &lt;LI-MESSAGE title="Deprecating the Name() parser directive in JMP 16" uid="374401" url="https://community.jmp.com/t5/JMPer-Cable/Deprecating-the-Name-parser-directive-in-JMP-16/m-p/374401#U374401" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-blog-thread lia-fa-icon lia-fa-blog lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp; and &lt;A href="https://www.jmp.com/support/help/en/16.0/?os=win&amp;amp;source=application&amp;amp;utm_source=helpmenu&amp;amp;utm_medium=application#page/jmp/names.shtml" target="_self"&gt;Names (jmp.com)&lt;/A&gt; regarding Name("col name")&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Apr 2022 15:01:12 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Recode-multiple-columns-with-list/m-p/482799#M72712</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2022-04-28T15:01:12Z</dc:date>
    </item>
  </channel>
</rss>

