<?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: Referencing a Column by Column Number in Formula in a For loop in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Referencing-a-Column-by-Column-Number-in-Formula-in-a-For-loop/m-p/74706#M35850</link>
    <description>&lt;P&gt;My error.&amp;nbsp; The string needs to be parsed to get rid of the pair of quotes&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Current Data Table();
sample = {0, 1, 2};
new columns = N Items( sample );

For( i = 1, i &amp;lt;= new columns, i++,
	col = Num( sample[i] );
	colNamesList = dt &amp;lt;&amp;lt; Get Column Names;
	Wait( 0 );
	Eval(
		Substitute(
				Expr(
					dt &amp;lt;&amp;lt; New Column( sample[i] || " Days",
						formula( __column__ - :Name( "0" ) )
					)
				),
			Expr( __column__ ),
				Parse(":Name(\!"" || colNamesList[13 + i + new columns] || "\!")")
		)
	);
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 20 Sep 2018 19:11:56 GMT</pubDate>
    <dc:creator>txnelson</dc:creator>
    <dc:date>2018-09-20T19:11:56Z</dc:date>
    <item>
      <title>Referencing a Column by Column Number in Formula in a For loop</title>
      <link>https://community.jmp.com/t5/Discussions/Referencing-a-Column-by-Column-Number-in-Formula-in-a-For-loop/m-p/74335#M35808</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to generate a bunch of new columns at once using a "for" loop, and each column will get named as the next item in the "sample" matrix. I can figure that part out, I think. But I also want each column to include a formula to look up a different column, which will be different each time, and subtract that value from the initial value (column "0"). When I run this it just gives me formula evaluation errors, not calculating what the column should be that it references.&lt;/P&gt;&lt;P&gt;Thanks for your help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;sample = {0, 1, 2, ...}
new columns = N Items (sample);

for( i=1, i&amp;lt;= new columns, i++, col = num(sample [i]);
	
	dt &amp;lt;&amp;lt; New Column (sample [i] || " Days",  formula (as column (13+i+new columns) - :Name("0")))
	 	
	 );&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Sep 2018 22:34:04 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Referencing-a-Column-by-Column-Number-in-Formula-in-a-For-loop/m-p/74335#M35808</guid>
      <dc:creator>Jackieandrews</dc:creator>
      <dc:date>2018-09-19T22:34:04Z</dc:date>
    </item>
    <item>
      <title>Re: Referencing a Column by Column Number in Formula in a For loop</title>
      <link>https://community.jmp.com/t5/Discussions/Referencing-a-Column-by-Column-Number-in-Formula-in-a-For-loop/m-p/74534#M35816</link>
      <description>&lt;P&gt;The issue with your current formula, is the variables "i".and "New Columns".&amp;nbsp; What you are assuming is that the calculation will be evalueated to a final calculation, and then placed into the As Column() function.&amp;nbsp; The issue is that what is placed into the formula is As Column( 13 + i + new columns ).&amp;nbsp; So when the formula is executed, it does not have the values of "i" and "new columns" when you created the column, but it uses whatever the values are whenever it calculates or recalculates the formulas.&amp;nbsp; And since "i" and "New Columns" is changing within your script, you have an issue.&amp;nbsp; Now you could change the formula to do the calculation before defining the formula, and then substitute in the column number into the formula.&amp;nbsp; However, the issue with this is that by using column numbers, if a column in the table is moved, or deleted, the column number in the formula will not be changed and therefore, the calculation will be wrong.&amp;nbsp; I suggest that you instead, substitute into the formula, the actual column name.&amp;nbsp; The below script illustrates the approach to do that.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Current Data Table();
sample = {0, 1, 2};
new columns = N Items( sample );

For( i = 1, i &amp;lt;= new columns, i++,
	col = Num( sample[i] );
	colNamesList = dt &amp;lt;&amp;lt; Get Column Names;
	Wait( 0 );
	Eval(
		Substitute(
				Expr(
					dt &amp;lt;&amp;lt; New Column( sample[i] || " Days",
						formula( __column__ - :Name( "0" ) )
					)
				),
			Expr( __column__ ),
				":Name(\!"" || colNamesList[13 + i + new columns] || "\!")"
		)
	);
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 Sep 2018 06:00:45 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Referencing-a-Column-by-Column-Number-in-Formula-in-a-For-loop/m-p/74534#M35816</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2018-09-20T06:00:45Z</dc:date>
    </item>
    <item>
      <title>Re: Referencing a Column by Column Number in Formula in a For loop</title>
      <link>https://community.jmp.com/t5/Discussions/Referencing-a-Column-by-Column-Number-in-Formula-in-a-For-loop/m-p/74682#M35845</link>
      <description>&lt;P&gt;Thank you for your help!! This solution almost works - but I am stuck on one last bit.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Currently, the resulting spreadsheet gives me the following formula.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;":Name(\!"5\!")" - 0&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want the first half to reference a column called "5", like the second half references a column called "0". No matter what I do, I can't get the first half to just show :Name("5"). When I reformat the quotation marks or try moving them, I just get variations on that theme.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again!&lt;/P&gt;</description>
      <pubDate>Thu, 20 Sep 2018 17:17:22 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Referencing-a-Column-by-Column-Number-in-Formula-in-a-For-loop/m-p/74682#M35845</guid>
      <dc:creator>Jackieandrews</dc:creator>
      <dc:date>2018-09-20T17:17:22Z</dc:date>
    </item>
    <item>
      <title>Re: Referencing a Column by Column Number in Formula in a For loop</title>
      <link>https://community.jmp.com/t5/Discussions/Referencing-a-Column-by-Column-Number-in-Formula-in-a-For-loop/m-p/74706#M35850</link>
      <description>&lt;P&gt;My error.&amp;nbsp; The string needs to be parsed to get rid of the pair of quotes&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Current Data Table();
sample = {0, 1, 2};
new columns = N Items( sample );

For( i = 1, i &amp;lt;= new columns, i++,
	col = Num( sample[i] );
	colNamesList = dt &amp;lt;&amp;lt; Get Column Names;
	Wait( 0 );
	Eval(
		Substitute(
				Expr(
					dt &amp;lt;&amp;lt; New Column( sample[i] || " Days",
						formula( __column__ - :Name( "0" ) )
					)
				),
			Expr( __column__ ),
				Parse(":Name(\!"" || colNamesList[13 + i + new columns] || "\!")")
		)
	);
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 Sep 2018 19:11:56 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Referencing-a-Column-by-Column-Number-in-Formula-in-a-For-loop/m-p/74706#M35850</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2018-09-20T19:11:56Z</dc:date>
    </item>
    <item>
      <title>Re: Referencing a Column by Column Number in Formula in a For loop</title>
      <link>https://community.jmp.com/t5/Discussions/Referencing-a-Column-by-Column-Number-in-Formula-in-a-For-loop/m-p/74719#M35852</link>
      <description>&lt;P&gt;Thank you so much!!&lt;/P&gt;</description>
      <pubDate>Thu, 20 Sep 2018 20:24:02 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Referencing-a-Column-by-Column-Number-in-Formula-in-a-For-loop/m-p/74719#M35852</guid>
      <dc:creator>Jackieandrews</dc:creator>
      <dc:date>2018-09-20T20:24:02Z</dc:date>
    </item>
  </channel>
</rss>

