cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
samir
Level IV

how to pass a column as parameter in a formula

Hi,

I am trying to create a simple formula on columns that are stored as variables in a list.

I do not understand why JSL refuses to perform the formula.

If I change in the formula the parameter by hte string it stores, it works perfectly.

Any help ??

Here is the code I use:

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

MyList = {"age", "height"};
ParamPlot = {"age2", "height2"};
for(j=1, j <= N items(MyList), j++,
 dt << New Column( ParamPlot[j], Formula( 2* ParamPlot[j]) );
);

 

I tried things like Eval, Eval expr,...but noway :(

4 ACCEPTED SOLUTIONS

Accepted Solutions

Re: how to pass a column as parameter in a formula

The problem is that the argument to Formula() is treated as a literal expression. It does not evaluate ParamPlot[j] and substitute the result to make the formula you want. It seems like you should be able to use expressions but I got no where with that approach so here is a solution based on character strings instead:

Names Default To Here( 1 );

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

MyList = {"age", "height"};
ParamPlot = {"age2", "height2"};

For( j = 1, j <= N Items( MyList ), j++,
	Eval(
		Parse(
			Substitute("dt << New Column( \!"nnn\!", Formula( 2 * :ppp ) )",
				"nnn",
				ParamPlot[j],
				"ppp",
				MyList[j]
			)
		);
	);
);

I really don't like using character strings but sometimes, you gotta do what you gotta do.

 

View solution in original post

ian_jmp
Level X

Re: how to pass a column as parameter in a formula

Maybe this is slightly better (in the eyes of some):

Names Default To Here( 1 );

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

MyList = {"age", "height"};
ParamPlot = {"age2", "height2"};

For( j = 1, j <= N Items( MyList ), j++,
	CMD = Expr(dt << New Column( NameTBD, Formula( 2 * colTBD) ));
	SubstituteInto(CMD, Expr(NameTBD), ParamPlot[j], Expr(ColTBD), Parse(":"||MyList[j]));
	CMD;
);

 

View solution in original post

Byron_JMP
Staff

Re: how to pass a column as parameter in a formula

it looks like you're trying to make a formula that multiplies its self by 2. this won't work and should return an empty value. 

Also, "j" is a function for making matricies (note that it turns blue in the JSL editor) so I usually avoid using "j" as a variable just in case it might get interpeted in an unexpected way.

 

Working with column names and references can be pretty frustrating. Take a look at this strategy.

Inside the loop, first the column is created, and as its created, its given a handle "newcol" to use as a reference. Next there is the big eval(substitute)) clause.  Inside this argument, the "col<<formula()" is defined as an expression (expressons aren't evaluated) with a place holder for the column name. Then that place holder is substuted for the column name. Still nothing is evaluated. After the substitution, the whole thing gets evaluated, because it wrapped in Eval. 

 

 

 

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
MyList = {"age", "height"};
ParamPlot = {"age2", "height2"};
For( i = 1, i <= N Items( MyList ), i++,
	newcol=dt << New Column( ParamPlot[i] );
	Eval(
		Substitute( 
			Expr( 
				newcol << set formula( a * 2 ) ) ,
			Expr( a ), As Name( mylist[i] )
	);
));
JMP Systems Engineer, Health and Life Sciences (Pharma)

View solution in original post

pmroz
Super User

Re: how to pass a column as parameter in a formula

I couldn't resist one last take.  Short and sweet and it works.  Use evalinsert to evaluate variables, :name() to refer to columns, "\[   ]\" to allow double quotes inside a string, and finally use eval(parse()) to run the resulting string.

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

MyList = {"age", "height"};
ParamPlot = {"age2", "height2"};
For( j = 1, j <= N Items( MyList ), j++,
    new_col_expr = evalinsert("\[dt << New Column("^paramplot[j]^", Formula(2 * :name("^mylist[j]^")))]\");
    eval(parse(new_col_expr));
);

View solution in original post

7 REPLIES 7

Re: how to pass a column as parameter in a formula

The problem is that the argument to Formula() is treated as a literal expression. It does not evaluate ParamPlot[j] and substitute the result to make the formula you want. It seems like you should be able to use expressions but I got no where with that approach so here is a solution based on character strings instead:

Names Default To Here( 1 );

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

MyList = {"age", "height"};
ParamPlot = {"age2", "height2"};

For( j = 1, j <= N Items( MyList ), j++,
	Eval(
		Parse(
			Substitute("dt << New Column( \!"nnn\!", Formula( 2 * :ppp ) )",
				"nnn",
				ParamPlot[j],
				"ppp",
				MyList[j]
			)
		);
	);
);

I really don't like using character strings but sometimes, you gotta do what you gotta do.

 

ian_jmp
Level X

Re: how to pass a column as parameter in a formula

Maybe this is slightly better (in the eyes of some):

Names Default To Here( 1 );

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

MyList = {"age", "height"};
ParamPlot = {"age2", "height2"};

For( j = 1, j <= N Items( MyList ), j++,
	CMD = Expr(dt << New Column( NameTBD, Formula( 2 * colTBD) ));
	SubstituteInto(CMD, Expr(NameTBD), ParamPlot[j], Expr(ColTBD), Parse(":"||MyList[j]));
	CMD;
);

 

Byron_JMP
Staff

Re: how to pass a column as parameter in a formula

it looks like you're trying to make a formula that multiplies its self by 2. this won't work and should return an empty value. 

Also, "j" is a function for making matricies (note that it turns blue in the JSL editor) so I usually avoid using "j" as a variable just in case it might get interpeted in an unexpected way.

 

Working with column names and references can be pretty frustrating. Take a look at this strategy.

Inside the loop, first the column is created, and as its created, its given a handle "newcol" to use as a reference. Next there is the big eval(substitute)) clause.  Inside this argument, the "col<<formula()" is defined as an expression (expressons aren't evaluated) with a place holder for the column name. Then that place holder is substuted for the column name. Still nothing is evaluated. After the substitution, the whole thing gets evaluated, because it wrapped in Eval. 

 

 

 

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
MyList = {"age", "height"};
ParamPlot = {"age2", "height2"};
For( i = 1, i <= N Items( MyList ), i++,
	newcol=dt << New Column( ParamPlot[i] );
	Eval(
		Substitute( 
			Expr( 
				newcol << set formula( a * 2 ) ) ,
			Expr( a ), As Name( mylist[i] )
	);
));
JMP Systems Engineer, Health and Life Sciences (Pharma)
pmroz
Super User

Re: how to pass a column as parameter in a formula

As Byron said you are trying to create a new column that is 2 times itself.  I think this is what you want.  AS COLUMN did the trick.

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

MyList = {"age", "height"};
ParamPlot = {"age2", "height2"};
For( j = 1, j <= N Items( MyList ), j++,
	dt << New Column( ParamPlot[j], Formula( 2 * as column(dt, MyList[j]) ) )
);

 This works great as long as you don't add rows.  Then you'll need the evals, exprs etc.

samir
Level IV

Re: how to pass a column as parameter in a formula

First of all, thank you all for your answers...
Here is my view on all the solutions proposed:
1/ The solution proposed by Mark: Works perfectly (though difficult to read when not used to Eval, parse, Substitue,...).
2/ The solution proposed by Jan: Indeed, better in the eyes (also uses eval, substituteinto,...). By the way it works perfectly.
3/ The Solution of Byron: Also uses Eval, substitute, expr...Works perfectly
4/ The solution of pmroz is by far the simplest (to the eyes), but the formula we get is still referenced to MyList and not to the columns they should represent...Meaning that if I change a value in the column "age" , the column "age2" will not be updated.

Thank you all for the suggestions :)

pmroz
Super User

Re: how to pass a column as parameter in a formula

I couldn't resist one last take.  Short and sweet and it works.  Use evalinsert to evaluate variables, :name() to refer to columns, "\[   ]\" to allow double quotes inside a string, and finally use eval(parse()) to run the resulting string.

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

MyList = {"age", "height"};
ParamPlot = {"age2", "height2"};
For( j = 1, j <= N Items( MyList ), j++,
    new_col_expr = evalinsert("\[dt << New Column("^paramplot[j]^", Formula(2 * :name("^mylist[j]^")))]\");
    eval(parse(new_col_expr));
);
samir
Level IV

Re: how to pass a column as parameter in a formula

Short and efficient :)

Thanks a lot :)