cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
%3CLINGO-SUB%20id%3D%22lingo-sub-722542%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3EEstoy%20confundido%20acerca%20de%20las%20variables%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-722542%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3ETengo%20un%20problema%20conceptual%20al%20descubrir%20c%C3%B3mo%20funcionan%20las%20variables%20locales.%20Simplificar%C3%A9%20el%20tema%20para%20centrarme%20en%20lo%20que%20me%20resulta%20confuso.%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CP%3ECreo%20una%20variable%20local%2C%20digamos%20t0%2C%20con%20un%20valor%20de%205.%20Luego%20escribo%20una%20f%C3%B3rmula%20de%20columna%20para%20incrementar%20este%20valor%20en%201...%20ya%20sea%20t0%3Dt0%2B1%20o%20t0%2B%3D1%20(no%20parece%20importar)%2C%20El%20resultado%20que%20esperaba%20era%20que%20cuando%20JMP%20evaluara%20cada%20celda%20de%20la%20columna%2C%20incrementar%C3%ADa%20la%20variable%20para%20obtener%206%2C%207%2C%208%2C%20etc.%20a%20medida%20que%20avanzaba%20hacia%20abajo%20en%20la%20columna.%20En%20cambio%2C%20obtengo%20una%20columna%20de%206.%20Entonces%20la%20f%C3%B3rmula%20no%20incrementa%20la%20variable.%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CP%3E%C2%BFAlguien%20puede%20explicarme%20qu%C3%A9%20es%20lo%20que%20no%20entiendo%20acerca%20de%20c%C3%B3mo%20JMP%20maneja%20las%20variables%20locales%3F%20Adem%C3%A1s%2C%20dado%20que%20mi%20enfoque%20no%20funciona%2C%20%C2%BFhay%20otra%20forma%20de%20incrementar%20una%20variable%3F%20Puedo%20hacerlo%20con%20Lag%20pero%20parece%20innecesariamente%20complejo%20para%20algo%20tan%20simple.%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CP%3EAdem%C3%A1s%2C%20no%20tengo%20clara%20la%20distinci%C3%B3n%20entre%20variables%20locales%2C%20variables%20de%20tabla%20y%20par%C3%A1metros.%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CP%3EGracias%20de%20antemano%20por%20toda%20la%20ayuda%20brindada.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-722542%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CLINGO-LABEL%3EAutomatizaci%C3%B3n%20y%20secuencias%20de%20comandos%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-722589%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20Estoy%20confundido%20acerca%20de%20las%20variables%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-722589%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EYou%20might%20be%20interested%20in%20the%20%3CCODE%20class%3D%22%20language-jsl%22%3ERow()%3C%2FCODE%3E%20function%20inside%20of%20a%20column%20formula%20--%20it%20returns%20the%20current%20row%20that%20the%20formula%20is%20being%20run%20against.%20%20As%20for%20variables%20and%20scoping%20--%20it%20is%20usually%20best%20to%20be%20explicit%20about%20variable%20scopes.%20%20Thus%20to%20use%20a%20global%20counter%2C%20do%20%3CCODE%20class%3D%22%20language-jsl%22%3E%3A%3At0%20%2B%3D%201%3C%2FCODE%3E%20--%20the%20double%20colon%20explicitly%20scopes%20the%20name%20to%20the%20global%20namespace.%20%20If%20you%20want%20a%20local%20name%2C%20explicitly%20scope%20it%2C%20i.e.%2C%20%3CCODE%20class%3D%22%20language-jsl%22%3Elocal%3At0%3C%2FCODE%3E%20%20There%20is%20also%20the%20%3CCODE%20class%3D%22%20language-jsl%22%3EHere%3C%2FCODE%3E%20namespace%2C%20and%20again%20it%20is%20best%20to%20be%20explicit%20in%20scoping.%20%20I%20recommend%20being%20explicit%20as%20JMP%20is%20buggy%20and%20will%20not%20always%20adhere%20to%20the%20stated%20scoping%20rules.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-722702%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20Estoy%20confundido%20acerca%20de%20las%20variables%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-722702%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3ESupongo%20que%20escribiste%20algo%20como%20lo%20siguiente%20como%20f%C3%B3rmula%20de%20columna%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3ELocal(%7Bt0%20%3D%205%7D%2C%20t0%20%3D%20t0%20%2B%201)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EY%20esperas%20que%20la%20tarea%20inicial%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Et0%20%3D%205%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3Esolo%20ocurre%20una%20vez%20cuando%20la%20tabla%20de%20datos%20eval%C3%BAa%20una%20columna%2C%20celda%20por%20celda%20de%20arriba%20a%20abajo.%3C%2FP%3E%0A%3CP%3EHas%20visto%20que%20eso%20no%20est%C3%A1%20sucediendo.%20Lo%20que%20ve%20es%20que%20la%20asignaci%C3%B3n%20ocurre%20cada%20vez%20que%20se%20eval%C3%BAa%20una%20celda.%20Si%20tiene%20experiencia%20en%20programaci%C3%B3n%20SAS%2C%20es%20posible%20que%20est%C3%A9%20buscando%20algo%20similar%20a%20la%20declaraci%C3%B3n%20RETAIN%20en%20SAS.%20No%20tengo%20conocimiento%20de%20que%20tal%20cosa%20exista%20en%20el%20lenguaje%20de%20programaci%C3%B3n%20de%20JMP.%3C%2FP%3E%0A%3CP%3EPero%20tuve%20la%20necesidad%20similar%20de%20hacer%20c%C3%A1lculos%20que%20necesitan%20recuperar%20resultados%20en%20c%C3%A1lculos%20anteriores.%3C%2FP%3E%0A%3CP%3ESi%20el%20resultado%20es%20intermedio%20y%20temporal%2C%20lo%20almacenar%C3%A9%20en%20una%20columna%20separada.%20Si%20el%20resultado%20es%20solo%20un%20valor%20en%20la%20celda%20anterior%20de%20la%20misma%20columna%2C%20simplemente%20me%20referir%C3%A9%20a%20la%20celda%20anterior.%20De%20cualquier%20manera%2C%20es%20posible%20que%20necesites%20usar%20la%20funci%C3%B3n%20Row()%20que%20mencion%C3%B3%20%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F26363%22%20target%3D%22_blank%22%3E%40ErraticAttack%3C%2FA%3E%20.%3C%2FP%3E%0A%3CP%3EAdjunto%20un%20ejemplo%20para%20ilustrar.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Choose Language Hide Translation Bar
scott1588
Level IV

I'm Confused About Variables

I'm having a conceptual problem figuring out how local variables work. I'll simplify the issue to focus in on what is confusing to me.

 

I create a local variable, say t0, with a value of 5. Then I write a column formula to increment this value by 1... either t0=t0+1 or t0+=1 (it doesn't seem to matter), the result that I expected was that when JMP evaluated each cell in the column, it would increment the variable so I'd get 6, 7, 8, etc as I moved down the column. Instead, I get a column of 6's. So the formula is not incrementing the variable.

 

Can someone explain to me what I'm not understanding about how JMP handles local variables? Also, since my approach doesn't work is there another way to increment a variable? I can do it with Lag but that seems needlessly complex for something so simple.

 

Also, I'm not clear on the distinction between local variables, table variables, and parameters.

 

Thanks in advance for all help provided.

4 REPLIES 4
ErraticAttack
Level VI

Re: I'm Confused About Variables

You might be interested in the Row() function inside of a column formula -- it returns the current row that the formula is being run against.  As for variables and scoping -- it is usually best to be explicit about variable scopes.  Thus to use a global counter, do ::t0 += 1 -- the double colon explicitly scopes the name to the global namespace.  If you want a local name, explicitly scope it, i.e., local:t0  There is also the Here namespace, and again it is best to be explicit in scoping.  I recommend being explicit as JMP is buggy and will not always adhere to the stated scoping rules.

Jordan
peng_liu
Staff

Re: I'm Confused About Variables

I guess that you wrote something like the following as a column formula:

Local({t0 = 5}, t0 = t0 + 1)

And you expect that the initial assignment

t0 = 5

only occurs once when the data table evaluate a column, cell by cell from top to bottom.

You have seen that is not happening. What you see is the assignment occurs every time when a cell is evaluated. If you have SAS programming experience, you might be looking for something similar to RETAIN statement in SAS. I am not aware of such thing exists in JMP's scripting language.

But I had the similar need to do calculations that need to retrieve results in previous calculations.

If the result is intermediate and temporary, I will store the result in a separate column. If the result is just a value in previous cell in the same column, I will just refer to the previous cell. Either way, you may need to use the Row() function that @ErraticAttack mentioned.

I attach an example to illustrate.

jthi
Super User

Re: I'm Confused About Variables

When you want to have incrementing value in a formula you usually have three options:

  • Use Row()
  • Use As Constant() to define a variable(s)
  • Check if you are on first row and then define initial value for your variable

 

Names Default To Here(1);

dt = New Table("",
	Add Rows(10)
);
dt << delete column(1);

dt << New Column("AsConstant", Numeric, Continuous, Formula(
	As Constant(myvar = 4); // one less than the value you want on first row
	myvar = myvar + 1;
));

dt << New Column("Row()", Numeric, Continuous, Formula(
	Row() + 4;
));

dt << New Column("IfFirstRow", Numeric, Continuous, Formula(
	If(Row() == 1,
		myvar2 = 4;
	);
	myvar2 = myvar2 + 1;
));

 

 

Rest of this post gets a bit messy

 

What can happen, is that your variables "escape" to other formulas if you aren't careful

 

Names Default To Here(1);

dt = New Table("",
	Add Rows(10)
);
dt << delete column(1);

col1 = dt << New Column("AsConstant", Numeric, Continuous, Formula(
	As Constant(myvar = 4); // one less than the value you want on first row
	myvar = myvar + 1;
));

col2 = dt << New Column("AsConstant", Numeric, Continuous, Formula(
	myvar;
));

dt << New Column("AsConstant", Numeric, Continuous, Formula(
	myvar = 10;
));

dt << New Column("AsConstant", Numeric, Continuous, Formula(
	myvar;
));

// Observe the values for column 2
wait(2);
dt << add rows(1);
wait(2);
col1 << Suppress Eval(1);
wait(2);
dt << add rows(1);

 

 

 

More general

There are two types of variables in JMP: global and local. What local means is determined by the scope which defines it. Scripting Guide > JSL Building Blocks > Global and Local Variables. You can read regarding variables, scope and namespace from Scripting Guide from JMP Help web page. Note that not all of them can be easily applied to formulas and are specific scripting.

 

Documentation regarding formulas seems to have some "fun" with local variable. There is Local Variable, then there is Temporary Variable (which seems to be same as local variable, you just create it with a funny button? Shouldn't the button be Make Local Variable and not Make Temporary Variable?) and finally there is parameter, special type of local variable (no idea what parameter means in this case).

Using JMP > Create Formulas in JMP > Use Local Variables in Formulas 

Using JMP > Create Formulas in JMP > Examples of Building Formulas in the JMP Formula Editor > Use L... 

 

Names Default To Here(1);

dt = New Table("",
	Add Rows(10)
);
dt << delete column(1);

// Messing with Local({})
dt << New Column("Local1", Numeric, Continuous, Formula(
	Local({myvar3 = 4},  // "Constant as we redefine it every time"
		myvar3 = myvar3 + 1
	);
));

dt << New Column("Local2", Numeric, Continuous, Formula(
	Local({myvar4},
		myvar4 = 4;
		myvar4 = myvar4 + 1
	);
));

dt << New Column("Local3", Numeric, Continuous, Formula(
	Local({myvar5},
		If(Row() == 1,
			myvar5 = 4;
		);
		myvar5 = myvar5 + 1;
	);
));

dt << New Column("Local4", Numeric, Continuous, Formula(
	Local({myvar6},
		As Constant(myvar6 = 4);
		myvar6 = myvar6 + 1;
	);
));

 

 

 

 

-Jarmo
scott1588
Level IV

Re: I'm Confused About Variables

This is excellent guidance. Thanks to all of you.