<?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: Get column values using Loop in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Get-column-values-using-Loop/m-p/276153#M53590</link>
    <description>&lt;P&gt;Here are 3 different methods to solve the problem&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;This one loops through the rows and columns, using the column names to come up with the answer
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );

// Create an Example data table
dt = New Table( "Example 1",
	Add Rows( 3 ),
	New Column( "Col 1", Character, "Nominal", Set Values( {"Jane", "NA", "NA"} ) ),
	New Column( "Col 2", Character, "Nominal", Set Values( {"NA", "Henry", "NA"} ) ),
	New Column( "Col 3",
		Character,
		"Nominal",
		Set Values( {"Don", "Terry", "NA"} )
	),
	New Column( "Col 4",
		Character,
		"Nominal",
		Set Values( {"Tom", "NA", "Jack"} ),
		Set Display Width( 48 )
	),
	New Column( "Col 5", Character, "Nominal", Set Values( {"Amy", "NA", "Bob"} ) )
);

// Get the names of the columns in the data table
colNames = dt &amp;lt;&amp;lt; get column names( string );

// Create the new column
dt &amp;lt;&amp;lt; New Column( "All Members", character );

// Loop through each row
For( k = 1, k &amp;lt;= N Rows( dt ), k++,
	// Loop through all columns finding non NA values
	For( i = 1, i &amp;lt;= N Items( colNames ), i++,
		If( Column( colNames[i] )[k] != "NA",
			If( :All Members[k] == "",
				:All Members[k] = Column( colNames[i] )[k],
				:All Members[k] = :All Members[k] || ", " || Column( colNames[i] )[k]
			)
		)
	)
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI&gt;
&lt;LI&gt;This one loops through the rows and columns using the column numbers to solve the problem
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );

// Create an Example data table
dt = New Table( "Example 2",
	Add Rows( 3 ),
	New Column( "Col 1", Character, "Nominal", Set Values( {"Jane", "NA", "NA"} ) ),
	New Column( "Col 2", Character, "Nominal", Set Values( {"NA", "Henry", "NA"} ) ),
	New Column( "Col 3",
		Character,
		"Nominal",
		Set Values( {"Don", "Terry", "NA"} )
	),
	New Column( "Col 4",
		Character,
		"Nominal",
		Set Values( {"Tom", "NA", "Jack"} ),
		Set Display Width( 48 )
	),
	New Column( "Col 5", Character, "Nominal", Set Values( {"Amy", "NA", "Bob"} ) )
);

// Get the number of current columns
numCol = N Cols( dt );

// Create the new column
dt &amp;lt;&amp;lt; New Column( "All Members", character );

// Loop through each row
For( k = 1, k &amp;lt;= N Rows( dt ), k++,
	// Loop through all columns finding non NA values
	For( i = 1, i &amp;lt;= numCol, i++,
		If( Column( i )[k] != "NA",
			If( :All Members[k] == "",
				:All Members[k] = Column( i )[k],
				:All Members[k] = :All Members[k] || ", " || Column( i )[k]
			)
		)
	)
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI&gt;
&lt;LI&gt;This one changes the NA values in the data table, to blanks, which is the normal way to indicate missing values for character columns, and then uses a built in function called "Combine Columns()" to create the desired column
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );

// Create an Example data table
dt = New Table( "Example 3",
	Add Rows( 3 ),
	New Column( "Col 1", Character, "Nominal", Set Values( {"Jane", "NA", "NA"} ) ),
	New Column( "Col 2", Character, "Nominal", Set Values( {"NA", "Henry", "NA"} ) ),
	New Column( "Col 3",
		Character,
		"Nominal",
		Set Values( {"Don", "Terry", "NA"} )
	),
	New Column( "Col 4",
		Character,
		"Nominal",
		Set Values( {"Tom", "NA", "Jack"} ),
		Set Display Width( 48 )
	),
	New Column( "Col 5", Character, "Nominal", Set Values( {"Amy", "NA", "Bob"} ) )
);

// Get the names of the columns in the data table
colNames = dt &amp;lt;&amp;lt; get column names( string );

// Loop through the columns and delete the NA values
For( i = 1, i &amp;lt;= N Cols( dt ), i++,
	dt &amp;lt;&amp;lt; select where( as column( colNames[i] ) == "NA" );
	// set the found cells to a blank
	try( column( colNames[i] )[ dt &amp;lt;&amp;lt; get selected rows ] = "" )
);

// Create the new column using a built in function
dt &amp;lt;&amp;lt; Combine Columns(
	delimiter( "," ),
	Columns( eval(colNames) ),
	Selected Columns are Indicator Columns(0 ),
	Column Name( "All Members" )
);

// Move the column to the last position
dt &amp;lt;&amp;lt; Move Selected Columns( :All Members, To last );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 30 Jun 2020 09:22:37 GMT</pubDate>
    <dc:creator>txnelson</dc:creator>
    <dc:date>2020-06-30T09:22:37Z</dc:date>
    <item>
      <title>Get column values using Loop</title>
      <link>https://community.jmp.com/t5/Discussions/Get-column-values-using-Loop/m-p/276127#M53585</link>
      <description>&lt;P&gt;Hi, I am a beginner, currently learning jsl scripting. I would like to know how shall i write a script (loop) to check N columns of values (names) and only output those that with a valid name, and ignore those "NA"? The end result (Column "All members") in each row shall be a combined list of valid names (ignoring column value with "NA"). See example below:-&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;Col 1&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;Col 2&amp;nbsp;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;Col 3&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;Col 4&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;…………….&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;Col n&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;All members&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT size="2"&gt;Jane&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;NA&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;Don&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;Tom&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;Amy&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;Jane, Don, Tom, Amy&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT size="2"&gt;NA&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;Henry&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;Terry&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;NA&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;NA&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;Henry, Terry&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT size="2"&gt;NA&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;NA&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;NA&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;Jack&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;Bob&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT size="2"&gt;Jack, Bob&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Fri, 09 Jun 2023 23:30:37 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Get-column-values-using-Loop/m-p/276127#M53585</guid>
      <dc:creator>brandon_mcrv</dc:creator>
      <dc:date>2023-06-09T23:30:37Z</dc:date>
    </item>
    <item>
      <title>Re: Get column values using Loop</title>
      <link>https://community.jmp.com/t5/Discussions/Get-column-values-using-Loop/m-p/276153#M53590</link>
      <description>&lt;P&gt;Here are 3 different methods to solve the problem&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;This one loops through the rows and columns, using the column names to come up with the answer
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );

// Create an Example data table
dt = New Table( "Example 1",
	Add Rows( 3 ),
	New Column( "Col 1", Character, "Nominal", Set Values( {"Jane", "NA", "NA"} ) ),
	New Column( "Col 2", Character, "Nominal", Set Values( {"NA", "Henry", "NA"} ) ),
	New Column( "Col 3",
		Character,
		"Nominal",
		Set Values( {"Don", "Terry", "NA"} )
	),
	New Column( "Col 4",
		Character,
		"Nominal",
		Set Values( {"Tom", "NA", "Jack"} ),
		Set Display Width( 48 )
	),
	New Column( "Col 5", Character, "Nominal", Set Values( {"Amy", "NA", "Bob"} ) )
);

// Get the names of the columns in the data table
colNames = dt &amp;lt;&amp;lt; get column names( string );

// Create the new column
dt &amp;lt;&amp;lt; New Column( "All Members", character );

// Loop through each row
For( k = 1, k &amp;lt;= N Rows( dt ), k++,
	// Loop through all columns finding non NA values
	For( i = 1, i &amp;lt;= N Items( colNames ), i++,
		If( Column( colNames[i] )[k] != "NA",
			If( :All Members[k] == "",
				:All Members[k] = Column( colNames[i] )[k],
				:All Members[k] = :All Members[k] || ", " || Column( colNames[i] )[k]
			)
		)
	)
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI&gt;
&lt;LI&gt;This one loops through the rows and columns using the column numbers to solve the problem
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );

// Create an Example data table
dt = New Table( "Example 2",
	Add Rows( 3 ),
	New Column( "Col 1", Character, "Nominal", Set Values( {"Jane", "NA", "NA"} ) ),
	New Column( "Col 2", Character, "Nominal", Set Values( {"NA", "Henry", "NA"} ) ),
	New Column( "Col 3",
		Character,
		"Nominal",
		Set Values( {"Don", "Terry", "NA"} )
	),
	New Column( "Col 4",
		Character,
		"Nominal",
		Set Values( {"Tom", "NA", "Jack"} ),
		Set Display Width( 48 )
	),
	New Column( "Col 5", Character, "Nominal", Set Values( {"Amy", "NA", "Bob"} ) )
);

// Get the number of current columns
numCol = N Cols( dt );

// Create the new column
dt &amp;lt;&amp;lt; New Column( "All Members", character );

// Loop through each row
For( k = 1, k &amp;lt;= N Rows( dt ), k++,
	// Loop through all columns finding non NA values
	For( i = 1, i &amp;lt;= numCol, i++,
		If( Column( i )[k] != "NA",
			If( :All Members[k] == "",
				:All Members[k] = Column( i )[k],
				:All Members[k] = :All Members[k] || ", " || Column( i )[k]
			)
		)
	)
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI&gt;
&lt;LI&gt;This one changes the NA values in the data table, to blanks, which is the normal way to indicate missing values for character columns, and then uses a built in function called "Combine Columns()" to create the desired column
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );

// Create an Example data table
dt = New Table( "Example 3",
	Add Rows( 3 ),
	New Column( "Col 1", Character, "Nominal", Set Values( {"Jane", "NA", "NA"} ) ),
	New Column( "Col 2", Character, "Nominal", Set Values( {"NA", "Henry", "NA"} ) ),
	New Column( "Col 3",
		Character,
		"Nominal",
		Set Values( {"Don", "Terry", "NA"} )
	),
	New Column( "Col 4",
		Character,
		"Nominal",
		Set Values( {"Tom", "NA", "Jack"} ),
		Set Display Width( 48 )
	),
	New Column( "Col 5", Character, "Nominal", Set Values( {"Amy", "NA", "Bob"} ) )
);

// Get the names of the columns in the data table
colNames = dt &amp;lt;&amp;lt; get column names( string );

// Loop through the columns and delete the NA values
For( i = 1, i &amp;lt;= N Cols( dt ), i++,
	dt &amp;lt;&amp;lt; select where( as column( colNames[i] ) == "NA" );
	// set the found cells to a blank
	try( column( colNames[i] )[ dt &amp;lt;&amp;lt; get selected rows ] = "" )
);

// Create the new column using a built in function
dt &amp;lt;&amp;lt; Combine Columns(
	delimiter( "," ),
	Columns( eval(colNames) ),
	Selected Columns are Indicator Columns(0 ),
	Column Name( "All Members" )
);

// Move the column to the last position
dt &amp;lt;&amp;lt; Move Selected Columns( :All Members, To last );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jun 2020 09:22:37 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Get-column-values-using-Loop/m-p/276153#M53590</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2020-06-30T09:22:37Z</dc:date>
    </item>
    <item>
      <title>Re: Get column values using Loop</title>
      <link>https://community.jmp.com/t5/Discussions/Get-column-values-using-Loop/m-p/276910#M53794</link>
      <description>Hi txnelson, I really appreciate for your willingness to share multiple solutions! The scripts work like a charm and really thanks ! :)&lt;/img&gt;</description>
      <pubDate>Mon, 06 Jul 2020 08:19:29 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Get-column-values-using-Loop/m-p/276910#M53794</guid>
      <dc:creator>brandon_mcrv</dc:creator>
      <dc:date>2020-07-06T08:19:29Z</dc:date>
    </item>
  </channel>
</rss>

