cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
The Discovery Summit 2025 Call for Content is open! Submit an abstract today to present at our premier analytics conference.
Choose Language Hide Translation Bar
AsymptoticCos
Level II

Process many columns with for loop

I have 100 Character columns named Column_1, Column_2,....Column_100. Each column has entries like A1, A2, ... A100. I am trying to create 200 columns from these 100 columns where each input column will create 2 output columns one output column will have the letter (A) and the other column will have the number (1,2,...). How to write this? 

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Process many columns with for loop

Here is an example

names default to here(1);
dt =
New Table( "Example",
	Add Rows( 20 ),
	New Column( "column 1",
		Character,
		Formula( "A" || Char( Random Integer( 1, 100 ) ) )
	),
	New Column( "column 2",
		Character,
		Formula( "A" || Char( Random Integer( 1, 100 ) ) )
	)
);

colList = dt << get column names( string );
For Each( {col}, colList,
	dt << New Column( col || " Letter",
		character,
		set each value( Substr( As Column( col ), 1, 1 ) )
	);
	dt << New Column( col || " Number",
		set each value( Num( Substr( As Column( col ), 2 ) ) )
	);
);

txnelson_0-1719507327010.png

 

Or using a For() Loop

names default to here(1);
dt =
New Table( "Example",
	Add Rows( 20 ),
	New Column( "column 1",
		Character,
		Formula( "A" || Char( Random Integer( 1, 100 ) ) )
	),
	New Column( "column 2",
		Character,
		Formula( "A" || Char( Random Integer( 1, 100 ) ) )
	)
);

colList = dt << get column names( string );
For( i=1,i<= NItems(colList), i++,
	dt << New Column( colList[i] || " Letter",
		character,
		set each value( Substr( As Column( colList[i] ), 1, 1 ) )
	);
	dt << New Column( colList[i] || " Number",
		set each value( Num( Substr( As Column( colList[i] ), 2 ) ) )
	);
);

 

Jim

View solution in original post

3 REPLIES 3
txnelson
Super User

Re: Process many columns with for loop

Here is an example

names default to here(1);
dt =
New Table( "Example",
	Add Rows( 20 ),
	New Column( "column 1",
		Character,
		Formula( "A" || Char( Random Integer( 1, 100 ) ) )
	),
	New Column( "column 2",
		Character,
		Formula( "A" || Char( Random Integer( 1, 100 ) ) )
	)
);

colList = dt << get column names( string );
For Each( {col}, colList,
	dt << New Column( col || " Letter",
		character,
		set each value( Substr( As Column( col ), 1, 1 ) )
	);
	dt << New Column( col || " Number",
		set each value( Num( Substr( As Column( col ), 2 ) ) )
	);
);

txnelson_0-1719507327010.png

 

Or using a For() Loop

names default to here(1);
dt =
New Table( "Example",
	Add Rows( 20 ),
	New Column( "column 1",
		Character,
		Formula( "A" || Char( Random Integer( 1, 100 ) ) )
	),
	New Column( "column 2",
		Character,
		Formula( "A" || Char( Random Integer( 1, 100 ) ) )
	)
);

colList = dt << get column names( string );
For( i=1,i<= NItems(colList), i++,
	dt << New Column( colList[i] || " Letter",
		character,
		set each value( Substr( As Column( colList[i] ), 1, 1 ) )
	);
	dt << New Column( colList[i] || " Number",
		set each value( Num( Substr( As Column( colList[i] ), 2 ) ) )
	);
);

 

Jim
AsymptoticCos
Level II

Re: Process many columns with for loop

I have other columns in the table as well. Say Column X, Column Y. I don't want these columns to be part of the processing. How do I change your line to only focus on the columns I care about.  

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

 

txnelson
Super User

Re: Process many columns with for loop

remove from(colList,contains(colList,"Column X"),1);

Please take the time to read the material under the Help=>JMP Help Online  in the Scripting Guide section.  Also, familiarize yourself with the Scripting Index under the Help pull down menu.  These pieces of documentation will provide you with a good background on the Scripting Language and conventions in JMP.

Jim