<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: how to pass a column as parameter in a formula in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/how-to-pass-a-column-as-parameter-in-a-formula/m-p/43473#M25175</link>
    <description>&lt;P&gt;First of all, thank you all for your answers...&lt;BR /&gt;Here is my view on all the solutions proposed:&lt;BR /&gt;1/ The solution proposed by Mark: Works perfectly (though difficult to read when not used to Eval, parse, Substitue,...).&lt;BR /&gt;2/ The solution proposed by Jan: Indeed, better in the eyes (also uses eval, substituteinto,...). By the way it works perfectly.&lt;BR /&gt;3/ The Solution of Byron: Also uses Eval, substitute, expr...Works perfectly&lt;BR /&gt;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.&lt;/P&gt;&lt;P&gt;Thank you all for the suggestions :)&lt;/img&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 22 Aug 2017 13:17:54 GMT</pubDate>
    <dc:creator>samir</dc:creator>
    <dc:date>2017-08-22T13:17:54Z</dc:date>
    <item>
      <title>how to pass a column as parameter in a formula</title>
      <link>https://community.jmp.com/t5/Discussions/how-to-pass-a-column-as-parameter-in-a-formula/m-p/43437#M25151</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am trying to create a simple formula on columns that are stored as variables in a list.&lt;/P&gt;&lt;P&gt;I do not understand why JSL refuses to perform the formula.&lt;/P&gt;&lt;P&gt;If I change in the formula the parameter by hte string it stores, it works perfectly.&lt;/P&gt;&lt;P&gt;Any help ??&lt;/P&gt;&lt;P&gt;Here is the code I use:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

MyList = {"age", "height"};
ParamPlot = {"age2", "height2"};
for(j=1, j &amp;lt;= N items(MyList), j++,
 dt &amp;lt;&amp;lt; New Column( ParamPlot[j], Formula( 2* ParamPlot[j]) );
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried things like Eval, Eval expr,...but noway :(&lt;/img&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Aug 2017 11:53:09 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/how-to-pass-a-column-as-parameter-in-a-formula/m-p/43437#M25151</guid>
      <dc:creator>samir</dc:creator>
      <dc:date>2017-08-21T11:53:09Z</dc:date>
    </item>
    <item>
      <title>Re: how to pass a column as parameter in a formula</title>
      <link>https://community.jmp.com/t5/Discussions/how-to-pass-a-column-as-parameter-in-a-formula/m-p/43439#M25153</link>
      <description>&lt;P&gt;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:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

MyList = {"age", "height"};
ParamPlot = {"age2", "height2"};

For( j = 1, j &amp;lt;= N Items( MyList ), j++,
	Eval(
		Parse(
			Substitute("dt &amp;lt;&amp;lt; New Column( \!"nnn\!", Formula( 2 * :ppp ) )",
				"nnn",
				ParamPlot[j],
				"ppp",
				MyList[j]
			)
		);
	);
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I really don't like using character strings but sometimes, you gotta do what you gotta do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Aug 2017 13:27:42 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/how-to-pass-a-column-as-parameter-in-a-formula/m-p/43439#M25153</guid>
      <dc:creator>Mark_Bailey</dc:creator>
      <dc:date>2017-08-21T13:27:42Z</dc:date>
    </item>
    <item>
      <title>Re: how to pass a column as parameter in a formula</title>
      <link>https://community.jmp.com/t5/Discussions/how-to-pass-a-column-as-parameter-in-a-formula/m-p/43441#M25155</link>
      <description>&lt;P&gt;Maybe this is slightly better (in the eyes of some):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

MyList = {"age", "height"};
ParamPlot = {"age2", "height2"};

For( j = 1, j &amp;lt;= N Items( MyList ), j++,
	CMD = Expr(dt &amp;lt;&amp;lt; New Column( NameTBD, Formula( 2 * colTBD) ));
	SubstituteInto(CMD, Expr(NameTBD), ParamPlot[j], Expr(ColTBD), Parse(":"||MyList[j]));
	CMD;
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Aug 2017 14:36:35 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/how-to-pass-a-column-as-parameter-in-a-formula/m-p/43441#M25155</guid>
      <dc:creator>ian_jmp</dc:creator>
      <dc:date>2017-08-21T14:36:35Z</dc:date>
    </item>
    <item>
      <title>Re: how to pass a column as parameter in a formula</title>
      <link>https://community.jmp.com/t5/Discussions/how-to-pass-a-column-as-parameter-in-a-formula/m-p/43445#M25158</link>
      <description>&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Working with column names and references can be pretty frustrating. Take a look at this strategy.&lt;/P&gt;
&lt;P&gt;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. &amp;nbsp;Inside this argument, the "col&amp;lt;&amp;lt;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.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
MyList = {"age", "height"};
ParamPlot = {"age2", "height2"};
For( i = 1, i &amp;lt;= N Items( MyList ), i++,
	newcol=dt &amp;lt;&amp;lt; New Column( ParamPlot[i] );
	Eval(
		Substitute( 
			Expr( 
				newcol &amp;lt;&amp;lt; set formula( a * 2 ) ) ,
			Expr( a ), As Name( mylist[i] )
	);
));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Aug 2017 15:54:16 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/how-to-pass-a-column-as-parameter-in-a-formula/m-p/43445#M25158</guid>
      <dc:creator>Byron_JMP</dc:creator>
      <dc:date>2017-08-21T15:54:16Z</dc:date>
    </item>
    <item>
      <title>Re: how to pass a column as parameter in a formula</title>
      <link>https://community.jmp.com/t5/Discussions/how-to-pass-a-column-as-parameter-in-a-formula/m-p/43447#M25160</link>
      <description>&lt;P&gt;As Byron said you are trying to create a new column that is 2 times itself. &amp;nbsp;I think this is what you want. &amp;nbsp;AS COLUMN did the trick.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

MyList = {"age", "height"};
ParamPlot = {"age2", "height2"};
For( j = 1, j &amp;lt;= N Items( MyList ), j++,
	dt &amp;lt;&amp;lt; New Column( ParamPlot[j], Formula( 2 * as column(dt, MyList[j]) ) )
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;This works great as long as you don't add rows. &amp;nbsp;Then you'll need the evals, exprs etc.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Aug 2017 17:40:23 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/how-to-pass-a-column-as-parameter-in-a-formula/m-p/43447#M25160</guid>
      <dc:creator>pmroz</dc:creator>
      <dc:date>2017-08-21T17:40:23Z</dc:date>
    </item>
    <item>
      <title>Re: how to pass a column as parameter in a formula</title>
      <link>https://community.jmp.com/t5/Discussions/how-to-pass-a-column-as-parameter-in-a-formula/m-p/43473#M25175</link>
      <description>&lt;P&gt;First of all, thank you all for your answers...&lt;BR /&gt;Here is my view on all the solutions proposed:&lt;BR /&gt;1/ The solution proposed by Mark: Works perfectly (though difficult to read when not used to Eval, parse, Substitue,...).&lt;BR /&gt;2/ The solution proposed by Jan: Indeed, better in the eyes (also uses eval, substituteinto,...). By the way it works perfectly.&lt;BR /&gt;3/ The Solution of Byron: Also uses Eval, substitute, expr...Works perfectly&lt;BR /&gt;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.&lt;/P&gt;&lt;P&gt;Thank you all for the suggestions :)&lt;/img&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Aug 2017 13:17:54 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/how-to-pass-a-column-as-parameter-in-a-formula/m-p/43473#M25175</guid>
      <dc:creator>samir</dc:creator>
      <dc:date>2017-08-22T13:17:54Z</dc:date>
    </item>
    <item>
      <title>Re: how to pass a column as parameter in a formula</title>
      <link>https://community.jmp.com/t5/Discussions/how-to-pass-a-column-as-parameter-in-a-formula/m-p/43479#M25180</link>
      <description>&lt;P&gt;I couldn't resist one last take. &amp;nbsp;Short and sweet and it works. &amp;nbsp;Use &lt;STRONG&gt;evalinsert&lt;/STRONG&gt; to evaluate variables, &lt;STRONG&gt;:name()&lt;/STRONG&gt; to refer to columns,&amp;nbsp;&lt;STRONG&gt;"\[ &amp;nbsp; ]\"&lt;/STRONG&gt; to allow double quotes inside a string, and finally use &lt;STRONG&gt;eval(parse())&lt;/STRONG&gt; to run the resulting string.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

MyList = {"age", "height"};
ParamPlot = {"age2", "height2"};
For( j = 1, j &amp;lt;= N Items( MyList ), j++,
    new_col_expr = evalinsert("\[dt &amp;lt;&amp;lt; New Column("^paramplot[j]^", Formula(2 * :name("^mylist[j]^")))]\");
    eval(parse(new_col_expr));
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 22 Aug 2017 14:56:11 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/how-to-pass-a-column-as-parameter-in-a-formula/m-p/43479#M25180</guid>
      <dc:creator>pmroz</dc:creator>
      <dc:date>2017-08-22T14:56:11Z</dc:date>
    </item>
    <item>
      <title>Re: how to pass a column as parameter in a formula</title>
      <link>https://community.jmp.com/t5/Discussions/how-to-pass-a-column-as-parameter-in-a-formula/m-p/43511#M25189</link>
      <description>&lt;P&gt;Short and efficient :)&lt;/img&gt;&lt;/P&gt;&lt;P&gt;Thanks a lot :)&lt;/img&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Aug 2017 07:42:42 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/how-to-pass-a-column-as-parameter-in-a-formula/m-p/43511#M25189</guid>
      <dc:creator>samir</dc:creator>
      <dc:date>2017-08-23T07:42:42Z</dc:date>
    </item>
  </channel>
</rss>

