cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
Choose Language Hide Translation Bar
terapin
Level VI

Select columns contained in user supplied list

I'm trying to figure out how to use a user specified list to select those data columns contained in the list.  The user supplied list may contain more or less names than present in data table.  For example, I've tried the following but I can't seem to get it to work.  Any ideas would be appreciated.  Thanks

dt = open("$SAMPLE_DATA/Big Class.jmp");

// Get list of data column names

col_list = dt << Get Column Names ( String );

// User specified column names

cc = { sex, weight, test, new, old };

 

for( i=1, i<=ncol( cc ), i++,

       if(contains( col_list, cc ),

       Column ( dt, col_list ) << Set Selected ( 1 )

       )

);

1 ACCEPTED SOLUTION

Accepted Solutions
ms
Super User (Alumni) ms
Super User (Alumni)

Re: Select columns contained in user supplied list

I was not looking into the details of the output. You are looping over cc, then you should refer to cc rather than col_list to get the desired result.

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

// Get list of data column names

col_list = dt << Get Column Names( string );

// User specified column names

cc = {sex, weight, test, new, old};

For( i = 1, i <= N Items( cc ), i++,

  If( Contains( col_list, Char( cc[i] ) ),

  Column( dt, Char( cc[i] ) ) << Set Selected( 1 )

  )

);


//This works too

For( i = 1, i <= N Items( cc ), i++,

  If( Contains( col_list, Char( cc[i] ) ),

   :(cc[i]) << Set Selected( 1 )

  )

);

View solution in original post

4 REPLIES 4
ms
Super User (Alumni) ms
Super User (Alumni)

Re: Select columns contained in user supplied list

Almost there. Use N Items() instead of ncol() for lists. And to match two items both need  to of the same data type. So, either provide the user supplied list as a list of strings or use Char()as below.

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

// Get list of data column names

col_list = dt << Get Column Names( string );

// User specified column names

cc = {sex, weight, test, new, old};

For( i = 1, i <= N Items( cc ), i++,

  If( Contains( col_list, Char( cc[i] ) ),

  Column( dt, col_list[i] ) << Set Selected( 1 )

  )

);

terapin
Level VI

Re: Select columns contained in user supplied list

Hi MS,

Thanks for the tips.  Although the code you provided runs it doesn't select just those columns listed in the user specified list.  For example, the code listed above selects the first two columns ( name and sex) even though sex isn't in the user specified list.  I was thinking this list construct was going through and finding those column numbers in col_list that match those located in cc and then selecting them.  It appears that it is actually selects the columns based on position in cc, not name.  Is it possible to select the columns according to matched names, not position?

ms
Super User (Alumni) ms
Super User (Alumni)

Re: Select columns contained in user supplied list

I was not looking into the details of the output. You are looping over cc, then you should refer to cc rather than col_list to get the desired result.

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

// Get list of data column names

col_list = dt << Get Column Names( string );

// User specified column names

cc = {sex, weight, test, new, old};

For( i = 1, i <= N Items( cc ), i++,

  If( Contains( col_list, Char( cc[i] ) ),

  Column( dt, Char( cc[i] ) ) << Set Selected( 1 )

  )

);


//This works too

For( i = 1, i <= N Items( cc ), i++,

  If( Contains( col_list, Char( cc[i] ) ),

   :(cc[i]) << Set Selected( 1 )

  )

);

terapin
Level VI

Re: Select columns contained in user supplied list

MS,

Thanks for your help with this.  I see now that my thinking about which column to loop over was what was causing me some of my earlier grief.  I assumed I would loop over and select the columns listed in the data table variable list, not the user specified list.  Always learning something new here.  Thanks.