<?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 Summing all columns using loop in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Summing-all-columns-using-loop/m-p/591127#M79523</link>
    <description>&lt;P&gt;Hi all, building a fairly lengthy script where I won't know what columns are present in a given table as it's creating multiple tables from different data sets. A summary table is created with upwards of 50 columns, sometimes more than 70. I need to sum all the values in these columns, but can't specify the sum by :Name("Column Name")&amp;nbsp;in case some of the categories are present/missing. This is the loop I am trying to use:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Current Data Table() &amp;lt;&amp;lt; New Column( "Total Die",
	formula(
		For( j = 1, j &amp;lt;= N, j++,
			cols = Column( dt, j ) &amp;lt;&amp;lt; getname()
		);
		Sum( cols );
	)
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The sum won't accept the format of the list , so just wondering how else to do this? Thanks!&lt;/P&gt;</description>
    <pubDate>Thu, 08 Jun 2023 16:42:30 GMT</pubDate>
    <dc:creator>OC200m</dc:creator>
    <dc:date>2023-06-08T16:42:30Z</dc:date>
    <item>
      <title>Summing all columns using loop</title>
      <link>https://community.jmp.com/t5/Discussions/Summing-all-columns-using-loop/m-p/591127#M79523</link>
      <description>&lt;P&gt;Hi all, building a fairly lengthy script where I won't know what columns are present in a given table as it's creating multiple tables from different data sets. A summary table is created with upwards of 50 columns, sometimes more than 70. I need to sum all the values in these columns, but can't specify the sum by :Name("Column Name")&amp;nbsp;in case some of the categories are present/missing. This is the loop I am trying to use:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Current Data Table() &amp;lt;&amp;lt; New Column( "Total Die",
	formula(
		For( j = 1, j &amp;lt;= N, j++,
			cols = Column( dt, j ) &amp;lt;&amp;lt; getname()
		);
		Sum( cols );
	)
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The sum won't accept the format of the list , so just wondering how else to do this? Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jun 2023 16:42:30 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Summing-all-columns-using-loop/m-p/591127#M79523</guid>
      <dc:creator>OC200m</dc:creator>
      <dc:date>2023-06-08T16:42:30Z</dc:date>
    </item>
    <item>
      <title>Re: Summing all columns using loop</title>
      <link>https://community.jmp.com/t5/Discussions/Summing-all-columns-using-loop/m-p/591130#M79525</link>
      <description>&lt;P&gt;I should add that the script intitialises dt and N like so:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = Current Data Table();
N = N Col( dt );&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Jan 2023 16:33:17 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Summing-all-columns-using-loop/m-p/591130#M79525</guid>
      <dc:creator>OC200m</dc:creator>
      <dc:date>2023-01-18T16:33:17Z</dc:date>
    </item>
    <item>
      <title>Re: Summing all columns using loop</title>
      <link>https://community.jmp.com/t5/Discussions/Summing-all-columns-using-loop/m-p/591187#M79527</link>
      <description>&lt;P&gt;If I understand correctly, then rather than building a loop you could exploit 'Tables &amp;gt; Summary' and let JMP do the work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;NamesDefaultToHere(1);

// Get some data
dt = Open("$SAMPLE_DATA/Semiconductor Capability.jmp");

// Get all the numeric columns
cols = dt &amp;lt;&amp;lt; getColumnNames("Numeric");

// Get all the column means
dt2 = dt &amp;lt;&amp;lt; Summary(Mean(cols));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Whether or not this is better might depend on what else your JSL is doing, but generally it's good to use JMP platforms, especially if numerics are involved, even simple ones.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Jan 2023 17:15:31 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Summing-all-columns-using-loop/m-p/591187#M79527</guid>
      <dc:creator>ian_jmp</dc:creator>
      <dc:date>2023-01-18T17:15:31Z</dc:date>
    </item>
    <item>
      <title>Re: Summing all columns using loop</title>
      <link>https://community.jmp.com/t5/Discussions/Summing-all-columns-using-loop/m-p/591189#M79529</link>
      <description>&lt;P&gt;Ooops! Sorry - It looks like you actually want to sum across columns row-wise. See, &lt;A href="https://community.jmp.com/t5/Discussions/How-do-I-sum-multiple-columns-using-the-formula-window/td-p/12471" target="_self"&gt;for example&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively, if you do want a formula:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;NamesDefaultToHere(1);

// How many columns in the table?
nc = RandomInteger(2, 20);

// Build the table with some random data
dt = NewTable("Sum Columns", &amp;lt;&amp;lt; addMultipleColumns("Col ", nc, Numeric));
for(c=1, c&amp;lt;=nc, c++, Column(dt, c) &amp;lt;&amp;lt; setFormula(RandomNormal()));
dt &amp;lt;&amp;lt; addRows(10);
dt &amp;lt;&amp;lt; runFormulas;

// Start here with the current data table
dt2 = CurrentDataTable();
cols2 = dt2 &amp;lt;&amp;lt; getColumnNames("Numeric");

// Add the row-wise sum as a new formula column
afc = Expr(dt2 &amp;lt;&amp;lt; newColumn("Sum over Columns", Numeric, Continuous, Formula(Sum(colsTBD))));
SubstituteInto(afc, Expr(colsTBD), Eval(cols2));
afc;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(This posting was originally incomplete).&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2023 09:50:20 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Summing-all-columns-using-loop/m-p/591189#M79529</guid>
      <dc:creator>ian_jmp</dc:creator>
      <dc:date>2023-01-19T09:50:20Z</dc:date>
    </item>
    <item>
      <title>Re: Summing all columns using loop</title>
      <link>https://community.jmp.com/t5/Discussions/Summing-all-columns-using-loop/m-p/591208#M79530</link>
      <description>&lt;P&gt;Stretching your knowledge a bit, I think the easiest way to handle the issue is to not create a formula column, but to rather create the column, and then use JSL's functionality using matrices to perform the magic.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Current Data Table();
N = N Col( dt );

dt &amp;lt;&amp;lt; New Column( "new" );

For Each Row(
	dt:new = Sum( dt[Row(), index(1,ncols(dt)-1)] )
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Jan 2023 17:28:00 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Summing-all-columns-using-loop/m-p/591208#M79530</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2023-01-18T17:28:00Z</dc:date>
    </item>
    <item>
      <title>Re: Summing all columns using loop</title>
      <link>https://community.jmp.com/t5/Discussions/Summing-all-columns-using-loop/m-p/591240#M79533</link>
      <description>&lt;P&gt;Completely vectorize the computation.&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 = Current Data Table();
n = N Col( dt );
vals = (dt &amp;lt;&amp;lt; Get As Matrix) * J( n, 1, 1);
dt &amp;lt;&amp;lt; New Column( "new", "Numeric", "Continuous",
	Values( vals )
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Jan 2023 18:48:28 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Summing-all-columns-using-loop/m-p/591240#M79533</guid>
      <dc:creator>Mark_Bailey</dc:creator>
      <dc:date>2023-01-18T18:48:28Z</dc:date>
    </item>
  </channel>
</rss>

