<?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: I'm Confused About Variables in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/I-m-Confused-About-Variables/m-p/722737#M90499</link>
    <description>&lt;P&gt;When you want to have incrementing value in a formula you usually have three options:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Use &lt;A href="https://www.jmp.com/support/help/en/17.2/#page/jmp/row-functions-2.shtml?os=win&amp;amp;source=application#ww2737000" target="_blank" rel="noopener"&gt;Row()&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;Use &lt;A href="https://www.jmp.com/support/help/en/17.2/#page/jmp/programming-functions.shtml?os=win&amp;amp;source=application#ww5032467" target="_blank" rel="noopener"&gt;As Constant()&lt;/A&gt; to define a variable(s)&lt;/LI&gt;
&lt;LI&gt;Check if you are on first row and then define initial value for your variable&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

dt = New Table("",
	Add Rows(10)
);
dt &amp;lt;&amp;lt; delete column(1);

dt &amp;lt;&amp;lt; New Column("AsConstant", Numeric, Continuous, Formula(
	As Constant(myvar = 4); // one less than the value you want on first row
	myvar = myvar + 1;
));

dt &amp;lt;&amp;lt; New Column("Row()", Numeric, Continuous, Formula(
	Row() + 4;
));

dt &amp;lt;&amp;lt; New Column("IfFirstRow", Numeric, Continuous, Formula(
	If(Row() == 1,
		myvar2 = 4;
	);
	myvar2 = myvar2 + 1;
));&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;&lt;STRONG&gt;Rest of this post gets a bit messy&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What can happen, is that your variables "escape" to other formulas if you aren't careful&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

dt = New Table("",
	Add Rows(10)
);
dt &amp;lt;&amp;lt; delete column(1);

col1 = dt &amp;lt;&amp;lt; New Column("AsConstant", Numeric, Continuous, Formula(
	As Constant(myvar = 4); // one less than the value you want on first row
	myvar = myvar + 1;
));

col2 = dt &amp;lt;&amp;lt; New Column("AsConstant", Numeric, Continuous, Formula(
	myvar;
));

dt &amp;lt;&amp;lt; New Column("AsConstant", Numeric, Continuous, Formula(
	myvar = 10;
));

dt &amp;lt;&amp;lt; New Column("AsConstant", Numeric, Continuous, Formula(
	myvar;
));

// Observe the values for column 2
wait(2);
dt &amp;lt;&amp;lt; add rows(1);
wait(2);
col1 &amp;lt;&amp;lt; Suppress Eval(1);
wait(2);
dt &amp;lt;&amp;lt; add rows(1);&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;
&lt;P&gt;&lt;STRONG&gt;More general&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;There are two types of variables in JMP: global and local. What local means is determined by the scope which defines it.&amp;nbsp;&lt;A href="https://www.jmp.com/support/help/en/17.2/#page/jmp/global-and-local-variables.shtml#" target="_blank" rel="noopener"&gt;Scripting Guide &amp;gt; JSL Building Blocks &amp;gt; Global and Local Variables&lt;/A&gt;. You can read regarding variables, scope and namespace from Scripting Guide from JMP Help web page. Note that not all of them can be easily applied to formulas and are specific scripting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Documentation regarding formulas seems to have some "fun" with local variable. There is Local Variable, then there is Temporary Variable (which seems to be same as local variable, you just create it with a funny button? Shouldn't the button be Make Local Variable and not Make Temporary Variable?) and finally there is parameter, special type of local variable (no idea what parameter means in this case).&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.jmp.com/support/help/en/17.2/#page/jmp/use-local-variables-in-formulas.shtml#" target="_blank" rel="noopener"&gt;Using JMP &amp;gt; Create Formulas in JMP &amp;gt; Use Local Variables in Formulas&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.jmp.com/support/help/en/17.2/#page/jmp/use-local-variables-in-formulas-2.shtml#ww108741" target="_self"&gt;Using JMP &amp;gt; Create Formulas in JMP &amp;gt; Examples of Building Formulas in the JMP Formula Editor &amp;gt; Use Local Variables in Formulas&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

dt = New Table("",
	Add Rows(10)
);
dt &amp;lt;&amp;lt; delete column(1);

// Messing with Local({})
dt &amp;lt;&amp;lt; New Column("Local1", Numeric, Continuous, Formula(
	Local({myvar3 = 4},  // "Constant as we redefine it every time"
		myvar3 = myvar3 + 1
	);
));

dt &amp;lt;&amp;lt; New Column("Local2", Numeric, Continuous, Formula(
	Local({myvar4},
		myvar4 = 4;
		myvar4 = myvar4 + 1
	);
));

dt &amp;lt;&amp;lt; New Column("Local3", Numeric, Continuous, Formula(
	Local({myvar5},
		If(Row() == 1,
			myvar5 = 4;
		);
		myvar5 = myvar5 + 1;
	);
));

dt &amp;lt;&amp;lt; New Column("Local4", Numeric, Continuous, Formula(
	Local({myvar6},
		As Constant(myvar6 = 4);
		myvar6 = myvar6 + 1;
	);
));&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 08 Feb 2024 05:46:39 GMT</pubDate>
    <dc:creator>jthi</dc:creator>
    <dc:date>2024-02-08T05:46:39Z</dc:date>
    <item>
      <title>I'm Confused About Variables</title>
      <link>https://community.jmp.com/t5/Discussions/I-m-Confused-About-Variables/m-p/722542#M90487</link>
      <description>&lt;P&gt;I'm having a conceptual problem figuring out how local variables work. I'll simplify the issue to focus in on what is confusing to me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I create a local variable, say t0, with a value of 5. Then I write a column formula to increment this value by 1... either t0=t0+1 or t0+=1 (it doesn't seem to matter), the result that I expected was that when JMP evaluated each cell in the column, it would increment the variable so I'd get 6, 7, 8, etc as I moved down the column. Instead, I get a column of 6's. So the formula is not incrementing the variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone explain to me what I'm not understanding about how JMP handles local variables? Also, since my approach doesn't work is there another way to increment a variable? I can do it with Lag but that seems needlessly complex for something so simple.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, I'm not clear on the distinction between local variables, table variables, and parameters.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance for all help provided.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Feb 2024 17:42:56 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/I-m-Confused-About-Variables/m-p/722542#M90487</guid>
      <dc:creator>scott1588</dc:creator>
      <dc:date>2024-02-07T17:42:56Z</dc:date>
    </item>
    <item>
      <title>Re: I'm Confused About Variables</title>
      <link>https://community.jmp.com/t5/Discussions/I-m-Confused-About-Variables/m-p/722589#M90490</link>
      <description>&lt;P&gt;You might be interested in the &lt;CODE class=" language-jsl"&gt;Row()&lt;/CODE&gt; function inside of a column formula -- it returns the current row that the formula is being run against.&amp;nbsp; As for variables and scoping -- it is usually best to be explicit about variable scopes.&amp;nbsp; Thus to use a global counter, do &lt;CODE class=" language-jsl"&gt;::t0 += 1&lt;/CODE&gt; -- the double colon explicitly scopes the name to the global namespace.&amp;nbsp; If you want a local name, explicitly scope it, i.e., &lt;CODE class=" language-jsl"&gt;local:t0&lt;/CODE&gt;&amp;nbsp; There is also the &lt;CODE class=" language-jsl"&gt;Here&lt;/CODE&gt; namespace, and again it is best to be explicit in scoping.&amp;nbsp; I recommend being explicit as JMP is buggy and will not always adhere to the stated scoping rules.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Feb 2024 18:27:04 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/I-m-Confused-About-Variables/m-p/722589#M90490</guid>
      <dc:creator>ErraticAttack</dc:creator>
      <dc:date>2024-02-07T18:27:04Z</dc:date>
    </item>
    <item>
      <title>Re: I'm Confused About Variables</title>
      <link>https://community.jmp.com/t5/Discussions/I-m-Confused-About-Variables/m-p/722702#M90497</link>
      <description>&lt;P&gt;I guess that you wrote something like the following as a column formula:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Local({t0 = 5}, t0 = t0 + 1)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And you expect that the initial assignment&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;t0 = 5&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;only occurs once when the data table evaluate a column, cell by cell from top to bottom.&lt;/P&gt;
&lt;P&gt;You have seen that is not happening. What you see is the assignment occurs every time when a cell is evaluated. If you have SAS programming experience, you might be looking for something similar to RETAIN statement in SAS. I am not aware of such thing exists in JMP's scripting language.&lt;/P&gt;
&lt;P&gt;But I had the similar need to do calculations that need to retrieve results in previous calculations.&lt;/P&gt;
&lt;P&gt;If the result is intermediate and temporary, I will store the result in a separate column. If the result is just a value in previous cell in the same column, I will just refer to the previous cell. Either way, you may need to use the Row() function that &lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/26363"&gt;@ErraticAttack&lt;/a&gt; mentioned.&lt;/P&gt;
&lt;P&gt;I attach an example to illustrate.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Feb 2024 03:27:00 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/I-m-Confused-About-Variables/m-p/722702#M90497</guid>
      <dc:creator>peng_liu</dc:creator>
      <dc:date>2024-02-08T03:27:00Z</dc:date>
    </item>
    <item>
      <title>Re: I'm Confused About Variables</title>
      <link>https://community.jmp.com/t5/Discussions/I-m-Confused-About-Variables/m-p/722737#M90499</link>
      <description>&lt;P&gt;When you want to have incrementing value in a formula you usually have three options:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Use &lt;A href="https://www.jmp.com/support/help/en/17.2/#page/jmp/row-functions-2.shtml?os=win&amp;amp;source=application#ww2737000" target="_blank" rel="noopener"&gt;Row()&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;Use &lt;A href="https://www.jmp.com/support/help/en/17.2/#page/jmp/programming-functions.shtml?os=win&amp;amp;source=application#ww5032467" target="_blank" rel="noopener"&gt;As Constant()&lt;/A&gt; to define a variable(s)&lt;/LI&gt;
&lt;LI&gt;Check if you are on first row and then define initial value for your variable&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

dt = New Table("",
	Add Rows(10)
);
dt &amp;lt;&amp;lt; delete column(1);

dt &amp;lt;&amp;lt; New Column("AsConstant", Numeric, Continuous, Formula(
	As Constant(myvar = 4); // one less than the value you want on first row
	myvar = myvar + 1;
));

dt &amp;lt;&amp;lt; New Column("Row()", Numeric, Continuous, Formula(
	Row() + 4;
));

dt &amp;lt;&amp;lt; New Column("IfFirstRow", Numeric, Continuous, Formula(
	If(Row() == 1,
		myvar2 = 4;
	);
	myvar2 = myvar2 + 1;
));&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;&lt;STRONG&gt;Rest of this post gets a bit messy&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What can happen, is that your variables "escape" to other formulas if you aren't careful&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

dt = New Table("",
	Add Rows(10)
);
dt &amp;lt;&amp;lt; delete column(1);

col1 = dt &amp;lt;&amp;lt; New Column("AsConstant", Numeric, Continuous, Formula(
	As Constant(myvar = 4); // one less than the value you want on first row
	myvar = myvar + 1;
));

col2 = dt &amp;lt;&amp;lt; New Column("AsConstant", Numeric, Continuous, Formula(
	myvar;
));

dt &amp;lt;&amp;lt; New Column("AsConstant", Numeric, Continuous, Formula(
	myvar = 10;
));

dt &amp;lt;&amp;lt; New Column("AsConstant", Numeric, Continuous, Formula(
	myvar;
));

// Observe the values for column 2
wait(2);
dt &amp;lt;&amp;lt; add rows(1);
wait(2);
col1 &amp;lt;&amp;lt; Suppress Eval(1);
wait(2);
dt &amp;lt;&amp;lt; add rows(1);&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;
&lt;P&gt;&lt;STRONG&gt;More general&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;There are two types of variables in JMP: global and local. What local means is determined by the scope which defines it.&amp;nbsp;&lt;A href="https://www.jmp.com/support/help/en/17.2/#page/jmp/global-and-local-variables.shtml#" target="_blank" rel="noopener"&gt;Scripting Guide &amp;gt; JSL Building Blocks &amp;gt; Global and Local Variables&lt;/A&gt;. You can read regarding variables, scope and namespace from Scripting Guide from JMP Help web page. Note that not all of them can be easily applied to formulas and are specific scripting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Documentation regarding formulas seems to have some "fun" with local variable. There is Local Variable, then there is Temporary Variable (which seems to be same as local variable, you just create it with a funny button? Shouldn't the button be Make Local Variable and not Make Temporary Variable?) and finally there is parameter, special type of local variable (no idea what parameter means in this case).&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.jmp.com/support/help/en/17.2/#page/jmp/use-local-variables-in-formulas.shtml#" target="_blank" rel="noopener"&gt;Using JMP &amp;gt; Create Formulas in JMP &amp;gt; Use Local Variables in Formulas&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.jmp.com/support/help/en/17.2/#page/jmp/use-local-variables-in-formulas-2.shtml#ww108741" target="_self"&gt;Using JMP &amp;gt; Create Formulas in JMP &amp;gt; Examples of Building Formulas in the JMP Formula Editor &amp;gt; Use Local Variables in Formulas&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

dt = New Table("",
	Add Rows(10)
);
dt &amp;lt;&amp;lt; delete column(1);

// Messing with Local({})
dt &amp;lt;&amp;lt; New Column("Local1", Numeric, Continuous, Formula(
	Local({myvar3 = 4},  // "Constant as we redefine it every time"
		myvar3 = myvar3 + 1
	);
));

dt &amp;lt;&amp;lt; New Column("Local2", Numeric, Continuous, Formula(
	Local({myvar4},
		myvar4 = 4;
		myvar4 = myvar4 + 1
	);
));

dt &amp;lt;&amp;lt; New Column("Local3", Numeric, Continuous, Formula(
	Local({myvar5},
		If(Row() == 1,
			myvar5 = 4;
		);
		myvar5 = myvar5 + 1;
	);
));

dt &amp;lt;&amp;lt; New Column("Local4", Numeric, Continuous, Formula(
	Local({myvar6},
		As Constant(myvar6 = 4);
		myvar6 = myvar6 + 1;
	);
));&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Feb 2024 05:46:39 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/I-m-Confused-About-Variables/m-p/722737#M90499</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2024-02-08T05:46:39Z</dc:date>
    </item>
    <item>
      <title>Re: I'm Confused About Variables</title>
      <link>https://community.jmp.com/t5/Discussions/I-m-Confused-About-Variables/m-p/723619#M90571</link>
      <description>&lt;P&gt;This is excellent guidance. Thanks to all of you.&lt;/P&gt;</description>
      <pubDate>Sat, 10 Feb 2024 19:21:28 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/I-m-Confused-About-Variables/m-p/723619#M90571</guid>
      <dc:creator>scott1588</dc:creator>
      <dc:date>2024-02-10T19:21:28Z</dc:date>
    </item>
  </channel>
</rss>

