<?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 Create Column with Values Based Off Existence of Other Rows &amp;amp; Logic in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Create-Column-with-Values-Based-Off-Existence-of-Other-Rows-amp/m-p/629980#M82815</link>
    <description>&lt;P&gt;Hello anyone,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to create a formula or script that will set a certain value ("Good", "Bad", or "N/A") for each row based off if there is another row in the table with values matching logic that uses the current row as an input.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a script that does just this by using nested For loops when using a small subset of the full datatable. However, with the full datatable I am trying to use which is roughly 1 million rows, the script just crashes JMP.&lt;/P&gt;&lt;P&gt;Is there a more efficient way in order to allow JMP to perform this? (Or if it can be done with SQL, I can do that before bringing it into JMP).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;See attached for the script that does what I am talking about.&lt;/P&gt;</description>
    <pubDate>Sun, 11 Jun 2023 00:01:04 GMT</pubDate>
    <dc:creator>ATAbbott</dc:creator>
    <dc:date>2023-06-11T00:01:04Z</dc:date>
    <item>
      <title>Create Column with Values Based Off Existence of Other Rows &amp; Logic</title>
      <link>https://community.jmp.com/t5/Discussions/Create-Column-with-Values-Based-Off-Existence-of-Other-Rows-amp/m-p/629980#M82815</link>
      <description>&lt;P&gt;Hello anyone,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to create a formula or script that will set a certain value ("Good", "Bad", or "N/A") for each row based off if there is another row in the table with values matching logic that uses the current row as an input.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a script that does just this by using nested For loops when using a small subset of the full datatable. However, with the full datatable I am trying to use which is roughly 1 million rows, the script just crashes JMP.&lt;/P&gt;&lt;P&gt;Is there a more efficient way in order to allow JMP to perform this? (Or if it can be done with SQL, I can do that before bringing it into JMP).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;See attached for the script that does what I am talking about.&lt;/P&gt;</description>
      <pubDate>Sun, 11 Jun 2023 00:01:04 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Create-Column-with-Values-Based-Off-Existence-of-Other-Rows-amp/m-p/629980#M82815</guid>
      <dc:creator>ATAbbott</dc:creator>
      <dc:date>2023-06-11T00:01:04Z</dc:date>
    </item>
    <item>
      <title>Re: Create Column with Values Based Off Existence of Other Rows &amp; Logic</title>
      <link>https://community.jmp.com/t5/Discussions/Create-Column-with-Values-Based-Off-Existence-of-Other-Rows-amp/m-p/630024#M82819</link>
      <description>&lt;P&gt;Usually there is no need to nest for loops when working with data tables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you provide small subset of your data and include a column with the results you want in it?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 May 2023 06:01:13 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Create-Column-with-Values-Based-Off-Existence-of-Other-Rows-amp/m-p/630024#M82819</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2023-05-09T06:01:13Z</dc:date>
    </item>
    <item>
      <title>Re: Create Column with Values Based Off Existence of Other Rows &amp; Logic</title>
      <link>https://community.jmp.com/t5/Discussions/Create-Column-with-Values-Based-Off-Existence-of-Other-Rows-amp/m-p/630277#M82843</link>
      <description>&lt;P&gt;Okay, I will mock up the data though because of privacy reasons.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;colA&lt;/TD&gt;&lt;TD&gt;colB&lt;/TD&gt;&lt;TD&gt;colC&lt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gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If the logic of the original script wasn't clear:&lt;/P&gt;&lt;P&gt;If current row&amp;nbsp;'s colE is "Action 1": then assign "N/A";&amp;nbsp;&lt;BR /&gt;Else, if current row's colE is "Action 2", then then follow the logic in the next paragraph.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If there exists another row in the table with the same values of the current row for columns colA and colB; current row's colC value is exactly 82 more than this other row; colDate values are within 18 seconds of each other; current row's colE is "Action 2" and this other row's colE is "Action 1": then assign "Good"; else&amp;nbsp;then assign "Bad".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Appreciate any help! I'm just not seeing a way without nested loops at the moment.&lt;/P&gt;&lt;P&gt;But I do now at least realize I should put a Break() after Line 25 of my original script. No reason to keep looking for another row that matches my criteria if I already found at least one exists.&lt;/P&gt;</description>
      <pubDate>Tue, 09 May 2023 17:26:27 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Create-Column-with-Values-Based-Off-Existence-of-Other-Rows-amp/m-p/630277#M82843</guid>
      <dc:creator>ATAbbott</dc:creator>
      <dc:date>2023-05-09T17:26:27Z</dc:date>
    </item>
    <item>
      <title>Re: Create Column with Values Based Off Existence of Other Rows &amp; Logic</title>
      <link>https://community.jmp.com/t5/Discussions/Create-Column-with-Values-Based-Off-Existence-of-Other-Rows-amp/m-p/630434#M82860</link>
      <description>&lt;P&gt;Logic in this script is overly complicated as I don't know all the limitations of the data (can there for example be multiple matches) and I didn't optimize it&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

dt = New Table("Untitled",
	Add Rows(9),
	Compress File When Saved(1),
	New Column("colA", Character, "Nominal", Set Values({"A0001", "A0001", "A0001", "A0002", "A0002", "A0002", "A0003", "A0004", "A0004"})),
	New Column("colB", Numeric, "Continuous", Format("Best", 9), Set Values([16, 100, 100, 212, 211, 212, 441, 441, 441])),
	New Column("colC", Numeric, "Continuous", Format("Best", 9), Set Values([100, 182, 100, 200, 282, 200, 382, 382, 300])),
	New Column("colDate",
		Character(22),
		"Nominal",
		Set Values(
			{" 2022/04/01 9:15:01 AM", " 2022/04/01 9:15:31 AM", " 2022/04/01 9:15:36 AM", " 2022/04/02 3:15:21 AM", " 2022/04/02 3:15:11 AM",
			" 2022/04/02 3:15:16 AM", " 2022/05/01 1:15:31 AM", " 2022/05/01 5:16:31 AM", " 2022/05/01 5:15:31 AM"}
		)
	),
	New Column("colE",
		Character(16),
		"Nominal",
		Set Values({"Action1", "Action2", "Action1", "Action1", "Action2", "Action1", "Action2", "Action2", "Action1"})
	),
	New Column(" Logic", Character(16), "Nominal", Set Values({" N/A", " Good", " N/A", " N/A", " Bad", " N/A", " Bad", " Bad", " N/A"}))
);

dt &amp;lt;&amp;lt; New Column("Group", Character, Nominal, &amp;lt;&amp;lt; Set Each Value(char(:colA)||char(:colB)));
//fix date
Column(dt, "colDate") &amp;lt;&amp;lt; Set Each Value(Substitute(:colDate, " AM", "")) &amp;lt;&amp;lt; Data Type(Numeric);

dt &amp;lt;&amp;lt; New Column("Logic", Character, Nominal, &amp;lt;&amp;lt; Set Each Value(
	As Constant(
		m_group = :Group &amp;lt;&amp;lt; get values;
		m_c = :colC &amp;lt;&amp;lt; get values;
		m_date = :colDate &amp;lt;&amp;lt; get values;
	);
	
	If(:colE == "Action1", 
		"N/A";
	, :colE == "Action2",
		cur_group_idx = Loc(m_group, :Group);
		If(N Items(cur_group_idx) == 0,
			"Bad"
		,
			plus_idx = cur_group_idx[Loc(:colC - 82 == m_c[cur_group_idx])];
			If(N Items(plus_idx) == 0,
				"Bad"
			,
				timediff_idx = Loc(Abs(m_date[plus_idx] - :colDate) &amp;lt;= 18);
				If(N Items(timediff_idx) &amp;gt; 0,
					If(:colE == "Action2" &amp;amp; :colE[timediff_idx[1]] == "Action1",
						"Good"
					,
						"Bad"
					)
				,
					"Bad"
				);
			)
		);
	, 
		"Bad";
	);
));

dt &amp;lt;&amp;lt; Delete Column("Group");&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jthi_0-1683697089311.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/52684iC842950676BE8D55/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jthi_0-1683697089311.png" alt="jthi_0-1683697089311.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 May 2023 05:38:23 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Create-Column-with-Values-Based-Off-Existence-of-Other-Rows-amp/m-p/630434#M82860</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2023-05-10T05:38:23Z</dc:date>
    </item>
  </channel>
</rss>

