For Loop to Create Columns with Moving average formulasI'm very new to jmp, trying to move from spreadsheets for big datasets. I'm trying to create a series of columns to calculate different moving averages for a data column (xV-C). I have most of the code working to create the columns, but the formula for each created column uses the for-loop variable (j) instead of the value for the for-loop (10, 11, 12...) for each column. I think I need to do something with Eval and Parse, but I can't figure it out. This is what I have so far:
for(j=10,j<=12,j++,
New Column("xF-C MA " || Char(j),Numeric,Formula(Col Moving Average( :"xV-C"n, 1, j-1 )))
);
I'm sure this is an easy one for someone! I appreciate the help!
Automation and ScriptingWindowsRe: For Loop to Create Columns with Moving average formulasWelcome to the community
With a variable specified in the formula, "J" and the changing of its value in the JSL, the columns with the variable will change as the value of "J" changes. To rectify this, one needs to evaluate the value of the variable "J" as the formula is specified to JMP.
Eval(EvalExpr()) will force the evaluation before JMP parses the code
Try this
For( j = 10, j <= 12, j++,
Eval(
Eval Expr(
New Column( "xF-C MA x " || Char( j ),
Numeric,
Formula( Col Moving Average( :"xV-C"n, 1, Expr( j - 1 ) ) )
)
)
)
);
Re: For Loop to Create Columns with Moving average formulasI like using Eval+EvalExpr but there is one more good option Eval+Substitute which can sometimes be easier to read (not always!). In your case it could look something like this
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
For(j = 10, j <= 12, j++,
Eval(Substitute(
Expr(
dt << New Column("height " || Char(j), Numeric, Formula(Col Moving Average(:height, 1, _lag_)))
),
Expr(_lag_), j - 1
));
);
Good (old) post related to this topic Insert one expression into another using Eval Insert, Eval Expr, Parse, and Substitute
Re: For Loop to Create Columns with Moving average formulasPerfect! Thank you!