Tengo una tabla con bastantes columnas (> 100). Estoy tratando de crear una columna que cuente la cantidad de columnas en una fila que son mayores que 1. ¿Cómo escribiría una fórmula para hacer eso? A continuación se muestra una pequeña imagen de una tabla de ejemplo.
Sería preferible si esto fuera solo una fórmula que pudiera colocar en una tabla y no un script JSL independiente.
He aquí una forma:
NamesDefaultToHere(1);
// Build a table
nr = 20; // Number of rows
nc = 10; // Number of columns
dt = AsTable(J(nr, nc, RandomNormal(1, 1)));
dt << setName("Above and Below One");
// Find out what columns are in the table
cols = dt << getColumnNames("String");
// Build an expression to be used as the formula:
// Sum the results of an 'if' clause for each column that exists
form = Expr(Add());
for(c=1, c<=NItems(cols), c++,
ifClause = Expr(If(columnTBD > 1, 1, 0));
SubstituteInto(ifClause, Expr(columnTBD), Parse(":"||cols[c]));
InsertInto(form, NameExpr(ifClause));
);
// Inspect the resulting formula
Print(form);
// Add the formula column
fc = dt << NewColumn("Count Above One", Numeric, Ordinal, Formula(NameExpr(form)));