cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
StarfruitBob
Level VI

Prepping list column names for future use

Hello, I'm trying to retrieve and prep a list of column names to save in a variable to use later. The issue is that << Get Column Names() returns everything as a list of strings. Below is how I've added in a " : " to the beginning of each item string and what a potential usage of this may be. More still needs to be done.

 

names default to here(1);
dt = current data table();

col_names = dt << Get Column Names( );  // retrieves all col names
col_list = {};  // list for holding col names

// loop formats names in way usable to be called on as cols, not strings
for( i = 1 , i <= N items( col_names ), i++,
	list_item = ":" || list_item;
	Insert Into( col_list, list_item );
);

dt << Ungroup Scripts( col_list ); // example usage

Another example would be usage of this list into something like:

Fit Model( Y( col_list ) ....

 

What I get: { "ColA", "ColB" }

What I want: { :ColA, :ColB }

 

I've browsed and found multiple related threads about this, but the usage of the column names as an input for a function is something I didn't stumble across.  Any advice?

 

 

Learning every day!
2 ACCEPTED SOLUTIONS

Accepted Solutions
Thierry_S
Super User

Re: Prepping list column names for future use

Hi,

 

I had the same problem a while back. The solution is simple: you need to evaluate the list to return the series of columns.

dt << Ungroup Scripts( Eval (col_list ));

Best,

TS

Thierry R. Sornasse

View solution in original post

jthi
Super User

Re: Prepping list column names for future use

If you want to use columns formatted as :colname, you can get them for example with something like this:

Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");

col_names = dt << Get Column Names(Continuous, "String"); 
 // dt << Get Column Reference(); /* all references */

/* one option */
col_list1 = dt << Get Column Reference(col_names);

/* second option */
col_list2 = Transform Each({col_name}, col_names,
	Name Expr(AsColumn(Column(dt, col_name))); /* Column() isnt necessary but makes it more robust */
);

show(col_list1, col_list2);

Usually I use them as strings and then use Column() or As Column() depending on the situation (with some Name Expr() in the mix).

Quite often using list of strings of column names is enough if you combine that with Eval().

Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Cities.jmp");
col_names = {"OZONE", "CO", "SO2", "NO", "PM10"};

//obj = dt << Cluster Variables(Y(:OZONE, :CO, :SO2, :NO, :PM10));
obj = dt << Cluster Variables(Y(Eval(col_names)));


col_list = Transform Each({col_name}, col_names,
	Name Expr(AsColumn(Column(dt, col_name)));
);

obj = dt << Cluster Variables(Y(col_list)); /* wont work, see log for error */

/* Using Eval() and Eval Expr() */
cv_expr = EvalExpr(
	obj = dt << Cluster Variables(Y(Expr(col_list)));
);
Eval(cv_expr);

/* using only Eval inside Y() should also work like earlier */
obj = dt << Cluster Variables(Y(Eval(col_list)));
-Jarmo

View solution in original post

3 REPLIES 3
Thierry_S
Super User

Re: Prepping list column names for future use

Hi,

 

I had the same problem a while back. The solution is simple: you need to evaluate the list to return the series of columns.

dt << Ungroup Scripts( Eval (col_list ));

Best,

TS

Thierry R. Sornasse
jthi
Super User

Re: Prepping list column names for future use

If you want to use columns formatted as :colname, you can get them for example with something like this:

Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");

col_names = dt << Get Column Names(Continuous, "String"); 
 // dt << Get Column Reference(); /* all references */

/* one option */
col_list1 = dt << Get Column Reference(col_names);

/* second option */
col_list2 = Transform Each({col_name}, col_names,
	Name Expr(AsColumn(Column(dt, col_name))); /* Column() isnt necessary but makes it more robust */
);

show(col_list1, col_list2);

Usually I use them as strings and then use Column() or As Column() depending on the situation (with some Name Expr() in the mix).

Quite often using list of strings of column names is enough if you combine that with Eval().

Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Cities.jmp");
col_names = {"OZONE", "CO", "SO2", "NO", "PM10"};

//obj = dt << Cluster Variables(Y(:OZONE, :CO, :SO2, :NO, :PM10));
obj = dt << Cluster Variables(Y(Eval(col_names)));


col_list = Transform Each({col_name}, col_names,
	Name Expr(AsColumn(Column(dt, col_name)));
);

obj = dt << Cluster Variables(Y(col_list)); /* wont work, see log for error */

/* Using Eval() and Eval Expr() */
cv_expr = EvalExpr(
	obj = dt << Cluster Variables(Y(Expr(col_list)));
);
Eval(cv_expr);

/* using only Eval inside Y() should also work like earlier */
obj = dt << Cluster Variables(Y(Eval(col_list)));
-Jarmo
StarfruitBob
Level VI

Re: Prepping list column names for future use

@Thierry_S & @jthi - thank you both! eval() was what I was looking for! I understood that the column was an expression, but I completely forgot about this command.

Learning every day!