<?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: JSL: Creating formula using a variable column name in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/950#M950</link>
    <description>I now made the workaround to read the values out of the column into a vector and write the calculated values in a new column.&lt;BR /&gt;&lt;BR /&gt;But I'm still interested in answers for the open questions.&lt;BR /&gt;&lt;BR /&gt;Dahla</description>
    <pubDate>Wed, 18 Nov 2009 13:26:03 GMT</pubDate>
    <dc:creator />
    <dc:date>2009-11-18T13:26:03Z</dc:date>
    <item>
      <title>JSL: Creating formula using a variable column name</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/942#M942</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;I want to do an actually simple thing (at least I thought so), namely to create a new column using JSL with a formula that contains a variable column name. I already tried several combinations of the commands "column, as column, name, as name, eval, parse .." and I searched the forum, but everything I tried results in an error that the formula could not be evaluated.&lt;BR /&gt;&lt;BR /&gt;An example what I want to do: &lt;BR /&gt;"column 1" is a column in my data table.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;a = "column 1";
new column("test", formula(a*2));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The column "test" should contain the formula that multiplies "column 1" with 2.&lt;BR /&gt;&lt;BR /&gt;Thanks for any help,&lt;BR /&gt;Dahla&lt;/P&gt;</description>
      <pubDate>Thu, 31 Aug 2017 08:24:18 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/942#M942</guid>
      <dc:creator />
      <dc:date>2017-08-31T08:24:18Z</dc:date>
    </item>
    <item>
      <title>Re: JSL: Creating formula using a variable column name</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/943#M943</link>
      <description>&lt;P&gt;Sometimes the simplest things can be mindboggling in JSL :)&lt;/img&gt;&lt;BR /&gt;&lt;BR /&gt;Here are a couple of approaches:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;new column("test",formula( as column("column 1")*2 ));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;a = "column 1";
new column("test",formula( a[]*2 ));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;The logic of the syntax is something like this. In the first example we want to treat "column 1" as a column of numbers. In the second example, "[]" indicates that a is an array of numbers. &lt;BR /&gt;&lt;BR /&gt;Hope this helps&lt;/P&gt;</description>
      <pubDate>Fri, 18 Aug 2017 21:27:09 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/943#M943</guid>
      <dc:creator />
      <dc:date>2017-08-18T21:27:09Z</dc:date>
    </item>
    <item>
      <title>Re: JSL: Creating formula using a variable column name</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/944#M944</link>
      <description>Thanks for the answer, but unfortunately it doesn't help. &lt;BR /&gt;&lt;BR /&gt;The first approach is not what I wanted because I need to take the column name out of a variable (it can change). &lt;BR /&gt;&lt;BR /&gt;The second approach does not work, because JMP doesn't insert the real column name into the formula. If you go into the dialog for changing the formula, it reads "a[Empty()] * 2" in your suggestion, and I need something like ":Column1 * 2" there.&lt;BR /&gt;&lt;BR /&gt;Any other suggestions?</description>
      <pubDate>Mon, 16 Nov 2009 08:10:00 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/944#M944</guid>
      <dc:creator />
      <dc:date>2009-11-16T08:10:00Z</dc:date>
    </item>
    <item>
      <title>Re: JSL: Creating formula using a variable column name</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/945#M945</link>
      <description>&lt;P&gt;I think my 2nd example should have read like this:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;a = Column("column 1");
new column("test",formula( a[]*2 ));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Aug 2017 21:28:02 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/945#M945</guid>
      <dc:creator />
      <dc:date>2017-08-18T21:28:02Z</dc:date>
    </item>
    <item>
      <title>Re: JSL: Creating formula using a variable column name</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/946#M946</link>
      <description>Thank you, that works!</description>
      <pubDate>Mon, 16 Nov 2009 14:53:06 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/946#M946</guid>
      <dc:creator />
      <dc:date>2009-11-16T14:53:06Z</dc:date>
    </item>
    <item>
      <title>Re: JSL: Creating formula using a variable column name</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/947#M947</link>
      <description>&lt;P&gt;I still have an additional question: &lt;BR /&gt;&lt;BR /&gt;What I finally want to do is to create a new column for each item of a list. In Stig's solution the column formula still depends on the variable "a[]" and not (as I'd prefer) on the column name "Column 1", so I'm not allowed to change "a[]" in the following script without changing the calculated column values as well. That means my following approach does not work (the length of collist and the column names can change, so I need a loop):&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;collist = {"column 1", "column 2"};

for (k=1, k&amp;lt;=length(collist), k++,
a = Column(collist);
new column(concat("test",char(k)), formula( a[]*2 ));
); 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Directly inserting a list element in the formula() statement does not work either. Does anyone have another suggestion? &lt;BR /&gt;&lt;BR /&gt;Dahla&lt;/P&gt;</description>
      <pubDate>Thu, 31 Aug 2017 08:26:00 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/947#M947</guid>
      <dc:creator />
      <dc:date>2017-08-31T08:26:00Z</dc:date>
    </item>
    <item>
      <title>Re: JSL: Creating formula using a variable column name</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/948#M948</link>
      <description>&lt;P&gt;If I use the following code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = Current Data Table();
a = Column("height");
dt &amp;lt;&amp;lt; New Column("test1",formula(a[]*2));
a = Column("weight");
dt &amp;lt;&amp;lt; New Column("test2",formula(a[]*2));
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;both columns test1 and test2 are computed correctly even though the formula for both reference the same variable a. So in principle you shouldn't have a problem with the code within a loop. However, this approach doesn't work if you want the new columns to retain their behaviour in the table as computed columns - which leaves open the question still open of how to generate from code a column with the formula ":column 1 * 2" ...&lt;/P&gt;</description>
      <pubDate>Thu, 31 Aug 2017 08:26:42 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/948#M948</guid>
      <dc:creator />
      <dc:date>2017-08-31T08:26:42Z</dc:date>
    </item>
    <item>
      <title>Re: JSL: Creating formula using a variable column name</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/949#M949</link>
      <description>Oh, I see only now that my previous post was not fully displayed... Sorry for that.&lt;BR /&gt;&lt;BR /&gt;Yes, I need the columns to retain their behaviour or at least the values. Thats why I had the idea to remove the formulas directly after calculating them. But I had the problem that I did not find a statement that does the same as "Formula... -&amp;gt; Clear", which means to remove the formula and retain the values as they are. Does someone know how this works?&lt;BR /&gt;That would not be the nicest solution, but enough for me at the moment.</description>
      <pubDate>Wed, 18 Nov 2009 07:16:43 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/949#M949</guid>
      <dc:creator />
      <dc:date>2009-11-18T07:16:43Z</dc:date>
    </item>
    <item>
      <title>Re: JSL: Creating formula using a variable column name</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/950#M950</link>
      <description>I now made the workaround to read the values out of the column into a vector and write the calculated values in a new column.&lt;BR /&gt;&lt;BR /&gt;But I'm still interested in answers for the open questions.&lt;BR /&gt;&lt;BR /&gt;Dahla</description>
      <pubDate>Wed, 18 Nov 2009 13:26:03 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/950#M950</guid>
      <dc:creator />
      <dc:date>2009-11-18T13:26:03Z</dc:date>
    </item>
    <item>
      <title>Re: JSL: Creating formula using a variable column name</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/951#M951</link>
      <description>&lt;P&gt;After trying to come up with a working solution I finally gave up and asked JMP support how this might be done. Wendy kindly showed me the key lines and I embedded them in a for loop as a demo. The code I am including assumes that the current data table has 2 columns named abc and def which have a few rows of data of your choice. By the way there exists a new SAS Press book called Jump into JMP Scripting by Wendy Murphrey and Rosemary Lucas which is a very helpful book on scripting. It does cover Expr and Eval Expr used in the example.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = Current Data Table();
cols = {abc, def};
nlist = N Items(cols);
 
For( i = 1, i &amp;lt;= nlist, i++,
     newColExpr = Expr(
          dt &amp;lt;&amp;lt; New Column( char(cols[i])||"*2", Numeric, Formula( Expr( cols[i] ) * 2 ) )
     );
     Eval( Eval Expr( newColExpr ) );
)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 15 Oct 2017 13:40:38 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/951#M951</guid>
      <dc:creator>mpb</dc:creator>
      <dc:date>2017-10-15T13:40:38Z</dc:date>
    </item>
    <item>
      <title>Re: JSL: Creating formula using a variable column name</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/952#M952</link>
      <description>&lt;P&gt;I tried but cant get it work on a variant.&lt;BR /&gt;&lt;BR /&gt;There are two column where the user chooses in a custom dialog box, and i assign them the names of lx,ly.&lt;BR /&gt;I need a new column with formula&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Radius = SQRT((X^2)+(Y^2))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, when i run it, althought the new column is created, JMP prompt me &lt;BR /&gt;"N Items() argument must be a list", and no formula created.&lt;BR /&gt;&lt;BR /&gt;Appreciate if a kind soul can advice on which part is faulty. Thanks.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;customDlg &amp;lt;&amp;lt; CloseWindow;
lp = colListP &amp;lt;&amp;lt; GetItems;
lx = colListX &amp;lt;&amp;lt; GetItems;
ly = colListY &amp;lt;&amp;lt; GetItems;
 
// Are the user selections viable?
if (NItems(lp) &amp;lt; 1,
     Dialog("ERROR: Select one or more Parameter Columns", Button("OK")); Throw());
if (NItems(lx) != 1,
     Dialog("ERROR: You need to select an X Column", Button("OK")); Throw(),
     xVar = lx(1)     
     );
if (NItems(ly) != 1,
     Dialog("ERROR: You need to select a Y Column", Button("OK")); Throw(),
     yVar = ly(1)
     );
 
 
// *********************************************************************************
// Build New Column with new formula
// *********************************************************************************
 
dt = Current Data Table();
a = Column(dt,xvar);
b = Column(dt,yvar);
colx = ;
coly = ;
nlist = N Items(a);
For( i = 1, i &amp;lt;= nlist, i++,     
newColExpr = Expr(          
               dt &amp;lt;&amp;lt; New Column( "Radius", Numeric,
               Formula( Root((Power(Expr(a(i)),2)+Power(Expr(b(i)),2)),2 ) )     );     
               Eval( Eval Expr( newColExpr ) );
);
);
);&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;</description>
      <pubDate>Thu, 31 Aug 2017 08:28:04 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/952#M952</guid>
      <dc:creator />
      <dc:date>2017-08-31T08:28:04Z</dc:date>
    </item>
    <item>
      <title>Re: JSL: Creating formula using a variable column name</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/953#M953</link>
      <description>&lt;P&gt;Here's an example using the Column Dialog which gives the user the choice to select exactly 2 columns from which the Radius column will be created.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = Current Data Table();
cdlg = Column Dialog( clist = ColList( "Pick 2 Columns", Min Col( 2 ), Max Col( 2 ) ) );
cols = cdlg["clist"];
 
newColExpr = Expr( dt &amp;lt;&amp;lt; New Column( "Radius", Numeric, Formula( Sqrt( Expr( cols&amp;amp;#91;1&amp;amp;#93; ) ^ 2 + Expr( cols&amp;amp;#91;2&amp;amp;#93; ) ^ 2 ) ) ) );
 
Eval( Eval Expr( newColExpr ) );&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Jan 2017 14:52:38 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/953#M953</guid>
      <dc:creator>mpb</dc:creator>
      <dc:date>2017-01-04T14:52:38Z</dc:date>
    </item>
    <item>
      <title>Re: JSL: Creating formula using a variable column name</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/954#M954</link>
      <description>Thanks for the help. Will try it out.&lt;BR /&gt;=)&lt;/img&gt;</description>
      <pubDate>Fri, 20 Nov 2009 22:37:14 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/954#M954</guid>
      <dc:creator />
      <dc:date>2009-11-20T22:37:14Z</dc:date>
    </item>
    <item>
      <title>Re: JSL: Creating formula using a variable column name</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/955#M955</link>
      <description>@mpb: That works fine! Thanks for finding out and for the book tip!</description>
      <pubDate>Mon, 23 Nov 2009 15:14:14 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Creating-formula-using-a-variable-column-name/m-p/955#M955</guid>
      <dc:creator />
      <dc:date>2009-11-23T15:14:14Z</dc:date>
    </item>
  </channel>
</rss>

