Hi all,
I have a datatable with multiple columns, some of which I want to add together. As a part of my script, I am selecting some of the columns, storing the column names in a list and then hoping to add the selected columns using the "Sum" feature.
In order to do that I need to build a string ":DATA1, :DATA2, :DATA3" and Parse it in a column.
New Column( "Addition", Numeric, Continuous, Formula( Parse( Sum( :DATA1, :DATA2, :DATA3 ) ) ) );
A snippet of the code I am using is below:
New Column( "Addition", Numeric, Continuous, Formula( Parse( Sum( :DATA1, :DATA2, :DATA3 ) ) ) );
list = {"DATA1", "DATA2", "DATA3"};
For( i = 1, i <= N Items( list ), i++,
If(
i == 1, string = ":" || Char( Eval( list[Eval( i )] ) ),
i > 1, string = string || "," || ":" || Char( Eval( list[Eval( i )] ) )
)
);
Parse( string );
On execution, I get the following error:
Unexpected ",". Perhaps there is a missing ";" or ",".
Line 1 Column 7: :DATA1►,:DATA2,:DATA3
The remaining text that was ignored was
,:DATA2,:DATA3
:DATA1
Is there anything I am missing?
Thanks in advance!
Thank you for helping and the explanation! The "Yet Another Data" column in your script does exactly what i want. The "Another Data" column seems to be returning an empty cell with an error "need arguments to be scalars, matrices, or lists..". I will try to figure out why this is happening.
Look at the string in which it returns.
":DATA1,:DATA2,:DATA3"
Try running just that without the quotes, you'll get the same thing.
If you have column names as strings, you can do something like this.
Names default to here(1);
dt = New Table( "Example",
Add Rows( 1 ),
New Column( "Data1", Set Values( [1] )),
New Column( "Data2", Set Values( [2] )),
New Column( "Data3", Set Values( [3] )),
);
// list of column names as strings
list = dt << Get Column Names("string"); // {"Data1", "Data2", "Data3"}
SumExpr = Expr(Sum());
for(i=1, i<=nitems(list), i++,
insert into(SumExpr, Column(dt, list[i]));
);
// see what the sumExpr looks like
show(nameexpr(sumExpr));
// then you have to do some substitute in order to actually get it to run
just_for_show = EvalExpr(
dt << New Column("Another Data", Formula(Expr(nameexpr(SumExpr))));
);
show(nameexpr(just_for_show));
just_for_show();// this will actually run the expression you just created.
// or you could use the list in Sum Directly.
// list of column names as references
list = dt << Get Column Names();
list=list[1::3];
// can do a list directly in Sum()
// should still substitute it in so it's no dependant on then list variable
just_for_show =Substitute(
Expr(
dt << new Column("Yet Another Data", Formula(Sum(DV_LIST)))
),
Expr(DV_LIST), list
);
show(nameexpr(just_for_show));
just_for_show(); // actually evaluates again.