<?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 Loop? to count missing data from  work with several columns and with column names which change names between the analysis (dates) in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Loop-to-count-missing-data-from-work-with-several-columns-and/m-p/344911#M59502</link>
    <description>&lt;P&gt;I have a question that I hope this community can help me with. I wish to add a column showing the consecutive days missing from the last day and backwards.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also wish to add a column counting the days where the result is higher than 0.02 from 7 columns.&lt;/P&gt;&lt;P&gt;I have found a solution for both by using a series of if statements but wish to learn if it can be done in a more elegant way, maybe by using a loop.&lt;/P&gt;&lt;P&gt;Lastly the dates in the table is changing week to week so I wish this formula to be independent of the column names. Is this possible?&lt;/P&gt;&lt;P&gt;I have attached a similarly looking data table, with my solutions.&lt;/P&gt;&lt;P&gt;Thank you in advance&lt;/P&gt;&lt;P&gt;/Markku&lt;/P&gt;</description>
    <pubDate>Fri, 09 Jun 2023 22:02:11 GMT</pubDate>
    <dc:creator>Markku</dc:creator>
    <dc:date>2023-06-09T22:02:11Z</dc:date>
    <item>
      <title>Loop? to count missing data from  work with several columns and with column names which change names between the analysis (dates)</title>
      <link>https://community.jmp.com/t5/Discussions/Loop-to-count-missing-data-from-work-with-several-columns-and/m-p/344911#M59502</link>
      <description>&lt;P&gt;I have a question that I hope this community can help me with. I wish to add a column showing the consecutive days missing from the last day and backwards.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also wish to add a column counting the days where the result is higher than 0.02 from 7 columns.&lt;/P&gt;&lt;P&gt;I have found a solution for both by using a series of if statements but wish to learn if it can be done in a more elegant way, maybe by using a loop.&lt;/P&gt;&lt;P&gt;Lastly the dates in the table is changing week to week so I wish this formula to be independent of the column names. Is this possible?&lt;/P&gt;&lt;P&gt;I have attached a similarly looking data table, with my solutions.&lt;/P&gt;&lt;P&gt;Thank you in advance&lt;/P&gt;&lt;P&gt;/Markku&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 22:02:11 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Loop-to-count-missing-data-from-work-with-several-columns-and/m-p/344911#M59502</guid>
      <dc:creator>Markku</dc:creator>
      <dc:date>2023-06-09T22:02:11Z</dc:date>
    </item>
    <item>
      <title>Re: Loop? to count missing data from  work with several columns and with column names which change names between the analysis (dates)</title>
      <link>https://community.jmp.com/t5/Discussions/Loop-to-count-missing-data-from-work-with-several-columns-and/m-p/344932#M59504</link>
      <description>&lt;P&gt;Here is the alternative formula that I came up with&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;If( Row() == 1,
	start = 2;
	end = 8;
);
count = end;
While( Is Missing( Column( count )[Row()] ) == 1 &amp;amp; count &amp;gt;= start, count-- );
end - count;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 30 Dec 2020 14:27:51 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Loop-to-count-missing-data-from-work-with-several-columns-and/m-p/344932#M59504</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2020-12-30T14:27:51Z</dc:date>
    </item>
    <item>
      <title>Re: Loop? to count missing data from  work with several columns and with column names which change names between the analysis (dates)</title>
      <link>https://community.jmp.com/t5/Discussions/Loop-to-count-missing-data-from-work-with-several-columns-and/m-p/345212#M59546</link>
      <description>&lt;P&gt;Dear &lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/22346"&gt;@Markku&lt;/a&gt; ,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i also struggle to create dynamic scripts and parse them into a column formula. In the past i had a similar situation to yours, where the number of repeated measures (in your case batches repeat each day) could change from one observation (batch) to another.&lt;/P&gt;
&lt;P&gt;my solution may look a bit rough for your needs but i think it should work.&lt;/P&gt;
&lt;P&gt;please do check if it robust to different scenarios you may have before usage.&lt;/P&gt;
&lt;P&gt;with your data table open run this script and inspect the results.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Current Data Table();

// get list of columns 
Cols = dt &amp;lt;&amp;lt; get column names ( Continuous );
maxlist = {};

For( i = 1, i &amp;lt;= N Items( cols ), i++,
	If( Contains( cols[i], "Max" ),
		Insert Into( maxlist, cols[i] )
	)
);


dt2 = dt &amp;lt;&amp;lt; Stack(
	columns(maxlist	),
	Source Label Column( "Time" ),
	Stacked Data Column( "Data" )
);

dt2 &amp;lt;&amp;lt; Sort(
	By( :Batch#, :Time ),
	Order( Ascending, Descending ),
	replace table
);


dt2 &amp;lt;&amp;lt;New Column( "Index",
	Numeric,
	"Continuous",
	Format( "Best", 12 ),
	Formula( Sum( :Batch#[Index( 1, Row() )] == :Batch# ) )
);

dt2:Index &amp;lt;&amp;lt; Delete Formula;


dt2 &amp;lt;&amp;lt; Select where ( and ( ismissing (:Data), :Index == colmin (:Index, :Batch#)  ) );
dt2 &amp;lt;&amp;lt; Go To (:Batch#);
dt2 &amp;lt;&amp;lt; Select Matching Cells();
dt2 &amp;lt;&amp;lt; invert row selection ();
dt2 &amp;lt;&amp;lt; delete rows ();


dt2 &amp;lt;&amp;lt; select where (! ismissing (:Data))&amp;lt;&amp;lt; delete rows ();

dt2 &amp;lt;&amp;lt;New Column( "Index2",
	Numeric,
	"Continuous",
	Format( "Best", 12 ),
	Formula( Sum( :Batch#[Index( 1, Row() )] == :Batch# ) )
);
dt2:Index2 &amp;lt;&amp;lt; Delete Formula;

dt2 &amp;lt;&amp;lt; select where (:Index &amp;gt; :Index2  ) &amp;lt;&amp;lt; delete rows ();

dt3 = dt2 &amp;lt;&amp;lt; Summary(
	Group( :Batch# ),
	Freq( "None" ),
	Weight( "None" ),
	Link to original data table( 0 ),

);

close (dt2, no save);

dt3:N Rows &amp;lt;&amp;lt; set name ("No of last days missing");

dt &amp;lt;&amp;lt; new column ("No of last days missing", set each value (0));

dt &amp;lt;&amp;lt; Update(
	With( dt3 ),
	Match Columns( :Batch# = :Batch# )
);

close (dt3, no save);

//
dt5 =  dt &amp;lt;&amp;lt; Stack(
	columns(maxlist	),
	Source Label Column( "Time" ),
	Stacked Data Column( "Data" )
);

dt5 &amp;lt;&amp;lt; select where (ismissing (:Data)) &amp;lt;&amp;lt; delete rows;
dt5 &amp;lt;&amp;lt; select where (:Data&amp;lt;=0.02) &amp;lt;&amp;lt; delete rows;

dt6 = dt5 &amp;lt;&amp;lt; Summary(
	Group( :Batch# ),
	Freq( "None" ),
	Weight( "None" ),
	Link to original data table( 0 )
);

close (dt5, no save);

dt6:N Rows &amp;lt;&amp;lt; set name ("Days over 0.02");

dt &amp;lt;&amp;lt; new column("Days over 0.02", set each value(0));
dt&amp;lt;&amp;lt; Update(
	With( dt6 ),
	Match Columns( :Batch# = :Batch# )
);

close (dt6, no save);
 

&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;let us know if it works,&lt;/P&gt;
&lt;P&gt;Ron&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jan 2021 02:22:38 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Loop-to-count-missing-data-from-work-with-several-columns-and/m-p/345212#M59546</guid>
      <dc:creator>ron_horne</dc:creator>
      <dc:date>2021-01-03T02:22:38Z</dc:date>
    </item>
    <item>
      <title>Re: Loop? to count missing data from  work with several columns and with column names which change names between the analysis (dates)</title>
      <link>https://community.jmp.com/t5/Discussions/Loop-to-count-missing-data-from-work-with-several-columns-and/m-p/345219#M59550</link>
      <description>&lt;P&gt;My solution is most likely a bit more complicated than necessary (full script attached with example_new table creation). This solution expects the columns of interest to be named with "Max(something" (and should work even when there are more than seven "Max(" columns).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;These both formula columns will loose their ability to re-calculate on data change and you will have to rerun them "manually" from tables red triangle if data changes on the datatable:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jthi_0-1609663027359.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/29159iCDD428783B590670/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jthi_0-1609663027359.png" alt="jthi_0-1609663027359.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Formulas for columns:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;//modified idea based on of @txnelson solution
tempCol = dt &amp;lt;&amp;lt; New Column("# last days missing_community", Numeric, Nominal,
	Formula(
		Local({
			//get information on column names
			dt = Current Data Table(),
			colNames = dt &amp;lt;&amp;lt; Get Column Names(String)
			},
			//get list of column names with only Max( in name
			For(i = N Items(colNames), i &amp;gt; 0, i--,
				If(!Contains(colNames[i], "Max("),
					Remove From(colNames, i);
				)
			);
			//get values on rows
			//this might make a mistake if columns before Max( have are missing values
			matLength = N Items(colNames) + 1;
			rowValues = dt[Row(), 0][1::matLength];
			count = 0;
			//calculate missing values from the end of matrix and stop on first found value
			While(Is Missing(rowValues[matLength]), count++; matLength--);
			count;
		)
	)
);

dt &amp;lt;&amp;lt; New Column("#days higher than 0,02_community", Numeric, Nominal,
	Formula(
		Local({
			//get information on column names
			dt = Current Data Table(),
			colNames = dt &amp;lt;&amp;lt; Get Column Names(String)
			},
			//get values on row and set values of colums without Max( in name to 0
			rowVals = dt[Row(), 0];
			For(i = N Items(colNames), i &amp;gt; 0, i--,
				If(!Contains(colNames[i], "Max("),
					rowVals[i] = 0;
				)
			);
			//create matrix with 0 and 1 based on if value is over 0.02
			rowVals_over = rowVals &amp;gt; 0.02;
			//get sum of previous matris to calculate Max( columns with values over 0.02
			Sum(rowVals_over);
		);
	)
);&lt;/CODE&gt;&lt;/PRE&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>Sun, 03 Jan 2021 09:24:52 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Loop-to-count-missing-data-from-work-with-several-columns-and/m-p/345219#M59550</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2021-01-03T09:24:52Z</dc:date>
    </item>
  </channel>
</rss>

