When you want to have incrementing value in a formula you usually have three options:
- Use Row()
- Use As Constant() to define a variable(s)
- Check if you are on first row and then define initial value for your variable
Names Default To Here(1);
dt = New Table("",
Add Rows(10)
);
dt << delete column(1);
dt << New Column("AsConstant", Numeric, Continuous, Formula(
As Constant(myvar = 4); // one less than the value you want on first row
myvar = myvar + 1;
));
dt << New Column("Row()", Numeric, Continuous, Formula(
Row() + 4;
));
dt << New Column("IfFirstRow", Numeric, Continuous, Formula(
If(Row() == 1,
myvar2 = 4;
);
myvar2 = myvar2 + 1;
));
Rest of this post gets a bit messy
What can happen, is that your variables "escape" to other formulas if you aren't careful
Names Default To Here(1);
dt = New Table("",
Add Rows(10)
);
dt << delete column(1);
col1 = dt << 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 << New Column("AsConstant", Numeric, Continuous, Formula(
myvar;
));
dt << New Column("AsConstant", Numeric, Continuous, Formula(
myvar = 10;
));
dt << New Column("AsConstant", Numeric, Continuous, Formula(
myvar;
));
// Observe the values for column 2
wait(2);
dt << add rows(1);
wait(2);
col1 << Suppress Eval(1);
wait(2);
dt << add rows(1);
More general
There are two types of variables in JMP: global and local. What local means is determined by the scope which defines it. Scripting Guide > JSL Building Blocks > Global and Local Variables. 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.
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).
Using JMP > Create Formulas in JMP > Use Local Variables in Formulas
Using JMP > Create Formulas in JMP > Examples of Building Formulas in the JMP Formula Editor > Use L...
Names Default To Here(1);
dt = New Table("",
Add Rows(10)
);
dt << delete column(1);
// Messing with Local({})
dt << New Column("Local1", Numeric, Continuous, Formula(
Local({myvar3 = 4}, // "Constant as we redefine it every time"
myvar3 = myvar3 + 1
);
));
dt << New Column("Local2", Numeric, Continuous, Formula(
Local({myvar4},
myvar4 = 4;
myvar4 = myvar4 + 1
);
));
dt << New Column("Local3", Numeric, Continuous, Formula(
Local({myvar5},
If(Row() == 1,
myvar5 = 4;
);
myvar5 = myvar5 + 1;
);
));
dt << New Column("Local4", Numeric, Continuous, Formula(
Local({myvar6},
As Constant(myvar6 = 4);
myvar6 = myvar6 + 1;
);
));
-Jarmo