I'm not sure what is the problem or what you are doing, but I would first build a list of columns which contain non zero values (how do you deal with missing values? do you care about non zero values OR sum of zero?). And then based on that list I would do some actions. I use Loc() here with datatable subscripting, but there are other methods for finding such columns
Names Default To Here(1);
dt = New Table("TEST",
Add Rows(9),
Compress File When Saved(1),
New Column("ATT1", Numeric, "Continuous", Format("Best", 12), Set Values([1, 0, 0, 0, 1, 0, 0, 1, 1])),
New Column("ATT2", Numeric, "Continuous", Format("Best", 12), Set Values([0, 0, 0, 0, 0, 0, 0, 0, 0])),
New Column("ATT3", Numeric, "Continuous", Format("Best", 12), Set Values([1, 1, 0, 0, 1, 1, 1, 1, 0])),
New Column("ATT4", Numeric, "Continuous", Format("Best", 12), Set Values([1, 1, 0, 0, -1, -1, 0, 0, 0])),
New Column("ATT5", Numeric, "Continuous", Format("Best", 12), Set Values([0, 0, 0, ., 0, 0, 0, 0, .])),
);
colnames = dt << Get Column Names("String");
// Option 1 - Loop using Loc and datatable subscripting
nonzerocols1 = {};
For Each({colname}, colnames,
nonzeros = Loc(dt[0, colname]);
If(N Items(nonzeros) > 0,
Insert Into(nonzerocols1, colname);
);
);
show(nonzerocols1);
// Option 2 - Matrix calculations
m_dt = dt[0,0]; // Could also use dt << Get As Matrix; instead of dt[0,0];
non_zero_col_idx = Loc(V Sum(m_dt != 0)); // single-liner for col indices
nonzerocols2 = colnames[non_zero_col_idx];
show(nonzerocols2);
// Option 3 - Using << Get Values
nonzerocols3 = {};
For Each({colname}, colnames,
vals = Column(dt, colname) << get values;
nonzeros = Sum(vals != 0);
If(nonzeros > 0,
Insert Into(nonzerocols3, colname);
);
);
show(nonzerocols3);
// Option 4 - Get Rows Where
nonzerocols4 = {};
For Each({colname}, colnames,
vals = dt << Get Rows Where(Column(colname) != 0); // This will also capture missing values
If(N Items(vals) > 0,
Insert Into(nonzerocols4, colname);
);
);
show(nonzerocols4);
// Perform some actions using nonzerocol list
stop();
nonzerocol = nonzerocols[1];
nonzerorows = Loc(dt[0, nonzerocol]);
dt1 = dt << Subset(Rows(nonzerorows), Selected Columns Only(0), Output table("NONZERO BY " || nonzerocol));
Edit: Added other options of getting non-zero value columns (most of these will "consider" missing value as zero value)
-Jarmo