<?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: Can we use &amp;quot;for loop&amp;quot; in Column Formula ? in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Can-we-use-quot-for-loop-quot-in-Column-Formula/m-p/443243#M69141</link>
    <description>&lt;P&gt;Here is one way of doing it&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="txnelson_0-1638952888785.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/38222i841C962FEE186DFA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="txnelson_0-1638952888785.png" alt="txnelson_0-1638952888785.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );

dt = New Table( "custIDs",
	New Column( "Customer_ID",
		character,
		values( {"c1", "c1", "c1", "c2", "c2", "c3", "c3", "c3"} )
	)
);

For( i = 1, i &amp;lt;= N Rows( dt ), i++,
	If( i == 1,
		dt &amp;lt;&amp;lt; New Column( Char( :Customer_ID[i] ), character );
		Column( dt, :Customer_ID[i] )[i] = :Customer_ID[i];
	,
		If( :Customer_ID[i] != :Customer_ID[i - 1],
			dt &amp;lt;&amp;lt; New Column( Char( :Customer_ID[i] ), character )
		)
	);
	Column( dt, :Customer_ID[i] )[i] = :Customer_ID[i];
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 08 Dec 2021 08:42:09 GMT</pubDate>
    <dc:creator>txnelson</dc:creator>
    <dc:date>2021-12-08T08:42:09Z</dc:date>
    <item>
      <title>Can we use "for loop" in Column Formula ?</title>
      <link>https://community.jmp.com/t5/Discussions/Can-we-use-quot-for-loop-quot-in-Column-Formula/m-p/443206#M69138</link>
      <description>&lt;P&gt;Hi All,&lt;BR /&gt;Is there any way to combine these two operations (Creating a new columns and then running a ForLoop) to a single one ?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;I have repeated Customer Ids in a columns and then it changes at some point and 2nd ID keeps on repeating and then 3rd one and so on. I want to create a new column say, Unq_Id and its values should be empty until Customer_Id changes. I can do this in script below, but I am wondering if there is any better way to do it ? This means - running a loop inside Column Formula, or something similar !&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt &amp;lt;&amp;lt; New Column( “Unq_ID”, Numeric, Continuous, Set Each Value() );
dt &amp;lt;&amp;lt; Sort( by( :Customer_ID ), Order( Ascending ), Replace Table( 1 ) );
For( i = 1, i &amp;lt;= N Rows( dt ), i++,
    If( 
        :Customer_ID[i] == :Customer_ID[i + 1]
    ,
        :Unq_ID[i] = :Customer_ID[i]
    )
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any suggestion?&lt;BR /&gt;thanks, Hari&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 18:06:55 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Can-we-use-quot-for-loop-quot-in-Column-Formula/m-p/443206#M69138</guid>
      <dc:creator>HSS</dc:creator>
      <dc:date>2023-06-09T18:06:55Z</dc:date>
    </item>
    <item>
      <title>Re: Can we use "for loop" in Column Formula ?</title>
      <link>https://community.jmp.com/t5/Discussions/Can-we-use-quot-for-loop-quot-in-Column-Formula/m-p/443243#M69141</link>
      <description>&lt;P&gt;Here is one way of doing it&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="txnelson_0-1638952888785.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/38222i841C962FEE186DFA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="txnelson_0-1638952888785.png" alt="txnelson_0-1638952888785.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );

dt = New Table( "custIDs",
	New Column( "Customer_ID",
		character,
		values( {"c1", "c1", "c1", "c2", "c2", "c3", "c3", "c3"} )
	)
);

For( i = 1, i &amp;lt;= N Rows( dt ), i++,
	If( i == 1,
		dt &amp;lt;&amp;lt; New Column( Char( :Customer_ID[i] ), character );
		Column( dt, :Customer_ID[i] )[i] = :Customer_ID[i];
	,
		If( :Customer_ID[i] != :Customer_ID[i - 1],
			dt &amp;lt;&amp;lt; New Column( Char( :Customer_ID[i] ), character )
		)
	);
	Column( dt, :Customer_ID[i] )[i] = :Customer_ID[i];
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Dec 2021 08:42:09 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Can-we-use-quot-for-loop-quot-in-Column-Formula/m-p/443243#M69141</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2021-12-08T08:42:09Z</dc:date>
    </item>
    <item>
      <title>Re: Can we use "for loop" in Column Formula ?</title>
      <link>https://community.jmp.com/t5/Discussions/Can-we-use-quot-for-loop-quot-in-Column-Formula/m-p/443250#M69142</link>
      <description>&lt;P&gt;Hi Nelson, I though about that but it is not what I am looking for.&lt;BR /&gt;&lt;BR /&gt;The script you provided is giving me something like below. And the one I am looking for is inside the red tringle.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Hari_1-1638955031232.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/38224i517371683172C267/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Hari_1-1638955031232.png" alt="Hari_1-1638955031232.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, rather than putting c1, c2 .... how to put 1, 2, 3 ... sort of counter, which increases by 1 as soon as there is a change.&lt;BR /&gt;&lt;BR /&gt;Any help?&lt;/P&gt;</description>
      <pubDate>Wed, 08 Dec 2021 09:17:22 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Can-we-use-quot-for-loop-quot-in-Column-Formula/m-p/443250#M69142</guid>
      <dc:creator>HSS</dc:creator>
      <dc:date>2021-12-08T09:17:22Z</dc:date>
    </item>
    <item>
      <title>Re: Can we use "for loop" in Column Formula ?</title>
      <link>https://community.jmp.com/t5/Discussions/Can-we-use-quot-for-loop-quot-in-Column-Formula/m-p/443293#M69145</link>
      <description>&lt;P&gt;Here are a couple of ways to create the column you want&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="txnelson_0-1638965213803.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/38226i1E02029FA70FBE0C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="txnelson_0-1638965213803.png" alt="txnelson_0-1638965213803.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );

dt = New Table( "custIDs",
	New Column( "Customer_ID", character, values( {"c1", "c1", "c1", "c2", "c2", "c3", "c3", "c3"} ) )
);

dt &amp;lt;&amp;lt; New Column( "Or like this" );

counter = 0;
For( i = 1, i &amp;lt;= N Rows( dt ), i++,
	If(
		i == N Rows( dt ),
			counter++;
			:or like this[i] = counter;,
		:Customer_ID[i] != :Customer_ID[i + 1],
			counter++;
			:or like this[i] = counter;
	)
);

// or

dt &amp;lt;&amp;lt; New Column( "or like this using formula",
	formula(
		x = .;
		If( Row() == 1, counter = 0 );
		If( :Customer_ID != Lag( :Customer_ID, -1 ) | Row() == N Rows( Current Data Table() ),
			counter++;
			x = counter;
		);
		x;
	)
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Dec 2021 12:07:20 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Can-we-use-quot-for-loop-quot-in-Column-Formula/m-p/443293#M69145</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2021-12-08T12:07:20Z</dc:date>
    </item>
    <item>
      <title>Re: Can we use "for loop" in Column Formula ?</title>
      <link>https://community.jmp.com/t5/Discussions/Can-we-use-quot-for-loop-quot-in-Column-Formula/m-p/443313#M69147</link>
      <description>&lt;P&gt;Yes, I was looking for the 2nd type of solution.&amp;nbsp;&lt;BR /&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Dec 2021 13:31:38 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Can-we-use-quot-for-loop-quot-in-Column-Formula/m-p/443313#M69147</guid>
      <dc:creator>HSS</dc:creator>
      <dc:date>2021-12-08T13:31:38Z</dc:date>
    </item>
  </channel>
</rss>

