cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
Melissa
Level I

How do I reference to a column by column number in jsl when column number is a variable?

Hi,

I'm having trouble subsetting columns.  In the code below, I'm looking to create multiple new data tables (and perform some analyses with them- though I deleted that part of the script) from my current table. My current table is formatted with one "X" column (column 1) and several "Y" columns (columns 2 through ncols(dt). I'm having an issue where the columns() function does not recognize icol as a variable representing a number (I assume it's looking for a column named icol). Is there a fix for this?

 

dt=current data table();
For( icol = 2, icol <= ncols(dt) , icol++,
	dt << Subset( All rows, columns( 1, icol  ))
) 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ron_horne
Super User (Alumni)

Re: How do I reference to a column by column number in jsl when column number is a variable?

Hi @Melissa 

try this method,

 

Names Default To Here( 1 );

dt = Current Data Table();
icol = dt << Get Column Names();

For( i = 2, i <= N Items( icol ), i++,
	dt1 = dt << subset( all rows, columns( 1, icol[i] ),
		output table( "subset - " || Char( icol[i] ) ), 
		
	)
);

 

 

 

let us know if it works for you.

ron

 

View solution in original post

2 REPLIES 2
ron_horne
Super User (Alumni)

Re: How do I reference to a column by column number in jsl when column number is a variable?

Hi @Melissa 

try this method,

 

Names Default To Here( 1 );

dt = Current Data Table();
icol = dt << Get Column Names();

For( i = 2, i <= N Items( icol ), i++,
	dt1 = dt << subset( all rows, columns( 1, icol[i] ),
		output table( "subset - " || Char( icol[i] ) ), 
		
	)
);

 

 

 

let us know if it works for you.

ron

 

Melissa
Level I

Re: How do I reference to a column by column number in jsl when column number is a variable?

Thanks! that's exactly what I was looking for.