<?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: call a column by referring  to variable in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/call-a-column-by-referring-to-variable/m-p/272349#M52970</link>
    <description>&lt;P&gt;You've already created variables that reference the columns.&amp;nbsp; You can use those in your formula, but you need to use the AsColumn function:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;formula( AsColumn(columnname1) + AsColumn(columnname2) )&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 12 Jun 2020 09:29:16 GMT</pubDate>
    <dc:creator>David_Burnham</dc:creator>
    <dc:date>2020-06-12T09:29:16Z</dc:date>
    <item>
      <title>call a column by referring  to variable</title>
      <link>https://community.jmp.com/t5/Discussions/call-a-column-by-referring-to-variable/m-p/272339#M52967</link>
      <description>&lt;P&gt;A complete newbie here,&lt;/P&gt;
&lt;P&gt;could not find the solution in older posts,&lt;/P&gt;
&lt;P&gt;I have created a variable containing an existing column name. I want to create a new column, were the data from two other columns is summed (calling the columns by variables). the usage of variable is for later use in a for loop.&lt;/P&gt;
&lt;P&gt;Thanks for helping!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;for the below code:&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 = Current Data Table();

columnname1 = Column( dt, 1 ) &amp;lt;&amp;lt; Get Name;
columnname2 = Column( dt, 2 ) &amp;lt;&amp;lt; Get Name;

dt &amp;lt;&amp;lt; New Column( "new_" || columnname1, Numeric , continuous, formula(column(dt, columnname1)+column(dt, columnname2)));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I get the following error:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Column new_Column 1 2 Formula Interrupted&lt;BR /&gt;Cannot convert argument to a number [or matrix] 1 times At rows: 2 Operation: Add, Column( dt, columnname1 ) + /*###*/Column( dt, columnname2 ) /*###*/&lt;BR /&gt;Formula evaluation errors have been ignored&lt;/P&gt;
&lt;P&gt;Column( "new_Column 1 2" )&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 10 Jun 2023 23:14:38 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/call-a-column-by-referring-to-variable/m-p/272339#M52967</guid>
      <dc:creator>YBerger</dc:creator>
      <dc:date>2023-06-10T23:14:38Z</dc:date>
    </item>
    <item>
      <title>Re: call a column by referring  to variable</title>
      <link>https://community.jmp.com/t5/Discussions/call-a-column-by-referring-to-variable/m-p/272349#M52970</link>
      <description>&lt;P&gt;You've already created variables that reference the columns.&amp;nbsp; You can use those in your formula, but you need to use the AsColumn function:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;formula( AsColumn(columnname1) + AsColumn(columnname2) )&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 12 Jun 2020 09:29:16 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/call-a-column-by-referring-to-variable/m-p/272349#M52970</guid>
      <dc:creator>David_Burnham</dc:creator>
      <dc:date>2020-06-12T09:29:16Z</dc:date>
    </item>
    <item>
      <title>Re: call a column by referring  to variable</title>
      <link>https://community.jmp.com/t5/Discussions/call-a-column-by-referring-to-variable/m-p/272352#M52971</link>
      <description>&lt;P&gt;You can get it to work, with&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/big class.jmp" );
columnname1 = Column( dt, 4 ) &amp;lt;&amp;lt; Get Name;
columnname2 = Column( dt, 5 ) &amp;lt;&amp;lt; Get Name;

dt &amp;lt;&amp;lt; New Column( "new_" || columnname1,
	Numeric,
	continuous,
	formula(
		As Column( dt, columnname1 ) + As Column( dt, columnname2 )
	)
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, there is a potential problem with this.&amp;nbsp; The stored formula is:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="stored.PNG" style="width: 680px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/24560i04531CF3BD35462E/image-size/large?v=v2&amp;amp;px=999" role="button" title="stored.PNG" alt="stored.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;This formula will continue to work, as long as your JMP session&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Has the variables, dt, columnname1 and columnname2 defined.&amp;nbsp; What happens if you save the data table, shutdown JMP, and then tomorrow you come in and startup JMP, and open the data table.&amp;nbsp; The formula will not find those variables, and the formula will fail.&lt;/LI&gt;
&lt;LI&gt;Within the session, the variables, dt, columnname1 and columnname2, do not change their value.&amp;nbsp; So, if you create this column, and later on in your script, you happen to change the value of variable columnname1, since formula columns in a JMP data table are dynamic, the formula will rerun when changes to the data table take place, JMP will use the new value of the variable, and will create new values for the formula column.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;What you really need to do, is to use a method that creates the actual JSL syntax for the formula.&amp;nbsp; In the above example, what you really want is for the formula to be&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;:height + :weight&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The JSL below is one way to do this.&amp;nbsp; It uses the Substitute() function to implant the actual column names into the formula.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/big class.jmp" );
columnname1 = Column( dt, 4 ) &amp;lt;&amp;lt; Get Name;
columnname2 = Column( dt, 5 ) &amp;lt;&amp;lt; Get Name;

Eval(
	Substitute(
			Expr(
				dt &amp;lt;&amp;lt; New Column( "new_" || columnname1,
					Numeric,
					continuous,
					formula( __c1__ + __c2__ )
				)
			),
		Expr( __c1__ ), Parse( ":" || columnname1 ),
		Expr( __c2__ ), Parse( ":" || columnname2 )
	)
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 12 Jun 2020 09:41:58 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/call-a-column-by-referring-to-variable/m-p/272352#M52971</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2020-06-12T09:41:58Z</dc:date>
    </item>
    <item>
      <title>Re: call a column by referring  to variable</title>
      <link>https://community.jmp.com/t5/Discussions/call-a-column-by-referring-to-variable/m-p/272385#M52976</link>
      <description>&lt;P&gt;thank you for your quick reply. it works!&lt;/P&gt;</description>
      <pubDate>Fri, 12 Jun 2020 12:30:24 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/call-a-column-by-referring-to-variable/m-p/272385#M52976</guid>
      <dc:creator>YBerger</dc:creator>
      <dc:date>2020-06-12T12:30:24Z</dc:date>
    </item>
    <item>
      <title>Re: call a column by referring  to variable</title>
      <link>https://community.jmp.com/t5/Discussions/call-a-column-by-referring-to-variable/m-p/272402#M52979</link>
      <description>&lt;P&gt;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/2687"&gt;@txnelson&lt;/a&gt;&amp;nbsp;is quite right about this.&amp;nbsp; The method I suggested was a lazy option and in hindsight not an approach I would recommend.&amp;nbsp; If you just want a column with correct values you can do a calculation in a loop and not bother with a formula; if the formula is important to you then you need to check that your script has correctly encoded the formula - it's not sufficient for the numbers to appear correct.&amp;nbsp; Script variables will not persist beyond the lifetime of the script,&amp;nbsp; so the formula needs to correctly include column references - this is achieved by using expression-substitution techniques such as the one illustrated by Jim.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Jun 2020 13:33:58 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/call-a-column-by-referring-to-variable/m-p/272402#M52979</guid>
      <dc:creator>David_Burnham</dc:creator>
      <dc:date>2020-06-12T13:33:58Z</dc:date>
    </item>
    <item>
      <title>Re: call a column by referring  to variable</title>
      <link>https://community.jmp.com/t5/Discussions/call-a-column-by-referring-to-variable/m-p/767585#M94775</link>
      <description>&lt;P&gt;Just for future readers of this question:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I used the above code to pass string column names to formulas but had problems with the use of&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Parse(":" || column_name)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Parse is vulnerable to odd column names since column names like "a - b" would be interpreted as column "a" minus variable "b".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Instead use&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;As Name(columnname)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;which fixes this issue.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jun 2024 08:57:07 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/call-a-column-by-referring-to-variable/m-p/767585#M94775</guid>
      <dc:creator>NisKorsgaard</dc:creator>
      <dc:date>2024-06-21T08:57:07Z</dc:date>
    </item>
  </channel>
</rss>

