Our World Statistics Day conversations have been a great reminder of how much statistics can inform our lives. Do you have an example of how statistics has made a difference in your life? Share your story with the Community!
Choose Language Hide Translation Bar
Highlighted
UserID16644
Level III

Check column name if existing

Hi,

I have a list of column names that I need to check from my table if it exist.

How will I check every columns of the table and prompt an error and stop the program from running if it is not existing?

 

col_list = {"Col1", "Col2", "Col3", "Col4", "Col5" };
//checks column if exist

//..code continue running
//if column doesn't exist
//..code will stop script & error prompt

  

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
txnelson
Super User

Re: Check column name if existing

Here is a modification of the script that I previously posted.

I strongly suggest that you take the time to read the Scripting Guide found in the JMP Documentation Library,  under the Help pull down menu so you will be able to take this sample code and use it to solve your own scripting needs.

animals = Open( "$SAMPLE_DATA\Animals.jmp" );
dt_animals = Data Table( "Animals" );

col_list = {"species", "subject", "miles", "season", "name"};
notFound = {};
For( i = 1, i <= N Items( col_list ), i++,
	If( Try( Column( col_list[i] ) << get name, "" ) == "",
		Insert Into( notFound, col_list[i] )
	)
);

If( N Items( notFound ),
	nf = New Window( "Error:",
		V List Box(
			Text Box( "The following columns are required" ),
			Text Box( "but were not found in the data table" ),
			List Box( notFound )
		)
	);
	throw();
);
Jim

View solution in original post

5 REPLIES 5
Highlighted
txnelson
Super User

Re: Check column name if existing

The method I use is

col_list = {"col1", "col2", "col3"};
for(i=1,i<=N Items(col_list),i++,
    if(try(column(col_list[i])<<get name,"") == "", throw());
);
Jim
Highlighted
UserID16644
Level III

Re: Check column name if existing

animals = Open( "C:\Animals.jmp" );
dt_animals = Data Table( "Animals" );

col_list = {"species", "subject", "miles", "season", "name"};
For( i = 1, i <= N Items( col_list ), i++,
    If( Try( Column( col_list[i] ) << get name, "" ) == "",
        Throw()
    )
);



I'm sure I missed something, it's not working for me

Highlighted
txnelson
Super User

Re: Check column name if existing

What is it that isn't working?  I ran your code, and when it reached the column called "name" the code stopped.

The value of the variable "i" ended up as 5, since it stopped processing on the 5th loop.  If it had completed processing, the variable "i" would have been 6.

If there was code beyond this piece of code, it would not have been processed.

Jim
Highlighted
UserID16644
Level III

Re: Check column name if existing

The prompt where it will tell what column is missing
Highlighted
txnelson
Super User

Re: Check column name if existing

Here is a modification of the script that I previously posted.

I strongly suggest that you take the time to read the Scripting Guide found in the JMP Documentation Library,  under the Help pull down menu so you will be able to take this sample code and use it to solve your own scripting needs.

animals = Open( "$SAMPLE_DATA\Animals.jmp" );
dt_animals = Data Table( "Animals" );

col_list = {"species", "subject", "miles", "season", "name"};
notFound = {};
For( i = 1, i <= N Items( col_list ), i++,
	If( Try( Column( col_list[i] ) << get name, "" ) == "",
		Insert Into( notFound, col_list[i] )
	)
);

If( N Items( notFound ),
	nf = New Window( "Error:",
		V List Box(
			Text Box( "The following columns are required" ),
			Text Box( "but were not found in the data table" ),
			List Box( notFound )
		)
	);
	throw();
);
Jim

View solution in original post