In my DB the number of columns is rather large. Hence, I am trying to eliminate any columns which contain only 0 entries.
The number of columns, nor the column keys, are know upfront. So, I step through the DB and generate a list of columns that are to be omitted. When I do that the omit list provided seems to be ignored. Below I have omitted the code that determines which columns are
There are no problems for the case of:
Manually specified omit list: So
\DTLdisplaydb[A,C]{myDB}works fine.Omit list specified by a macro that has fixed content: So
\def\ListOfColumnsToOmit{A,C} \DTLdisplaydb[\ListOfColumnsToOmit]{myDB}also works fine.
Only for the case where I compute the list of columns to omit and add them to \ListOfColumnsToOmit via the \AddToCommaSeparateList macro do things break.
So, the correct result would be that the last table was identical to the two middle tables.

References:
- Expansion issue when adding to CSV list from within a \foreach
- How keep a running list of strings and then process them one at a time
Code:
\documentclass{article}
\usepackage{xstring}
\usepackage{datatool}
% Make csv for question
\usepackage{filecontents}
\begin{filecontents*}{MyData.csv}
A, B, C, D, E
0, 1, 0, 2, 3
0, 0, 0, 4, 4
0, 2, 0, 0, 5
\end{filecontents*}
\makeatletter
% https://tex.stackexchange.com/questions/83595/expansion-issue-when-adding-to-csv-list-from-within-a-foreach/
\newcommand*{\AddToCommaSeparateList}[3][,\space]{%
% #1 = separator
% #2 = listname
% #3 = content to add to CSV list
\IfStrEq{#2}{}{}{\g@addto@macro#2{#1}}% Don't add a leading comma in list
%
% Extra brace group in case #3 contains a comma
\begingroup\edef\x{\endgroup%
\noexpand\g@addto@macro\noexpand#2{{#3}}}\x%
}%
\makeatother
\begin{document}
\DTLloaddb{myDB}{MyData.csv}
With \emph{no} omit list provided:\par
\noindent\DTLdisplaydb{myDB}%
With manually specified omit list:\par
\noindent\DTLdisplaydb[A,C]{myDB}%
\def\ListOfColumnsToOmit{A,C}
With fixed macro omit list:\par
\noindent\DTLdisplaydb[\ListOfColumnsToOmit]{myDB}%
\def\ListOfColumnsToOmit{}
\AddToCommaSeparateList{\ListOfColumnsToOmit}{A}%
\AddToCommaSeparateList{\ListOfColumnsToOmit}{C}%
With generated macro omit list:\par
\noindent\DTLdisplaydb[\ListOfColumnsToOmit]{myDB}%
The omit list was: \ListOfColumnsToOmit
\end{document}