<?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: By groups in column formula in for loop in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/By-groups-in-column-formula-in-for-loop/m-p/238657#M47161</link>
    <description>&lt;P&gt;JMP appears to be having an issue with using just the Column() function.&amp;nbsp; However, the As Column() function corrects the issue.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

New Column( "Old", Character, Formula( If( :age &amp;gt;= 14, "Old", "Young" ) ) );

colnames = dt &amp;lt;&amp;lt; get column names( numeric, string );
For( i = 1, i &amp;lt;= N Items( colnames ), i++,
	Eval(
		Substitute(
				Expr(
					New Column( Char( colnames[i] ) || " mean",
						Numeric,
						Formula( Col Mean( as Column( __colname__ ), :old ) )
					);
					New Column( Char( colnames[i] ) || " STD DEV",
						Numeric,
						Formula( Col Std Dev( as Column( __colname__ ) ) )
					);
					New Column( Char( colnames[i] ) || " CV%",
						Numeric,
						Format( "Percent", 2 ),
						Formula( Col Std Dev( as Column( __colname__ ) ) / Col Mean( as Column( __colname__ ) ) )
					);
				),
			Expr( __colname__ ), colnames[i]
		)
	)
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since you are using the Substitute() function, you could also remove the As Column() or Column() functions, and replace the __colname__ with the actual column reference of :theactualcolumnname. &amp;nbsp;&amp;nbsp; See below&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 = Open( "$SAMPLE_DATA/Big Class.jmp" );

New Column( "Old", Character, Formula( If( :age &amp;gt;= 14, "Old", "Young" ) ) );

colnames = dt &amp;lt;&amp;lt; get column names( numeric, string );
For( i = 1, i &amp;lt;= N Items( colnames ), i++,
	Eval(
		Substitute(
				Expr(
					New Column( Char( colnames[i] ) || " mean",
						Numeric,
						Formula( Col Mean(  __colname__ , :old ) )
					);
					New Column( Char( colnames[i] ) || " STD DEV",
						Numeric,
						Formula( Col Std Dev(  __colname__  ) )
					);
					New Column( Char( colnames[i] ) || " CV%",
						Numeric,
						Format( "Percent", 2 ),
						Formula( Col Std Dev( __colname__  ) / Col Mean(  __colname__  ) )
					);
				),
			Expr( __colname__ ), Parse( ":" || colnames[i] )
		)
	)
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 16 Dec 2019 15:40:49 GMT</pubDate>
    <dc:creator>txnelson</dc:creator>
    <dc:date>2019-12-16T15:40:49Z</dc:date>
    <item>
      <title>By groups in column formula in for loop</title>
      <link>https://community.jmp.com/t5/Discussions/By-groups-in-column-formula-in-for-loop/m-p/238631#M47157</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am looping through columns to make new columns of means, std dev and CV for each column. I have the below script, which works fine for global column statistics, but when I try to add by groups, like I have done in the mean column, the formula fails.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I realize that I could accomplish the same using a summary table updated back into the main table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&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 = Open( "$SAMPLE_DATA/Big Class.jmp");

New Column("Old",
			Character,
			Formula(If(:age&amp;gt;=14,"Old","Young"))
			);

colnames = dt &amp;lt;&amp;lt; get column names( numeric,string );
For( i = 1, i &amp;lt;= N Items( colnames ), i++,
	Eval(
		Substitute(
			Expr(
				New Column( Char( colnames[i] ) || " mean",
				Numeric,
				Formula(
					Col Mean(Column(__colname__),:old)
				) 
				);
				New Column( Char( colnames[i] ) || " STD DEV",
				Numeric,
				Formula(
					Col Std Dev(Column(__colname__))
				) 
				);
				New Column( Char( colnames[i] ) || " CV%",
				Numeric,
				Format( "Percent", 2 ),
				Formula(
					Col Std Dev(Column(__colname__))/
					Col Mean(Column(__colname__))
				) 
				);
			),
		Expr(__colname__),colnames[i]
		)
	)
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Dec 2019 13:55:49 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/By-groups-in-column-formula-in-for-loop/m-p/238631#M47157</guid>
      <dc:creator>olekh</dc:creator>
      <dc:date>2019-12-16T13:55:49Z</dc:date>
    </item>
    <item>
      <title>Re: By groups in column formula in for loop</title>
      <link>https://community.jmp.com/t5/Discussions/By-groups-in-column-formula-in-for-loop/m-p/238657#M47161</link>
      <description>&lt;P&gt;JMP appears to be having an issue with using just the Column() function.&amp;nbsp; However, the As Column() function corrects the issue.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

New Column( "Old", Character, Formula( If( :age &amp;gt;= 14, "Old", "Young" ) ) );

colnames = dt &amp;lt;&amp;lt; get column names( numeric, string );
For( i = 1, i &amp;lt;= N Items( colnames ), i++,
	Eval(
		Substitute(
				Expr(
					New Column( Char( colnames[i] ) || " mean",
						Numeric,
						Formula( Col Mean( as Column( __colname__ ), :old ) )
					);
					New Column( Char( colnames[i] ) || " STD DEV",
						Numeric,
						Formula( Col Std Dev( as Column( __colname__ ) ) )
					);
					New Column( Char( colnames[i] ) || " CV%",
						Numeric,
						Format( "Percent", 2 ),
						Formula( Col Std Dev( as Column( __colname__ ) ) / Col Mean( as Column( __colname__ ) ) )
					);
				),
			Expr( __colname__ ), colnames[i]
		)
	)
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since you are using the Substitute() function, you could also remove the As Column() or Column() functions, and replace the __colname__ with the actual column reference of :theactualcolumnname. &amp;nbsp;&amp;nbsp; See below&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 = Open( "$SAMPLE_DATA/Big Class.jmp" );

New Column( "Old", Character, Formula( If( :age &amp;gt;= 14, "Old", "Young" ) ) );

colnames = dt &amp;lt;&amp;lt; get column names( numeric, string );
For( i = 1, i &amp;lt;= N Items( colnames ), i++,
	Eval(
		Substitute(
				Expr(
					New Column( Char( colnames[i] ) || " mean",
						Numeric,
						Formula( Col Mean(  __colname__ , :old ) )
					);
					New Column( Char( colnames[i] ) || " STD DEV",
						Numeric,
						Formula( Col Std Dev(  __colname__  ) )
					);
					New Column( Char( colnames[i] ) || " CV%",
						Numeric,
						Format( "Percent", 2 ),
						Formula( Col Std Dev( __colname__  ) / Col Mean(  __colname__  ) )
					);
				),
			Expr( __colname__ ), Parse( ":" || colnames[i] )
		)
	)
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Dec 2019 15:40:49 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/By-groups-in-column-formula-in-for-loop/m-p/238657#M47161</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2019-12-16T15:40:49Z</dc:date>
    </item>
    <item>
      <title>Re: By groups in column formula in for loop</title>
      <link>https://community.jmp.com/t5/Discussions/By-groups-in-column-formula-in-for-loop/m-p/238859#M47199</link>
      <description>Thank you Jim, works perfectly!</description>
      <pubDate>Wed, 18 Dec 2019 09:23:33 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/By-groups-in-column-formula-in-for-loop/m-p/238859#M47199</guid>
      <dc:creator>olekh</dc:creator>
      <dc:date>2019-12-18T09:23:33Z</dc:date>
    </item>
  </channel>
</rss>

