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
Asterix
Level I

Declaring a variable inside a column formula

Hi,

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

2 REPLIES 2
Ressel
Level VI

Re: Declaring a variable inside a column formula

Welcome 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:

Ressel_0-1727703983643.png

 

Store your table in a variable and use that variable in your script. In the screenshot example, that'd be 'dt'.

 

hogi
Level XII

Re: Declaring a variable inside a column formula

Indeed 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()

: )

hogi_0-1727707451592.png

 

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