cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Learn how to build custom Python data connectors and further customize JMP’s Data Connector Framework with the Python Data Connector Demo, available now in the JMP Marketplace!
  • See how to create experiments to support product design and ID useful product features. Register for June 12 webinar, 2pm US Eastern Time.

Discussions

Solve problems, and share tips and tricks with other JMP users.
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 VII

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 XIII

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

Recommended Articles