<?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 Looping through variables inside a formula in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Looping-through-variables-inside-a-formula/m-p/278008#M53992</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;For a project I´m working on I have created a data table column (Animal No.Avg) where I am calculating some relative values for VO2 based on a baseline value (Treatment C) from table GroupAvg.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JulieSAppel_1-1594384684582.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/25240iBBA0C238A9222776/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JulieSAppel_1-1594384684582.png" alt="JulieSAppel_1-1594384684582.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JulieSAppel_2-1594385049696.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/25241iA4CB5013882DE5EB/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JulieSAppel_2-1594385049696.png" alt="JulieSAppel_2-1594385049696.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In this case there is only one baseline value per assessment (VO2, RQ, EE) as seen in groupAvg as it is an average of several days. For that I created the below which works nicely:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;VarVehicleVo2 = :Name( "Mean(Mean(VO2-[ml/min]))" )[Data Table( "GroupAvg" ) &amp;lt;&amp;lt;
Get rows Where( Contains( :Treatment, "C" ))];

Animal No.Avg &amp;lt;&amp;lt; New Column( "VO2 absolute relative (%)",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Formula(
			Local( {t0 = VarVehicleVo2()},
				(:Name( "Mean(Mean(VO2-[ml/min]))" ) / t0) * 100
			)
		)
	);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Now I have to do the same again, only now the data table (GroupAvgSEM) contains days too and the relative values should be calculated for each day (-6 up to 30).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Of course, I can do the same as above and repeat it for all days, thereby creating a very long script with a local variable for each day but that seems a bit extreme.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was looking into subsetting the Treatment C data and splitting it (on days) in order to loop through it inside the formula:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;VarTable = GroupAvgSEM &amp;lt;&amp;lt; select where(
	Contains( :Treatment, "ehicle" )) &amp;lt;&amp;lt; subset( 
		output table name( "VarTable" ), selected rows( 1 ), selected columns( 0 ));

VarVO2 = VarTable &amp;lt;&amp;lt; split(
			Output table name("VarVO2"),
			Split By( :Day no ),
			Split( :Name( "Mean(Mean(VO2-[ml/min]))" ) ),
			Remaining Columns( Drop All ),
			Sort by Column Property
		);


Animal No.Avg &amp;lt;&amp;lt; New Column("VO2 absolute relative (%)",
    Numeric, &lt;BR /&gt;    Formula(
		Local( {//loop through VarVO2 column values},
			(:Name( "Mean(Mean(VO2-[ml/min]))" ) / /*VarVO2 column values*/) * 100
		))
	);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Is that possible or should I go about this a different way?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Br Julie&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 09 Jun 2023 23:31:35 GMT</pubDate>
    <dc:creator>JulieSAppel</dc:creator>
    <dc:date>2023-06-09T23:31:35Z</dc:date>
    <item>
      <title>Looping through variables inside a formula</title>
      <link>https://community.jmp.com/t5/Discussions/Looping-through-variables-inside-a-formula/m-p/278008#M53992</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;For a project I´m working on I have created a data table column (Animal No.Avg) where I am calculating some relative values for VO2 based on a baseline value (Treatment C) from table GroupAvg.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JulieSAppel_1-1594384684582.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/25240iBBA0C238A9222776/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JulieSAppel_1-1594384684582.png" alt="JulieSAppel_1-1594384684582.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JulieSAppel_2-1594385049696.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/25241iA4CB5013882DE5EB/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JulieSAppel_2-1594385049696.png" alt="JulieSAppel_2-1594385049696.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In this case there is only one baseline value per assessment (VO2, RQ, EE) as seen in groupAvg as it is an average of several days. For that I created the below which works nicely:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;VarVehicleVo2 = :Name( "Mean(Mean(VO2-[ml/min]))" )[Data Table( "GroupAvg" ) &amp;lt;&amp;lt;
Get rows Where( Contains( :Treatment, "C" ))];

Animal No.Avg &amp;lt;&amp;lt; New Column( "VO2 absolute relative (%)",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Formula(
			Local( {t0 = VarVehicleVo2()},
				(:Name( "Mean(Mean(VO2-[ml/min]))" ) / t0) * 100
			)
		)
	);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Now I have to do the same again, only now the data table (GroupAvgSEM) contains days too and the relative values should be calculated for each day (-6 up to 30).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Of course, I can do the same as above and repeat it for all days, thereby creating a very long script with a local variable for each day but that seems a bit extreme.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was looking into subsetting the Treatment C data and splitting it (on days) in order to loop through it inside the formula:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;VarTable = GroupAvgSEM &amp;lt;&amp;lt; select where(
	Contains( :Treatment, "ehicle" )) &amp;lt;&amp;lt; subset( 
		output table name( "VarTable" ), selected rows( 1 ), selected columns( 0 ));

VarVO2 = VarTable &amp;lt;&amp;lt; split(
			Output table name("VarVO2"),
			Split By( :Day no ),
			Split( :Name( "Mean(Mean(VO2-[ml/min]))" ) ),
			Remaining Columns( Drop All ),
			Sort by Column Property
		);


Animal No.Avg &amp;lt;&amp;lt; New Column("VO2 absolute relative (%)",
    Numeric, &lt;BR /&gt;    Formula(
		Local( {//loop through VarVO2 column values},
			(:Name( "Mean(Mean(VO2-[ml/min]))" ) / /*VarVO2 column values*/) * 100
		))
	);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Is that possible or should I go about this a different way?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Br Julie&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 23:31:35 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Looping-through-variables-inside-a-formula/m-p/278008#M53992</guid>
      <dc:creator>JulieSAppel</dc:creator>
      <dc:date>2023-06-09T23:31:35Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables inside a formula</title>
      <link>https://community.jmp.com/t5/Discussions/Looping-through-variables-inside-a-formula/m-p/278025#M53996</link>
      <description>&lt;P&gt;Try this&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = Data Table("Animal No.Avg");
tb = dt &amp;lt;&amp;lt;
Tabulate(
	Show Control Panel( 0 ),
	Add Table(
		Column Table(
			Analysis Columns( :Name( "Mean(VO2-[ml/min])" ) ),
			Statistics( Mean )
		),
		Row Table( Grouping Columns( :Day no ) )
	),
	Local Data Filter(
		Add Filter( columns( :Treatment ), Where( :Treatment == "C" ) )
	)
);
dt1 = tb &amp;lt;&amp;lt; make into data table;

dt2 = dt &amp;lt;&amp;lt; Join(
	With( dt1 ),
	Select(
		:Treatment,
		:Animal No.,
		:Day no,
		:N Rows,
		:Name( "Mean(VO2-[ml/min])" ),
		:Name( "Mean(RQ)" ),
		:Name( "Mean(EE-[kcal/h])" )
	),
	SelectWith( :Name( "Mean(Mean(VO2-[ml/min]))" ) ),
	By Matching Columns( :Day no = :Day no ),
	Drop multiples( 0, 0 ),
	Include Nonmatches( 0, 0 ),
	Preserve main table order( 1 )
);

dt2 &amp;lt;&amp;lt; New Column("Relative VO2",
	Formula(:Name( "Mean(VO2-[ml/min])" )/:Name( "Mean(Mean(VO2-[ml/min]))" ) * 100)
);

tb &amp;lt;&amp;lt; Close Window;

close(dt1, NoSave);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 10 Jul 2020 20:50:50 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Looping-through-variables-inside-a-formula/m-p/278025#M53996</guid>
      <dc:creator>MathStatChem</dc:creator>
      <dc:date>2020-07-10T20:50:50Z</dc:date>
    </item>
  </channel>
</rss>

