<?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: How can I loop through column names in a for statement? in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/How-can-I-loop-through-column-names-in-a-for-statement/m-p/83505#M48797</link>
    <description>&lt;P&gt;Sometimes putting a multi-statement, formula is painful and it is better to type it in.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When you double click in the formula it opens a script window.&amp;nbsp; This might take a little practice. Maybe copy the formula that uses the column numbers and paste it in the blue box, then double click to bring up the script editor.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope that helps.&lt;/P&gt;</description>
    <pubDate>Thu, 15 Nov 2018 18:47:11 GMT</pubDate>
    <dc:creator>gzmorgan0</dc:creator>
    <dc:date>2018-11-15T18:47:11Z</dc:date>
    <item>
      <title>How can I loop through column names in a for statement?</title>
      <link>https://community.jmp.com/t5/Discussions/How-can-I-loop-through-column-names-in-a-for-statement/m-p/83310#M48793</link>
      <description>&lt;P&gt;Hi, I'm trying to create a new formula column that will loop through preceding columns,&amp;nbsp; The preceding columns are in two groups, each group has column names which start with one of two strings.&amp;nbsp; Then these column headers end in integers, 0, 1, 2,...up through 22.&amp;nbsp; Please see attached pdf screenshot of my code and let me know why it won't work.&amp;nbsp; Having not used JMP before yesterday, I'm guessing that I may be using "i" or "%i" incorrectly in the column names within the IF statement.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2018 18:09:37 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-can-I-loop-through-column-names-in-a-for-statement/m-p/83310#M48793</guid>
      <dc:creator>Liz_S</dc:creator>
      <dc:date>2018-11-14T18:09:37Z</dc:date>
    </item>
    <item>
      <title>Re: How can I loop through column names in a for statement?</title>
      <link>https://community.jmp.com/t5/Discussions/How-can-I-loop-through-column-names-in-a-for-statement/m-p/83425#M48794</link>
      <description>&lt;P&gt;Your PDF is difficult to read.&amp;nbsp; Will only one of the 22 colums have "Matriculated"? Or will several and you want all organization codes?&lt;/P&gt;</description>
      <pubDate>Thu, 15 Nov 2018 10:11:00 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-can-I-loop-through-column-names-in-a-for-statement/m-p/83425#M48794</guid>
      <dc:creator>gzmorgan0</dc:creator>
      <dc:date>2018-11-15T10:11:00Z</dc:date>
    </item>
    <item>
      <title>Re: How can I loop through column names in a for statement?</title>
      <link>https://community.jmp.com/t5/Discussions/How-can-I-loop-through-column-names-in-a-for-statement/m-p/83467#M48795</link>
      <description>&lt;P&gt;There are multiple methods to accomplish what I think you need. First of all you need to understand JMP syntax for referencing columns.&amp;nbsp; For example, if you wanted to create a new column that randomly choose organization 1-5 here would be teh column formula&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;i = Random Integer(1,5);
Column( "designation_organization_" || Char( i ) )[Row()];&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;|| is the concatenation operator, Char(i) is the string. Then brackets represent the row from the sepcified column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have attached a table. I do not have any idea what your table looks like, but maybe this script will help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Open the attached table.&amp;nbsp; Then run this script&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt=data table("matriculation");
cnmes = dt &amp;lt;&amp;lt; get column names("string");

dcols = {};  //create a list of decision columns
for(i=1, i&amp;lt;=nitems(cnmes), i++,
   if( contains(cnmes[i], "_decision_"), Insert into(dcols, cnmes[i]) )
);

//create a new column
matcol = dt &amp;lt;&amp;lt; New Column("Major", Character);

//rowwise
for(i=1, i &amp;lt;= nrow(dt) , i++, 
	k = Contains(dt[i,dcols], "Matriculated"); &lt;BR /&gt;        //dt[ i, dcols] is a list of values in the decision columns from row i&lt;BR /&gt;        //contains returns the position in the list for "Matriculated"
	matcol[i] = if(k&amp;gt;0, Column("designation_organization_" || char(k))[i], "None" );&lt;BR /&gt;        //if k &amp;gt; 0, "Matriculated" was found, so assign to matcol the organization for the corresponding k 
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Note, JMP allows using column numbers. Here is a script, for a new column for the attached table, that uses column numbers&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 833px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/14358iCC27DC0FB369254A/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Nov 2018 11:14:52 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-can-I-loop-through-column-names-in-a-for-statement/m-p/83467#M48795</guid>
      <dc:creator>gzmorgan0</dc:creator>
      <dc:date>2018-11-15T11:14:52Z</dc:date>
    </item>
    <item>
      <title>Re: How can I loop through column names in a for statement?</title>
      <link>https://community.jmp.com/t5/Discussions/How-can-I-loop-through-column-names-in-a-for-statement/m-p/83501#M48796</link>
      <description>&lt;P&gt;Hi gzmorgan0,&lt;/P&gt;&lt;P&gt;The first script you wrote for the table you attached does what I would like and is brilliant!&amp;nbsp; &amp;nbsp;I'll be able to finish my project at work today.&amp;nbsp; Thanks!&amp;nbsp; However, since this is day 3 for me in JMP, I would like to understand how to write the last option you wrote using the formula editor in JMP.&amp;nbsp; When I tried to create the new column in your table after running the first script I encountered two problems:&lt;/P&gt;&lt;P&gt;1.) I could not use two independent functions in the column formula editor in two separate statements. I started with the &lt;STRONG&gt;Contains&lt;/STRONG&gt; function and added a ";" after it.&amp;nbsp; But then when I clicked on the &lt;STRONG&gt;If&lt;/STRONG&gt; function, it embedded the Contains statement automatically.&amp;nbsp; Weird.&amp;nbsp; I don't want &lt;STRONG&gt;Contains&lt;/STRONG&gt; embedded within&lt;STRONG&gt; If&lt;/STRONG&gt; or vice versa.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2.) I didn't see how to add the row() and Index(6,10) to the &lt;STRONG&gt;Contains&lt;/STRONG&gt;( Current Data Table (),...&amp;nbsp; &amp;nbsp;The ^ option gave me a new comma separated "part": &lt;STRONG&gt;Contains( &lt;/STRONG&gt;Current Data Table()&lt;STRONG&gt;, &lt;/STRONG&gt;part&lt;STRONG&gt;, &lt;/STRONG&gt;"Matriculated").&amp;nbsp; How did you insert the rows / column options to your &lt;STRONG&gt;Contains&lt;/STRONG&gt; function in the formula editor?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have gone through the welcome kit tutorials but did not see enough info on scripting and using the formula editor in a new column.&amp;nbsp; Do you happen to know of a Youtube video you could recommend for beginners?&amp;nbsp; But I would also appreciate another reply from you if you have time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Nov 2018 17:08:18 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-can-I-loop-through-column-names-in-a-for-statement/m-p/83501#M48796</guid>
      <dc:creator>Liz_S</dc:creator>
      <dc:date>2018-11-15T17:08:18Z</dc:date>
    </item>
    <item>
      <title>Re: How can I loop through column names in a for statement?</title>
      <link>https://community.jmp.com/t5/Discussions/How-can-I-loop-through-column-names-in-a-for-statement/m-p/83505#M48797</link>
      <description>&lt;P&gt;Sometimes putting a multi-statement, formula is painful and it is better to type it in.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When you double click in the formula it opens a script window.&amp;nbsp; This might take a little practice. Maybe copy the formula that uses the column numbers and paste it in the blue box, then double click to bring up the script editor.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope that helps.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Nov 2018 18:47:11 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-can-I-loop-through-column-names-in-a-for-statement/m-p/83505#M48797</guid>
      <dc:creator>gzmorgan0</dc:creator>
      <dc:date>2018-11-15T18:47:11Z</dc:date>
    </item>
    <item>
      <title>Re: How can I loop through column names in a for statement?</title>
      <link>https://community.jmp.com/t5/Discussions/How-can-I-loop-through-column-names-in-a-for-statement/m-p/83507#M48798</link>
      <description>First thing I tried was to copy and paste the blue box formula into a new column script window. It doesn't paste; it is as if the information in the blue box in your solution is a picture or screen shot.&lt;BR /&gt;Yes, I do know how to double click in a formula box to be able to type in a script. I still can't see how to add row and column indices to a Contain statement. But thanks again for the earlier parts to your response.</description>
      <pubDate>Thu, 15 Nov 2018 18:58:11 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-can-I-loop-through-column-names-in-a-for-statement/m-p/83507#M48798</guid>
      <dc:creator>Liz_S</dc:creator>
      <dc:date>2018-11-15T18:58:11Z</dc:date>
    </item>
    <item>
      <title>Re: How can I loop through column names in a for statement?</title>
      <link>https://community.jmp.com/t5/Discussions/How-can-I-loop-through-column-names-in-a-for-statement/m-p/83508#M48799</link>
      <description>&lt;P&gt;Here is the formula that works for the Matriculation data table, this is just JSL text.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;k = Contains( Current Data Table()[Row(), Index( 6, 10 )], "Matriculated" );
If( k &amp;gt; 0,
	Column( "designation_organization_" || Char( k ) )[Row()],
	"None"
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 15 Nov 2018 19:01:51 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-can-I-loop-through-column-names-in-a-for-statement/m-p/83508#M48799</guid>
      <dc:creator>gzmorgan0</dc:creator>
      <dc:date>2018-11-15T19:01:51Z</dc:date>
    </item>
    <item>
      <title>Re: How can I loop through column names in a for statement?</title>
      <link>https://community.jmp.com/t5/Discussions/How-can-I-loop-through-column-names-in-a-for-statement/m-p/83517#M48800</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;This script worked perfectly and thanks for helping me around my unfamiliarity with the JMP formula editor.&amp;nbsp; This elegant code was more what I had in mind when I starting trying to write my loop.&amp;nbsp; Just one last question, could you please explain your selection of the numbers 6 and 10 within the Index statement?&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm guessing that the Index numbers 6 and 10 were because there were 5 of each of the two column types in your test file and 10 columns altogether?&amp;nbsp; So 6 starts the set of decision columns and 10 ends the decision columns?&amp;nbsp; (Decided to avoid guessing and just ask you.)&lt;/P&gt;&lt;P&gt;Feeling grateful!&lt;/P&gt;&lt;P&gt;Liz_S&lt;/P&gt;</description>
      <pubDate>Thu, 15 Nov 2018 20:03:57 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-can-I-loop-through-column-names-in-a-for-statement/m-p/83517#M48800</guid>
      <dc:creator>Liz_S</dc:creator>
      <dc:date>2018-11-15T20:03:57Z</dc:date>
    </item>
    <item>
      <title>Re: How can I loop through column names in a for statement?</title>
      <link>https://community.jmp.com/t5/Discussions/How-can-I-loop-through-column-names-in-a-for-statement/m-p/83535#M48801</link>
      <description>&lt;P&gt;Liz_s, yes these are referring to column numbers.&lt;/P&gt;&lt;P&gt;From the JMP Main Menu, select&amp;nbsp; Help &amp;gt; Books &amp;gt; Scripting Guide and go page 439 (or near by). This is a section in chapter 9 &lt;STRONG&gt;Data Tables&lt;/STRONG&gt;, section 1,&amp;nbsp; &lt;STRONG&gt;Working with Data Table Objects&lt;/STRONG&gt;, topic &lt;STRONG&gt;Accessing Data Values&lt;/STRONG&gt;&amp;nbsp;. Read that entire section for alternate methods to access and assign data values.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Comment: The key to your task was to find which decision column contained "Matriculated" for each row. Looping 22+ times for each row is a lot of nesting. The key here to learn more is to use the Help &amp;gt; Scripting Index &amp;gt; Functions to learn about Contains.&lt;/P&gt;&lt;P&gt;Contains( list|string, matching string) returns the first position where it is found. Note: Loc() returns all positions in a list or matrix where the matching value is found. Understanding that dt[row, c1::cn] where columns c1 through cn are character will return a list of the table values.&amp;nbsp; Contains() will find the position (k), so this will be the correct position for the organization column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The key is to "know your tools" how to access and assign and know your functions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Good luck with your journey learning JMP.&lt;/P&gt;</description>
      <pubDate>Fri, 16 Nov 2018 00:01:14 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-can-I-loop-through-column-names-in-a-for-statement/m-p/83535#M48801</guid>
      <dc:creator>gzmorgan0</dc:creator>
      <dc:date>2018-11-16T00:01:14Z</dc:date>
    </item>
  </channel>
</rss>

