cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
jmm1974
Level I

For Loop to Create Columns with Moving average formulas

I'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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: For Loop to Create Columns with Moving average formulas

Welcome 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 ) ) )
			)
		)
	)
);

 

 

Jim

View solution in original post

3 REPLIES 3
txnelson
Super User

Re: For Loop to Create Columns with Moving average formulas

Welcome 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 ) ) )
			)
		)
	)
);

 

 

Jim
jmm1974
Level I

Re: For Loop to Create Columns with Moving average formulas

Perfect!  Thank you!

jthi
Super User

Re: For Loop to Create Columns with Moving average formulas

I 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 

-Jarmo