Declaring a variable inside a column formulaHi,
Is it good practice to declare a variable inside a column formula? Or does this create risk of errors? My formula works well, but I'm concerned that it might be unreliable.
rownum = Row();
If( :Measurement type == "STUDY_VARIABLE",
:Control means[Min(
Current Data Table() << get rows where( :Measurement type == "STUDY_CONTROL" & :Control group == :Control group[rownum] )
)],
:Control means
);
The setup is as follows:
- :Measurement type is a column which labels each row as either STUDY_CONTROL or STUDY_VARIABLE
- :Control group is a column which uses a label (just a letter) to associate rows which are STUDY_VARIABLES with the appropriate mean (:Control means) of the relevant STUDY_CONTROL rows
- :Control group[Row()] didn't work, so I specified the rownum variable up front
Is this an elegant solution or a dangerous hack?
As a second question, the editor warns me that it's dangerous to use Current Data Table() in a formula - is there a better alternative?
Many thanks for any assistance - this is my first question!
David
Re: Declaring a variable inside a column formulaWelcome to the club!:):)
Regarding your question on 'Current Data Table()', here a screenshot from the shared script in Scripters Club Recording: Building Robust and fail-safe scripts:

Store your table in a variable and use that variable in your script. In the screenshot example, that'd be 'dt'.
Re: Declaring a variable inside a column formulaIndeed a wonderful lecture on JSL scripting!
This 1hr will save you months of headache : )
For you problem, please have a look at Col Min( ...).
It will do all the job for you:
No need to declare any rownum - no need to access the current data table nor to get rows where()
: )

Names Default to Here(1);
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
dt << New Column( "Measurement type",
Character,
Formula(
Map Value(
:sex,
{"F", "STUDY_VARIABLE", "M", "STUDY_CONTROL"}
)
)
);
:weight << Set Name( "Control means" );
:age << Set Name( "Control_Group" );
// ^^^^^^^^prepare table
// vvvvvvvv how to use Col Min()
New Column( "myCol",
Formula(
If( :Measurement type == "STUDY_VARIABLE",
Col Minimum(
If( :Measurement type == "STUDY_CONTROL",
:Control means,
Empty()
),
:Control_Group
),
:Control means
)
)
)