<?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: Using For Loop to Create New Column in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Using-For-Loop-to-Create-New-Column/m-p/211287#M42312</link>
    <description>&lt;P&gt;The main issue you have is that the Formula element within the New Column() function does not evaluate before processing, therefore, it will place into the formula your specification PN[i], and since the variable "i" is changing when your loop changes the formula for each of the columns you are creating will change.&amp;nbsp; So what you have to do, is to change the formula to be static values.&amp;nbsp; That is, you do not want the installed formula to have PN[i] but rather the actual evaluated value of whatever PN[i] is.&amp;nbsp; The Substitution() function allows for this to happen.&amp;nbsp; I think the below script will do what you want&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Current Data Table();
Summarize( PN = by( :Name( "~DEVICE TYPE" ) ) );
PN={"X"}
For( i = 1, i &amp;lt;= N Items( PN ), i++,
	Eval(
		Substitute(
				Expr(
					New Column( __name__,
						formula(
							If( :Name( "~DEVICE TYPE" ) == __PN__,
								If(
									:SERIAL_NO || :TIME_DATE ==
									Lag( :SERIAL_NO || :TIME_DATE, -1 ),
									0,
									1
								)
							)
						)
					)
				),
			Expr( __name__ ), "Cnt " || PN[i],
			Expr( __PN__ ), PN[i]
		)
	)
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 30 May 2019 19:38:58 GMT</pubDate>
    <dc:creator>txnelson</dc:creator>
    <dc:date>2019-05-30T19:38:58Z</dc:date>
    <item>
      <title>Using For Loop to Create New Column</title>
      <link>https://community.jmp.com/t5/Discussions/Using-For-Loop-to-Create-New-Column/m-p/211261#M42306</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I try to create the new column by using for loop&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;names default to here (1);
dt = current data table ();
summarize (PN=by (:Name("~DEVICE TYPE" )));

for (i=1, i &amp;lt;= N items(PN), i++,
new column("Cnt "||PN[i],
formula(If( :Name( "~DEVICE TYPE" ) == PN[i],
If( :SERIAL_NO || :TIME_DATE == Lag( :SERIAL_NO || :TIME_DATE, -1 ),0,1)
))));&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 can create the new column but cannot get the formula work. The log file show below error message.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Column Cnt TRB100BC-08 Formula Interrupted
Subscript Range 1 times At rows: 2 Operation: PN[i], PN[/*###*/i]
Formula evaluation errors have been ignored
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 May 2019 17:52:39 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Using-For-Loop-to-Create-New-Column/m-p/211261#M42306</guid>
      <dc:creator>yanee</dc:creator>
      <dc:date>2019-05-30T17:52:39Z</dc:date>
    </item>
    <item>
      <title>Re: Using For Loop to Create New Column</title>
      <link>https://community.jmp.com/t5/Discussions/Using-For-Loop-to-Create-New-Column/m-p/211287#M42312</link>
      <description>&lt;P&gt;The main issue you have is that the Formula element within the New Column() function does not evaluate before processing, therefore, it will place into the formula your specification PN[i], and since the variable "i" is changing when your loop changes the formula for each of the columns you are creating will change.&amp;nbsp; So what you have to do, is to change the formula to be static values.&amp;nbsp; That is, you do not want the installed formula to have PN[i] but rather the actual evaluated value of whatever PN[i] is.&amp;nbsp; The Substitution() function allows for this to happen.&amp;nbsp; I think the below script will do what you want&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Current Data Table();
Summarize( PN = by( :Name( "~DEVICE TYPE" ) ) );
PN={"X"}
For( i = 1, i &amp;lt;= N Items( PN ), i++,
	Eval(
		Substitute(
				Expr(
					New Column( __name__,
						formula(
							If( :Name( "~DEVICE TYPE" ) == __PN__,
								If(
									:SERIAL_NO || :TIME_DATE ==
									Lag( :SERIAL_NO || :TIME_DATE, -1 ),
									0,
									1
								)
							)
						)
					)
				),
			Expr( __name__ ), "Cnt " || PN[i],
			Expr( __PN__ ), PN[i]
		)
	)
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 May 2019 19:38:58 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Using-For-Loop-to-Create-New-Column/m-p/211287#M42312</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2019-05-30T19:38:58Z</dc:date>
    </item>
    <item>
      <title>Re: Using For Loop to Create New Column</title>
      <link>https://community.jmp.com/t5/Discussions/Using-For-Loop-to-Create-New-Column/m-p/211380#M42339</link>
      <description>thank you</description>
      <pubDate>Fri, 31 May 2019 17:02:21 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Using-For-Loop-to-Create-New-Column/m-p/211380#M42339</guid>
      <dc:creator>yanee</dc:creator>
      <dc:date>2019-05-31T17:02:21Z</dc:date>
    </item>
  </channel>
</rss>

