Subscribe Bookmark RSS Feed

Count Columns

jake

Community Trekker

Joined:

Aug 29, 2012

I have a script that adds a few columns into data table and creates some charts.  The first time I run the script I want to create the new columns, but I want to be able to run the script again on a data table that already has the added columns and just creates the charts.  I'm imagining an "IF" statement that checks if a certain column is present or counts the number of columns in the table.  Can anybody show me how to do either of those things and/or a different way to script what I am looking for?

Thanks.

1 REPLY
blackeneth

Community Trekker

Joined:

Mar 6, 2013

This is (thankfully) pretty easy in JMP. You can have all of the column names sent to a list.

The script below grabs the names of the columns in a table. Then it checks if a user-supplied list of column names is present in the table - if the column name is present, a message is written to the log window. It doesn't take too much imagination to see that if the new column name is not present in the table, the IF() condition in the For loop is changed so that the new column is created.

Names default to Here(1);

clear log();

bigClass = try(Open( "$SAMPLE_DATA/Big Class.JMP" ));

dt = current data table(); // table name in topmost window;

tblcols=dt << get column names(string);

mynewcols = {"height","weight","bmi"}; //lowercase or will not match

sort list into(mynewcols);

dt << begin data update;

write("\!NStart of Script.\!N");

For(i=1,i<=n items(tblcols),i++,

    mycol = trim(lowercase(tblcols)); //here is the force to lowercase

  myloc = contains(mynewcols,mycol);

  if(myloc != 0,

    write("Column ", mycol, " already present in table.\!N");

    remove from(mynewcols,myloc,1);

    continue()

    );

 

); // end of for loop

dt << end data update;

try(close(dt));

write("End of script.\!N");

// look in the log window for output;