<?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: Add multiple columns by scripts in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Add-multiple-columns-by-scripts/m-p/217841#M43551</link>
    <description>&lt;P&gt;You could do something like this.&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");
dt &amp;lt;&amp;lt; New Column("Cpk", Set Each Value(random normal(1, 1)));
dt &amp;lt;&amp;lt; New Column("1.0&amp;lt;=CpK &amp;lt;1.33", Set Each Value(0));

col_names = dt &amp;lt;&amp;lt; Get column names(string); // this is your existing columns

//I would just do a not contains which checks where an item is in a list, returning 0 if it doesn't
if(!Contains(col_names, "CpK&amp;lt;1.0"), 
	dt &amp;lt;&amp;lt; New Column( "CpK&amp;lt;1.0", numeric, 
		formula( 
			// you also don't have to do the if(thing, 1, 0) :Cpk &amp;lt; 1 will resolve to 1 and 0 anyway
			:Cpk &amp;lt; 1
		)
	)
);
// this one shouldn't get remade
if(!Contains(col_names, "1.0&amp;lt;=CpK &amp;lt;1.33"), 
	dt &amp;lt;&amp;lt; New Column( "1.0&amp;lt;=CpK &amp;lt;1.33", numeric, formula( 1.0&amp;lt;=:Cpk &amp;lt;1.33 ))
);

if(!Contains(col_names, "1.33&amp;lt;=CpK&amp;lt;1.67"), 
	dt &amp;lt;&amp;lt; New Column( "1.33&amp;lt;=CpK&amp;lt;1.67", numeric, formula( 1.33&amp;lt;=:Cpk &amp;lt;1.67 ))
);

if(!Contains(col_names, "1.67&amp;lt;=CpK&amp;lt;2.0"), 
	dt &amp;lt;&amp;lt; New Column( "1.67&amp;lt;=CpK&amp;lt;2.0", numeric, formula( 1.67&amp;lt;=:Cpk &amp;lt;2 ))
);

if(!Contains(col_names, "CpK&amp;gt;=2.0"), 
	dt &amp;lt;&amp;lt; New Column( "CpK&amp;gt;=2.0", numeric, formula( 2.0&amp;lt;=:Cpk ))
);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could also abstract it a little so you can change only 1 data structure and it would change your fields.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;//But I would probably do it another way
Names default to here(1);
dt = open("$SAMPLE_DATA\Big Class.jmp");
dt &amp;lt;&amp;lt; New Column("Cpk", Set Each Value(random normal(1, 1)));
dt &amp;lt;&amp;lt; New Column("1&amp;lt;=Cpk&amp;lt;1.33", Set Each Value(0));
col_names = dt &amp;lt;&amp;lt; Get column names(string); // this is your existing columns

//I'd make a 2 col matrix with low and high check then make the columns dynamically
cpk_check = [. 1, 1 1.33, 1.33 1.67, 1.67 2, 2 .];
for(i=1, i&amp;lt;=nrows(cpk_check), i++, 
	low = cpk_check[i, 1];
	high = cpk_check[i, 2];

	if(!ismissing(low), 
		if(!ismissing(high), 
			col_name = char(low) || "&amp;lt;=Cpk&amp;lt;" ||char(high);
			f = EvalExpr(Expr(low)&amp;lt;=:Cpk&amp;lt;Expr(high));
		, //else high is missing
			col_name = char(low) || "&amp;lt;=Cpk";
			f = EvalExpr(Expr(low)&amp;lt;=:Cpk);
		)
	, //else
		if(!ismissing(high), 
			col_name = "Cpk&amp;lt;" ||char(high);
			f = EvalExpr(:Cpk&amp;lt;Expr(high));
		, //else high is missing
			//assuming you don't want this
			throw("high and low values can't both be missing")
		);
	);
	
	show(col_name, nameexpr(f));
	if(!Contains(col_names, col_name), 
		Eval(EvalExpr(dt &amp;lt;&amp;lt; New Column( col_name, numeric, formula( Expr(nameexpr(f)) )) ));
	);

)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 17 Jul 2019 15:25:56 GMT</pubDate>
    <dc:creator>vince_faller</dc:creator>
    <dc:date>2019-07-17T15:25:56Z</dc:date>
    <item>
      <title>Add multiple columns by scripts</title>
      <link>https://community.jmp.com/t5/Discussions/Add-multiple-columns-by-scripts/m-p/217828#M43548</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wonder to add multiple columns on the defined data table. If column has already existed, then the script could ignore the column and continue to add the rest ones if not existed until the implemented.&lt;/P&gt;&lt;P&gt;I worte a script here. But the issue is even the column existed, the script still add duplicated column in the data table. Could you help to address this issue? Appreciate it!&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt0 = Current Data Table();


tnames = dt0 &amp;lt;&amp;lt; Get Column Names;
n = N Items( tnames );


For( i = 1, i &amp;lt;= n, i++,
	If( (tnames[i] ==  Char("CpK&amp;lt;1.0") ),
		judA = 0,
		judA=1
	);
	If( (tnames[i] == Char("1.0&amp;lt;=CpK &amp;lt;1.33") ),
		judB = 0,
		judA=1
	);
	If( (tnames[i] == Char("1.33&amp;lt;=CpK&amp;lt;1.67") ),
		judC = 0,
		judC=1
	);
	If( (tnames[i] ==  Char("1.67&amp;lt;=CpK&amp;lt;2.0") ),
		judD = 0,
		judD=1
	);
	If( (tnames[i] == Char("CpK&amp;gt;=2.0") ),
		judE = 0,
		judE=1
	);
);

If( judA == 1,
	dt0 &amp;lt;&amp;lt; New Column( "CpK&amp;lt;1.0", numeric, formula( If( :Cpk &amp;lt; 1, 1, 0 ) ), EvalFormula ),
);
If( judB == 1,
	dt0 &amp;lt;&amp;lt; New Column( "1.0&amp;lt;=CpK &amp;lt;1.33", numeric, formula( If( 1 &amp;lt;= :Cpk &amp;lt; 1.33, 1, 0 ) ), EvalFormula ),
);
If( judC == 1,
	dt0 &amp;lt;&amp;lt; New Column( "1.33&amp;lt;=CpK&amp;lt;1.67", numeric, formula( If( 1.33 &amp;lt;= :Cpk &amp;lt; 1.67, 1, 0 ) ), EvalFormula ),
);
If( judD == 1,
	dt0 &amp;lt;&amp;lt; New Column( "1.67&amp;lt;=CpK&amp;lt;2.0", numeric, formula( If( 1.67 &amp;lt;= :Cpk &amp;lt; 2, 1, 0 ) ), EvalFormula ),
);
If( judE == 1,
	dt0 &amp;lt;&amp;lt; New Column( "CpK&amp;gt;=2.0", numeric, formula( If( :Cpk &amp;gt;= 2, 1, 0 ) ), EvalFormula ),
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 Jul 2019 14:06:56 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Add-multiple-columns-by-scripts/m-p/217828#M43548</guid>
      <dc:creator>Adam_Xu</dc:creator>
      <dc:date>2019-07-17T14:06:56Z</dc:date>
    </item>
    <item>
      <title>Re: Add multiple columns by scripts</title>
      <link>https://community.jmp.com/t5/Discussions/Add-multiple-columns-by-scripts/m-p/217838#M43549</link>
      <description>Hi,&lt;BR /&gt;The problem is in the loop where your 5 switches judA - judE are reevaluated for each column. So when you have gone through the n existing columns, the state of all your switches are set to the last column.&lt;BR /&gt;</description>
      <pubDate>Wed, 17 Jul 2019 14:22:37 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Add-multiple-columns-by-scripts/m-p/217838#M43549</guid>
      <dc:creator>Thierry_S</dc:creator>
      <dc:date>2019-07-17T14:22:37Z</dc:date>
    </item>
    <item>
      <title>Re: Add multiple columns by scripts</title>
      <link>https://community.jmp.com/t5/Discussions/Add-multiple-columns-by-scripts/m-p/217839#M43550</link>
      <description>Thanks Thierry! Then how to address this issue?</description>
      <pubDate>Wed, 17 Jul 2019 14:48:04 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Add-multiple-columns-by-scripts/m-p/217839#M43550</guid>
      <dc:creator>Adam_Xu</dc:creator>
      <dc:date>2019-07-17T14:48:04Z</dc:date>
    </item>
    <item>
      <title>Re: Add multiple columns by scripts</title>
      <link>https://community.jmp.com/t5/Discussions/Add-multiple-columns-by-scripts/m-p/217841#M43551</link>
      <description>&lt;P&gt;You could do something like this.&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");
dt &amp;lt;&amp;lt; New Column("Cpk", Set Each Value(random normal(1, 1)));
dt &amp;lt;&amp;lt; New Column("1.0&amp;lt;=CpK &amp;lt;1.33", Set Each Value(0));

col_names = dt &amp;lt;&amp;lt; Get column names(string); // this is your existing columns

//I would just do a not contains which checks where an item is in a list, returning 0 if it doesn't
if(!Contains(col_names, "CpK&amp;lt;1.0"), 
	dt &amp;lt;&amp;lt; New Column( "CpK&amp;lt;1.0", numeric, 
		formula( 
			// you also don't have to do the if(thing, 1, 0) :Cpk &amp;lt; 1 will resolve to 1 and 0 anyway
			:Cpk &amp;lt; 1
		)
	)
);
// this one shouldn't get remade
if(!Contains(col_names, "1.0&amp;lt;=CpK &amp;lt;1.33"), 
	dt &amp;lt;&amp;lt; New Column( "1.0&amp;lt;=CpK &amp;lt;1.33", numeric, formula( 1.0&amp;lt;=:Cpk &amp;lt;1.33 ))
);

if(!Contains(col_names, "1.33&amp;lt;=CpK&amp;lt;1.67"), 
	dt &amp;lt;&amp;lt; New Column( "1.33&amp;lt;=CpK&amp;lt;1.67", numeric, formula( 1.33&amp;lt;=:Cpk &amp;lt;1.67 ))
);

if(!Contains(col_names, "1.67&amp;lt;=CpK&amp;lt;2.0"), 
	dt &amp;lt;&amp;lt; New Column( "1.67&amp;lt;=CpK&amp;lt;2.0", numeric, formula( 1.67&amp;lt;=:Cpk &amp;lt;2 ))
);

if(!Contains(col_names, "CpK&amp;gt;=2.0"), 
	dt &amp;lt;&amp;lt; New Column( "CpK&amp;gt;=2.0", numeric, formula( 2.0&amp;lt;=:Cpk ))
);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could also abstract it a little so you can change only 1 data structure and it would change your fields.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;//But I would probably do it another way
Names default to here(1);
dt = open("$SAMPLE_DATA\Big Class.jmp");
dt &amp;lt;&amp;lt; New Column("Cpk", Set Each Value(random normal(1, 1)));
dt &amp;lt;&amp;lt; New Column("1&amp;lt;=Cpk&amp;lt;1.33", Set Each Value(0));
col_names = dt &amp;lt;&amp;lt; Get column names(string); // this is your existing columns

//I'd make a 2 col matrix with low and high check then make the columns dynamically
cpk_check = [. 1, 1 1.33, 1.33 1.67, 1.67 2, 2 .];
for(i=1, i&amp;lt;=nrows(cpk_check), i++, 
	low = cpk_check[i, 1];
	high = cpk_check[i, 2];

	if(!ismissing(low), 
		if(!ismissing(high), 
			col_name = char(low) || "&amp;lt;=Cpk&amp;lt;" ||char(high);
			f = EvalExpr(Expr(low)&amp;lt;=:Cpk&amp;lt;Expr(high));
		, //else high is missing
			col_name = char(low) || "&amp;lt;=Cpk";
			f = EvalExpr(Expr(low)&amp;lt;=:Cpk);
		)
	, //else
		if(!ismissing(high), 
			col_name = "Cpk&amp;lt;" ||char(high);
			f = EvalExpr(:Cpk&amp;lt;Expr(high));
		, //else high is missing
			//assuming you don't want this
			throw("high and low values can't both be missing")
		);
	);
	
	show(col_name, nameexpr(f));
	if(!Contains(col_names, col_name), 
		Eval(EvalExpr(dt &amp;lt;&amp;lt; New Column( col_name, numeric, formula( Expr(nameexpr(f)) )) ));
	);

)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jul 2019 15:25:56 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Add-multiple-columns-by-scripts/m-p/217841#M43551</guid>
      <dc:creator>vince_faller</dc:creator>
      <dc:date>2019-07-17T15:25:56Z</dc:date>
    </item>
    <item>
      <title>Re: Add multiple columns by scripts</title>
      <link>https://community.jmp.com/t5/Discussions/Add-multiple-columns-by-scripts/m-p/217865#M43562</link>
      <description>&lt;P&gt;Thank you so much, Vince!&amp;nbsp;It works.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jul 2019 01:36:36 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Add-multiple-columns-by-scripts/m-p/217865#M43562</guid>
      <dc:creator>Adam_Xu</dc:creator>
      <dc:date>2019-07-18T01:36:36Z</dc:date>
    </item>
  </channel>
</rss>

