<?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 Re: Is Creating Multiple Stacked Tables From One Original Table in a Nested For Loop Possible? in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Is-Creating-Multiple-Stacked-Tables-From-One-Original-Table-in-a/m-p/753981#M93606</link>
    <description>&lt;P&gt;I may have confused you with making 2 changes to your code, but only talking about one.&amp;nbsp; &amp;nbsp;When you made your most recent test, did you use the line&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;tempTable = data table("Original Data") &amp;lt;&amp;lt; Stack(&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you did and then got the error you specified, I will ask you to supply a sample data table, and I will explore the issue more.&lt;/P&gt;</description>
    <pubDate>Mon, 13 May 2024 17:36:07 GMT</pubDate>
    <dc:creator>txnelson</dc:creator>
    <dc:date>2024-05-13T17:36:07Z</dc:date>
    <item>
      <title>Is Creating Multiple Stacked Tables From One Original Table in a Nested For Loop Possible?</title>
      <link>https://community.jmp.com/t5/Discussions/Is-Creating-Multiple-Stacked-Tables-From-One-Original-Table-in-a/m-p/753933#M93599</link>
      <description>&lt;P&gt;I would like to automate creating multiple stacked tables from one original data table. I plan on doing this by creating a list of keywords, and if any column in my original table contains that keyword then it would be stacked in a new data table. Essentially I have a list of 3 keywords, so I would be creating 3 stacked data tables. My script has a nested for loop to iterate through all the columns in the original data table and see if they contain the keyword being indexed. See below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;groupNames = {"abc", "def", "ghi"};


for (i = 1, i &amp;lt;= nitems(groupNames), i++,
for (j = 1, j &amp;lt;= ncols(Data Table("Original Data")), j++,
if ((contains(Column Name(j), groupNames[i])),
Insert Into(tempStack, Column Name(j));
);
);
tempTable = Stack(
Columns(tempStack),
Source Label Column("Label"),
Stacked Data Column("Data"),
"Non-stacked columns"n (Keep(:tst_temp, :stdf_file)),
);
tempStack = {};
tempTable &amp;lt;&amp;lt; set name(char(groupNames[i]) || " Stacked");
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem that arises when executing this code is that the first group gets stacked just fine, but when the for loop indexes to i=2 to look for the next group it fails to look at the original data table and instead looks at the table that was just stacked (Data table "abc Stacked" in this case).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I make it so that my for loop looks at my original data table instead of the newly created table. Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best,&lt;/P&gt;&lt;P&gt;kaoticllama&lt;/P&gt;</description>
      <pubDate>Mon, 13 May 2024 15:48:03 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Is-Creating-Multiple-Stacked-Tables-From-One-Original-Table-in-a/m-p/753933#M93599</guid>
      <dc:creator>kaoticllama</dc:creator>
      <dc:date>2024-05-13T15:48:03Z</dc:date>
    </item>
    <item>
      <title>Re: Is Creating Multiple Stacked Tables From One Original Table in a Nested For Loop Possible?</title>
      <link>https://community.jmp.com/t5/Discussions/Is-Creating-Multiple-Stacked-Tables-From-One-Original-Table-in-a/m-p/753945#M93601</link>
      <description>&lt;P&gt;You just need to specify to use the Original Data to the Stack input&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;groupNames = {"abc", "def", "ghi"};


For( i = 1, i &amp;lt;= N Items( groupNames ), i++,
	For( j = 1, j &amp;lt;= N Cols( Data Table( "Original Data" ) ), j++,
		If( (Contains( Column Name( j ), groupNames[i] )),
			Insert Into( tempStack, Column Name( j ) )
		)
	);
	tempTable = data table("Original Data") &amp;lt;&amp;lt; Stack(
		Columns( tempStack ),
		Source Label Column( "Label" ),
		Stacked Data Column( "Data" ),
		"Non-stacked columns"n( Keep( :tst_temp, :stdf_file ) ),
		Output Table( Char( groupNames[i] ) || " Stacked" )
	);
	tempStack = {};
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 May 2024 15:59:13 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Is-Creating-Multiple-Stacked-Tables-From-One-Original-Table-in-a/m-p/753945#M93601</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2024-05-13T15:59:13Z</dc:date>
    </item>
    <item>
      <title>Re: Is Creating Multiple Stacked Tables From One Original Table in a Nested For Loop Possible?</title>
      <link>https://community.jmp.com/t5/Discussions/Is-Creating-Multiple-Stacked-Tables-From-One-Original-Table-in-a/m-p/753980#M93605</link>
      <description>&lt;P&gt;Hey Jim,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Unfortunately I tried this already and it doesn't seem to work. It still gets confused and thinks I am referring to the newly stacked table when executing the i=2 iteration. I can tell because my newly stacked table has 4 columns. The error I receive is a j indexing error that fails when j=5.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="kaoticllama_1-1715620732271.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/64169i44561C502114A4D0/image-size/medium?v=v2&amp;amp;px=400" role="button" title="kaoticllama_1-1715620732271.png" alt="kaoticllama_1-1715620732271.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;For reference, I am using JMP Pro 17.2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 May 2024 17:30:29 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Is-Creating-Multiple-Stacked-Tables-From-One-Original-Table-in-a/m-p/753980#M93605</guid>
      <dc:creator>kaoticllama</dc:creator>
      <dc:date>2024-05-13T17:30:29Z</dc:date>
    </item>
    <item>
      <title>Re: Is Creating Multiple Stacked Tables From One Original Table in a Nested For Loop Possible?</title>
      <link>https://community.jmp.com/t5/Discussions/Is-Creating-Multiple-Stacked-Tables-From-One-Original-Table-in-a/m-p/753981#M93606</link>
      <description>&lt;P&gt;I may have confused you with making 2 changes to your code, but only talking about one.&amp;nbsp; &amp;nbsp;When you made your most recent test, did you use the line&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;tempTable = data table("Original Data") &amp;lt;&amp;lt; Stack(&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you did and then got the error you specified, I will ask you to supply a sample data table, and I will explore the issue more.&lt;/P&gt;</description>
      <pubDate>Mon, 13 May 2024 17:36:07 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Is-Creating-Multiple-Stacked-Tables-From-One-Original-Table-in-a/m-p/753981#M93606</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2024-05-13T17:36:07Z</dc:date>
    </item>
    <item>
      <title>Re: Is Creating Multiple Stacked Tables From One Original Table in a Nested For Loop Possible?</title>
      <link>https://community.jmp.com/t5/Discussions/Is-Creating-Multiple-Stacked-Tables-From-One-Original-Table-in-a/m-p/753992#M93607</link>
      <description>&lt;P&gt;Hey Jim,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;No worries, here is the updated code and sample data. It still does not work with the change you suggested.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;tempStack = {};

GroupNames = {"abc", "def"};

for (i = 1, i &amp;lt;= nitems(GroupNames), i++,
	for (j = 1, j &amp;lt;= ncols(Data Table("Sample")), j++,
		if ((contains(Column Name(j), GroupNames[i])),
			Insert Into(tempStack, Column Name(j));
		);
	);
	colNum = ncols(Data Table("Sample"));
	tempTable = Data Table("Sample") &amp;lt;&amp;lt; Stack(
	Columns(tempStack),
	Source Label Column("Label"),
	Stacked Data Column("Data"),
	"Non-stacked columns"n (Keep(:tst_temp, :stdf_file)),
	Invisible(1)
	);
	tempStack = {};
	tempTable &amp;lt;&amp;lt; set name(char(GroupNames[i]) || " Stacked");
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Let me know what you find. Thanks&lt;/P&gt;</description>
      <pubDate>Mon, 13 May 2024 17:52:08 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Is-Creating-Multiple-Stacked-Tables-From-One-Original-Table-in-a/m-p/753992#M93607</guid>
      <dc:creator>kaoticllama</dc:creator>
      <dc:date>2024-05-13T17:52:08Z</dc:date>
    </item>
    <item>
      <title>Re: Is Creating Multiple Stacked Tables From One Original Table in a Nested For Loop Possible?</title>
      <link>https://community.jmp.com/t5/Discussions/Is-Creating-Multiple-Stacked-Tables-From-One-Original-Table-in-a/m-p/754021#M93609</link>
      <description>&lt;P&gt;It was the Column Name() function that was messing the script up.&amp;nbsp; It points to the current active data table.&amp;nbsp; Thus, I added the setting of the Current Data Table back to the original Sample data table.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;names default to here(1);
tempStack = {};

GroupNames = {"abc", "def"};

for (i = 1, i &amp;lt;= nitems(GroupNames), i++,
	Current data table(data table("Sample"));
	for (j = 1, j &amp;lt;= ncols(Data Table("Sample")), j++,
		if ((contains(Column Name(j), GroupNames[i])),
			Insert Into(tempStack, Column Name(j));
		);
	);
	colNum = ncols(Data Table("Sample"));
	tempTable = Data Table("Sample") &amp;lt;&amp;lt; Stack(
	Columns(tempStack),
	Source Label Column("Label"),
	Stacked Data Column("Data"),
	"Non-stacked columns"n (Keep(:tst_temp, :stdf_file)),
	Invisible(1)
	);
	tempStack = {};
	tempTable &amp;lt;&amp;lt; set name(char(GroupNames[i]) || " Stacked");
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Using a variable as a pointer to a data table is sometimes very convenient.&amp;nbsp; In the script below, the variable dt is set to the Current Data table and then used in the script to make sure the code is using the correct table.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
tempStack = {};

GroupNames = {"abc", "def"};
dt = Current Data Table();

For( i = 1, i &amp;lt;= N Items( GroupNames ), i++,
	For( j = 1, j &amp;lt;= N Cols( dt ), j++,
		If( (Contains( Column( dt, j ) &amp;lt;&amp;lt; get name, GroupNames[i] )),
			Insert Into( tempStack, Column( dt, j ) &amp;lt;&amp;lt; get name )
		)
	);
	colNum = N Cols( dt );
	tempTable = dt &amp;lt;&amp;lt; Stack(
		Columns( tempStack ),
		Source Label Column( "Label" ),
		Stacked Data Column( "Data" ),
		"Non-stacked columns"n( Keep( :tst_temp, :stdf_file ) ),
		Invisible( 1 )
	);
	tempStack = {};
	tempTable &amp;lt;&amp;lt; set name( Char( GroupNames[i] ) || " Stacked" );
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 May 2024 19:01:57 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Is-Creating-Multiple-Stacked-Tables-From-One-Original-Table-in-a/m-p/754021#M93609</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2024-05-13T19:01:57Z</dc:date>
    </item>
    <item>
      <title>Re: Is Creating Multiple Stacked Tables From One Original Table in a Nested For Loop Possible?</title>
      <link>https://community.jmp.com/t5/Discussions/Is-Creating-Multiple-Stacked-Tables-From-One-Original-Table-in-a/m-p/754294#M93644</link>
      <description>&lt;P&gt;Jim,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot for your help on this one! Very well explained and works perfectly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best,&lt;/P&gt;&lt;P&gt;kaoticllama&lt;/P&gt;</description>
      <pubDate>Tue, 14 May 2024 15:32:57 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Is-Creating-Multiple-Stacked-Tables-From-One-Original-Table-in-a/m-p/754294#M93644</guid>
      <dc:creator>kaoticllama</dc:creator>
      <dc:date>2024-05-14T15:32:57Z</dc:date>
    </item>
    <item>
      <title>Re: Is Creating Multiple Stacked Tables From One Original Table in a Nested For Loop Possible?</title>
      <link>https://community.jmp.com/t5/Discussions/Is-Creating-Multiple-Stacked-Tables-From-One-Original-Table-in-a/m-p/754325#M93651</link>
      <description>&lt;P&gt;Here is other suggestion which modified your script quite heavily. It uses a bit different approach using For Each() (easier to read (in my opinion) than For()) and associative array to collect the column groups.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

// Initialize Associative Array with groups and empty lists
groups = ["abc" =&amp;gt; {}, "def" =&amp;gt; {}];

dt = Open("$DOWNLOADS/Sample.jmp");
colnames = dt &amp;lt;&amp;lt; Get Column Names("String");

// Collect columns to each key in associative array
For Each({colname}, colnames,
	For Each({{group, val}}, groups,
		If(Contains(colname, group),
			Insert Into(groups[group], colname);
		);
	);
);

// Loop over the associative array and create the stacked tables
stacked_tables = {};
For Each({{groupname, groupcols}}, groups,
	stacked_dt = dt &amp;lt;&amp;lt; Stack(
		Columns(Eval(groupcols)),
		Source Label Column("Label"),
		Stacked Data Column("Data"),
		"Non-stacked columns"n(Keep(:tst_temp, :stdf_file)),
		Output Table(groupname || " Stacked"),
		Invisible(1)
	);
	Insert Into(stacked_tables, stacked_dt);
);

show(stacked_tables);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-jsl"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 May 2024 16:28:02 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Is-Creating-Multiple-Stacked-Tables-From-One-Original-Table-in-a/m-p/754325#M93651</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2024-05-14T16:28:02Z</dc:date>
    </item>
  </channel>
</rss>

