<?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: Keep Columns Matching a List, Remove the Rest in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Keep-Columns-Matching-a-List-Remove-the-Rest/m-p/427173#M67682</link>
    <description>&lt;P&gt;Another option if using JMP 16:&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" );

//list of columns to include
col_of_interest = {"name", "age", "weight", "does not exist"};

// Option 1 - subset table
// Filter out columns in the list that aren't in the table and subset
cols_checked = filter each({cn}, dt &amp;lt;&amp;lt; Get Column Names( String ), n items(loc( col_of_interest, cn ))&amp;gt;0);
dtNew = dt &amp;lt;&amp;lt; subset( all rows, columns( cols_checked ) );

// Option 2 - delete columns from original table
for each({c}, dt &amp;lt;&amp;lt; get column references, if( n items( loc( col_of_interest, c &amp;lt;&amp;lt; get name ) ) == 0, dt &amp;lt;&amp;lt; delete columns(c) ) );&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 15 Oct 2021 19:25:05 GMT</pubDate>
    <dc:creator>ih</dc:creator>
    <dc:date>2021-10-15T19:25:05Z</dc:date>
    <item>
      <title>Keep Columns Matching a List, Remove the Rest</title>
      <link>https://community.jmp.com/t5/Discussions/Keep-Columns-Matching-a-List-Remove-the-Rest/m-p/427140#M67678</link>
      <description>&lt;P&gt;Hi -&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to remove columns (based on column names) from a data table that do &lt;STRONG&gt;not&lt;/STRONG&gt; match a list. I have following JSL code so far but it is not working.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Input appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;Names Default To Here( 1 );
bigClass = Open( "$SAMPLE_DATA/Big Class.JMP" );
dt = Current Data Table();

col_of_interest = {"name", "age", "weight"};

col_names = dt &amp;lt;&amp;lt; Get Column Names( String );

For(j = 1, j &amp;lt;= N Items( col_of_interest ), j++,
   For( i = N Items( col_names ), i &amp;gt; 0, i--,  
		if ( not( col_names[i] == col_of_interest[j],
			 Remove From( col_names, i ))     	
		  );  
	);
);
dtNew = dt &amp;lt;&amp;lt; subset( all rows, columns( col_names ) ); Wait( 0 );&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 18:02:46 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Keep-Columns-Matching-a-List-Remove-the-Rest/m-p/427140#M67678</guid>
      <dc:creator>uProf</dc:creator>
      <dc:date>2023-06-09T18:02:46Z</dc:date>
    </item>
    <item>
      <title>Re: Keep Columns Matching a List, Remove the Rest</title>
      <link>https://community.jmp.com/t5/Discussions/Keep-Columns-Matching-a-List-Remove-the-Rest/m-p/427158#M67679</link>
      <description>&lt;P&gt;I would loop over all columns in the data table and remove when needed (if you want to keep to your idea, I can also help debugging that):&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;Names Default To Here(1);
bigClass = Open("$SAMPLE_DATA/Big Class.JMP");
dt = Current Data Table();

col_of_interest = {"name", "age", "weight"};

col_names = dt &amp;lt;&amp;lt; Get Column Names(String);

For(i = 1, i &amp;lt;= N Items(col_names), i++,
	If(!Contains(col_of_interest, col_names[i]),
		dt &amp;lt;&amp;lt; Delete Columns(col_names[i]);
	);
);
Wait(0);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Also I would suggest not using J as variable name, as JMP has a matrix function J().&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit:&lt;/P&gt;&lt;P&gt;Also there is no need to use Current Data Table() (and you shouldn't) because you already have the reference after opening the table. Also here is my preferred method to remove columns (using associative array to get only the extra columns and removing those):&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.JMP");

col_of_interest = {"name", "age", "weight"};

//get columns which are "extra"
col_names_aa = Associative Array(dt &amp;lt;&amp;lt; Get Column Names(String));
col_names_aa &amp;lt;&amp;lt; Remove(Associative Array(col_of_interest));
col_names_to_remove = col_names_aa &amp;lt;&amp;lt; get keys;
Show(col_names_to_remove);

dt &amp;lt;&amp;lt; Delete Columns(col_names_to_remove);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Oct 2021 19:15:26 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Keep-Columns-Matching-a-List-Remove-the-Rest/m-p/427158#M67679</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2021-10-15T19:15:26Z</dc:date>
    </item>
    <item>
      <title>Re: Keep Columns Matching a List, Remove the Rest</title>
      <link>https://community.jmp.com/t5/Discussions/Keep-Columns-Matching-a-List-Remove-the-Rest/m-p/427165#M67680</link>
      <description>&lt;P&gt;When comparing two lists, I find it easier to make a third list to contain the results while working through the loops and then use the third list to take action when all the looping is complete.&amp;nbsp; The code below may accomplish what you are attempting.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.JMP" );

col_of_interest = {"name", "age", "weight"};

col_names = dt &amp;lt;&amp;lt; Get Column Names( String );

col_list = {};

For( i = N Items( col_names ), i &amp;gt; 0, i--,  
	if(contains(col_of_interest, col_names[i]),
		insert into(col_list, col_names[i])
	); 
);

dtNew = dt &amp;lt;&amp;lt; subset( all rows, columns( col_list ) ); 
Wait( 0 );&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Oct 2021 19:13:31 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Keep-Columns-Matching-a-List-Remove-the-Rest/m-p/427165#M67680</guid>
      <dc:creator>ngambles</dc:creator>
      <dc:date>2021-10-15T19:13:31Z</dc:date>
    </item>
    <item>
      <title>Re: Keep Columns Matching a List, Remove the Rest</title>
      <link>https://community.jmp.com/t5/Discussions/Keep-Columns-Matching-a-List-Remove-the-Rest/m-p/427166#M67681</link>
      <description>&lt;P&gt;Thanks for the input and the tip on matrix function J, jthi. Let me retry.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Oct 2021 19:14:21 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Keep-Columns-Matching-a-List-Remove-the-Rest/m-p/427166#M67681</guid>
      <dc:creator>uProf</dc:creator>
      <dc:date>2021-10-15T19:14:21Z</dc:date>
    </item>
    <item>
      <title>Re: Keep Columns Matching a List, Remove the Rest</title>
      <link>https://community.jmp.com/t5/Discussions/Keep-Columns-Matching-a-List-Remove-the-Rest/m-p/427173#M67682</link>
      <description>&lt;P&gt;Another option if using JMP 16:&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" );

//list of columns to include
col_of_interest = {"name", "age", "weight", "does not exist"};

// Option 1 - subset table
// Filter out columns in the list that aren't in the table and subset
cols_checked = filter each({cn}, dt &amp;lt;&amp;lt; Get Column Names( String ), n items(loc( col_of_interest, cn ))&amp;gt;0);
dtNew = dt &amp;lt;&amp;lt; subset( all rows, columns( cols_checked ) );

// Option 2 - delete columns from original table
for each({c}, dt &amp;lt;&amp;lt; get column references, if( n items( loc( col_of_interest, c &amp;lt;&amp;lt; get name ) ) == 0, dt &amp;lt;&amp;lt; delete columns(c) ) );&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Oct 2021 19:25:05 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Keep-Columns-Matching-a-List-Remove-the-Rest/m-p/427173#M67682</guid>
      <dc:creator>ih</dc:creator>
      <dc:date>2021-10-15T19:25:05Z</dc:date>
    </item>
    <item>
      <title>Re: Keep Columns Matching a List, Remove the Rest</title>
      <link>https://community.jmp.com/t5/Discussions/Keep-Columns-Matching-a-List-Remove-the-Rest/m-p/427678#M67715</link>
      <description>&lt;P&gt;As a matter of fact, JMP will not confuse the variable j and the function J() because when you use the name of a function, it must be followed by the parentheses, or it will be scoped as something else, like a variable if that name exists.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I take your warning seriously in general because names are important in JSL!&lt;/P&gt;</description>
      <pubDate>Mon, 18 Oct 2021 13:36:50 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Keep-Columns-Matching-a-List-Remove-the-Rest/m-p/427678#M67715</guid>
      <dc:creator>Mark_Bailey</dc:creator>
      <dc:date>2021-10-18T13:36:50Z</dc:date>
    </item>
  </channel>
</rss>

