cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Discussions

Solve problems, and share tips and tricks with other JMP users.
%3CLINGO-SUB%20id%3D%22lingo-sub-765189%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3EDichiarare%20una%20variabile%20all'interno%20di%20una%20formula%20di%20colonna%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-765189%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3ECIAO%2C%3C%2FP%3E%3CP%3E%C3%88%20buona%20norma%20dichiarare%20una%20variabile%20all'interno%20di%20una%20formula%20di%20colonna%3F%20Oppure%20questo%20crea%20rischi%20di%20errori%3F%20La%20mia%20formula%20funziona%20bene%2C%20ma%20temo%20che%20possa%20essere%20inaffidabile.%3C%2FP%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Erownum%20%3D%20Row()%3B%0AIf(%20%3AMeasurement%20type%20%3D%3D%20%22STUDY_VARIABLE%22%2C%0A%09%3AControl%20means%5BMin(%0A%09%09Current%20Data%20Table()%20%26lt%3B%26lt%3B%20get%20rows%20where(%20%3AMeasurement%20type%20%3D%3D%20%22STUDY_CONTROL%22%20%26amp%3B%20%3AControl%20group%20%3D%3D%20%3AControl%20group%5Brownum%5D%20)%0A%09)%5D%2C%0A%09%3AControl%20means%0A)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3ELa%20configurazione%20%C3%A8%20la%20seguente%3A%3C%2FP%3E%3CUL%3E%3CLI%3E%3AIl%20tipo%20di%20misurazione%20%C3%A8%20una%20colonna%20che%20etichetta%20ciascuna%20riga%20come%20STUDY_CONTROL%20o%20STUDY_VARIABLE%3C%2FLI%3E%3CLI%3E%3AIl%20gruppo%20di%20controllo%20%C3%A8%20una%20colonna%20che%20utilizza%20un'etichetta%20(solo%20una%20lettera)%20per%20associare%20le%20righe%20che%20sono%20STUDY_VARIABLES%20con%20la%20media%20appropriata%20(%3AControl%20significa)%20delle%20relative%20righe%20STUDY_CONTROL%3C%2FLI%3E%3CLI%3E%3AControl%20group%5BRow()%5D%20non%20ha%20funzionato%2C%20quindi%20ho%20specificato%20la%20variabile%20rownum%20in%20anticipo%3C%2FLI%3E%3C%2FUL%3E%3CP%3E%C3%88%20una%20soluzione%20elegante%20o%20un%20trucco%20pericoloso%3F%3C%2FP%3E%3CP%3ECome%20seconda%20domanda%2C%20l'editor%20mi%20avverte%20che%20%C3%A8%20pericoloso%20utilizzare%20Current%20Data%20Table()%20in%20una%20formula%3A%20esiste%20un'alternativa%20migliore%3F%3C%2FP%3E%3CP%3EMolte%20grazie%20per%20l'eventuale%20assistenza%3A%20questa%20%C3%A8%20la%20mia%20prima%20domanda!%3C%2FP%3E%3CP%3EDavide%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-802726%22%20slang%3D%22en-US%22%20mode%3D%22UPDATE%22%3ERe%3A%20Dichiarazione%20di%20una%20variabile%20all'interno%20di%20una%20formula%20di%20colonna%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-802726%22%20slang%3D%22en-US%22%20mode%3D%22UPDATE%22%3E%3CP%3EBenvenuti%20nel%20club!%20%3A)%3C%2Fimg%3E%3A)%3C%2Fimg%3E%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CP%3EPer%20quanto%20riguarda%20la%20tua%20domanda%20su%20'Current%20Data%20Table()'%2C%20ecco%20uno%20screenshot%20dallo%20script%20condiviso%20nella%20%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2FJMP-Scripters-Club-Discussions%2FScripters-Club-Recording-Building-Robust-and-fail-safe-scripts%2Fm-p%2F788286%23U788286%22%20class%3D%22lia-mention-container-editor-message%20lia-img-icon-forum-thread%20lia-fa-icon%20lia-fa-forum%20lia-fa-thread%20lia-fa%22%20target%3D%22_blank%22%3Eregistrazione%20di%20Scripters%20Club%3A%20Creazione%20di%20script%20robusti%20e%20a%20prova%20di%20errore%3A%3C%2FA%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Ressel_0-1727703983643.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22Ressel_0-1727703983643.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22Ressel_0-1727703983643.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22Ressel_0-1727703983643.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F68723iE83E94E25BD08DCE%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Ressel_0-1727703983643.png%22%20alt%3D%22Ressel_0-1727703983643.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CP%3EMemorizza%20la%20tua%20tabella%20in%20una%20variabile%20e%20usa%20quella%20variabile%20nel%20tuo%20script.%20Nell'esempio%20dello%20screenshot%2C%20sarebbe%20'dt'.%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-802740%22%20slang%3D%22en-US%22%20mode%3D%22UPDATE%22%3ERe%3A%20Dichiarazione%20di%20una%20variabile%20all'interno%20di%20una%20formula%20di%20colonna%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-802740%22%20slang%3D%22en-US%22%20mode%3D%22UPDATE%22%3E%3CP%3EDavvero%20una%20lezione%20meravigliosa%20sullo%20scripting%20JSL!%3C%2FP%3E%3CP%3EQuesta%20ora%20ti%20far%C3%A0%20risparmiare%20mesi%20di%20mal%20di%20testa%20%3A%20)%3CBR%20%2F%3E%3CBR%20%2F%3E%20Per%20il%20tuo%20problema%2C%20dai%20un'occhiata%20a%20%3CFONT%20face%3D%22courier%20new%2Ccourier%22%3ECol%20Min(%20...)%3C%2FFONT%3E%20.%3CBR%20%2F%3E%20Far%C3%A0%20tutto%20il%20lavoro%20per%20te%3A%3CBR%20%2F%3E%20Non%20c'%C3%A8%20bisogno%20di%20%3CEM%3Edichiarare%3C%2FEM%3E%20alcun%20rownum%20-%20non%20c'%C3%A8%20bisogno%20di%20accedere%20alla%20%3CSTRONG%3E%3CEM%3Etabella%20dati%20corrente%3C%2FEM%3E%3C%2FSTRONG%3E%20n%C3%A9%20di%20%3CSTRONG%3E%3CEM%3Eottenere%20righe%20where()%3C%2FEM%3E%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3A%20)%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22hogi_0-1727707451592.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22hogi_0-1727707451592.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F68727i2F216F9A5F2E853B%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22hogi_0-1727707451592.png%22%20alt%3D%22hogi_0-1727707451592.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3ENames%20Default%20to%20Here(1)%3B%0Adt%20%3D%20Open(%20%22%24SAMPLE_DATA%2FBig%20Class.jmp%22%20)%3B%0Adt%20%26lt%3B%26lt%3B%20New%20Column(%20%22Measurement%20type%22%2C%0A%09Character%2C%0A%09Formula(%0A%09%09Map%20Value(%0A%09%09%09%3Asex%2C%0A%09%09%09%7B%22F%22%2C%20%22STUDY_VARIABLE%22%2C%20%22M%22%2C%20%22STUDY_CONTROL%22%7D%0A%09%09)%0A%09)%0A)%3B%0A%3Aweight%20%26lt%3B%26lt%3B%20Set%20Name(%20%22Control%20means%22%20)%3B%0A%3Aage%20%26lt%3B%26lt%3B%20Set%20Name(%20%22Control_Group%22%20)%3B%0A%0A%0A%2F%2F%20%5E%5E%5E%5E%5E%5E%5E%5Eprepare%20table%0A%0A%2F%2F%20vvvvvvvv%20how%20to%20use%20Col%20Min()%0A%0ANew%20Column(%20%22myCol%22%2C%0A%09Formula(%0A%09%09If(%20%3AMeasurement%20type%20%3D%3D%20%22STUDY_VARIABLE%22%2C%0A%09%09%09Col%20Minimum(%0A%09%09%09%09If(%20%3AMeasurement%20type%20%3D%3D%20%22STUDY_CONTROL%22%2C%0A%09%09%09%09%09%3AControl%20means%2C%0A%09%09%09%09%09Empty()%0A%09%09%09%09)%2C%0A%09%09%09%09%3AControl_Group%0A%09%09%09)%2C%0A%09%09%09%3AControl%20means%0A%09%09)%0A%09)%0A)%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
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