cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Instantly extract effect sizes, F-ratios, and FDR-adjusted p-values from your models with the Calculate Effects Sizes extension, available now in the JMP Marketplace!
  • New to JMP? Join us Sept. 23-24 for the Early User Edition of Discovery Summit, tailor-made for new users. Register now for free!
  • See how to use the JMP Marketplace – Free tools to expand JMP capabilities. Register. July 10, 2 pm US Eastern Time.

Discussions

Solve problems, and share tips and tricks with other JMP users.
Choose Language Hide Translation Bar
Jackie_
Level VI

Create New Column if the list contains

Hi,

I have a list which contains column names. I want to create a new column if the column name exist in the string list.

 

Here's my code. It works but creates duplicate column names bc of the loop. Any advice?

 

Names Default To Here( 1 );
Clear Globals();
Clear Log();
dt = Current Data Table();

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

idds = {52,54};
For( i = 1, i <= N items( idds ), i++,

  If( Contains( col_list, Char( idds[i] ) ),
   dt<<Column( "%_IDD", Numeric, "Continuous", Format( "Percent", 12, 2 ));
  ););
  

 Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Create New Column if the list contains

Your code will create a new column for every column found in your idds list.  This code checks to see if the column already exists and if it doesn't it will create it.

Names Default To Here( 1 );
Clear Globals();
Clear Log();
dt = Current Data Table();

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

idds = {52, 54};
For( i = 1, i <= N Items( idds ), i++, 

	If(
		Contains( col_list, Char( idds[i] ) ) & Try( Column( dt, "%_IDD" ) << get name, "" ) ==
		"",
		dt << New Column( "%_IDD", Numeric, "Continuous", Format( "Percent", 12, 2 ) )
	)
);
Jim

View solution in original post

1 REPLY 1
txnelson
Super User

Re: Create New Column if the list contains

Your code will create a new column for every column found in your idds list.  This code checks to see if the column already exists and if it doesn't it will create it.

Names Default To Here( 1 );
Clear Globals();
Clear Log();
dt = Current Data Table();

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

idds = {52, 54};
For( i = 1, i <= N Items( idds ), i++, 

	If(
		Contains( col_list, Char( idds[i] ) ) & Try( Column( dt, "%_IDD" ) << get name, "" ) ==
		"",
		dt << New Column( "%_IDD", Numeric, "Continuous", Format( "Percent", 12, 2 ) )
	)
);
Jim

Recommended Articles