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-662740%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ECr%C3%A9ation%20d'une%20formule%20%C3%A0%20l'aide%20de%20variables%20dynamiques%20dans%20la%20boucle%20for%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-662740%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3ESalut%20tout%20le%20monde%3CBR%20%2F%3E%20Je%20l'ai%20fait%20une%20fois%2C%20et%20c'%C3%A9tait%20douloureux%20mais%20j'ai%20perdu%20le%20script%20et%20le%20souvenir%20de%20cela.%20Ci-dessous%20ce%20que%20j'essaye%20de%20faire%20%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CDIV%3ETweak_List%20%3D%20dt_Temp%20%26lt%3B%26lt%3B%20Obtenir%20les%20noms%20de%20colonne%20(%22String%22)%3B%3C%2FDIV%3E%3CDIV%3Efor(i%20%3D%201%2C%20i%20%26lt%3B%3D%20N%20Items(Tweak_List)%2Ci%2B%2B%2C%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Ea%20%3D%20Gauche(Tweak_List%5Bi%5D%2C%20Longueur(Tweak_List%5Bi%5D)%20-%201)%20%7C%7C%20%22%CE%94%22%26nbsp%3B%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Eb%20%3D%20Tweak_List%5Bi%5D%26nbsp%3B%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Ec%20%3D%20Gauche(Tweak_List%5Bi%5D%2C%20Longueur(Tweak_List%5Bi%5D)%20-%202)%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Edt%20%26lt%3B%26lt%3B%20New%20Column(%20Left(Tweak_List%5Bi%5D%2C%20Length(Tweak_List%5Bi%5D)%20-%201)%20%7C%7C%20%22%CE%94%22%2C%20Num%C3%A9rique%2C%20Continu%2C%20Formule(%20if(%3Aname1%20%3D%3D%20%3Aname2%2C%20c%20-%20b%2C%200%20))))%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E)%3B%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3EDans%20dt%20j'ai%20des%20colonnes%20nomm%C3%A9es%20ColName_2%20et%20ColName%2C%20le%20Tweak_List%20est%20la%20liste%20des%20noms%20de%20colonnes%20comme%20ColName_2.%3CBR%20%2F%3E%20J'ai%20besoin%20de%20cr%C3%A9er%20une%20nouvelle%20colonne%20Delta%20nomm%C3%A9e%20ColName_%3CSPAN%3E%20%CE%94%3C%2FSPAN%3E%20pour%20chaque%20paire%20et%20avoir%20la%20formule%20ColName%20-%20ColName_2%20dans%20laquelle%20une%20condition%20est%20remplie.%3CBR%20%2F%3E%20Je%20me%20souviens%20que%20je%20travaillais%20avec%20Eval%20and%20Co%2C%20mais%20j'ai%20oubli%C3%A9.%20Svp%20aidez%20moi%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-662740%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CLINGO-LABEL%3EAutomatisation%20et%20script%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-662751%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%26nbsp%3B%3A%20Cr%C3%A9ation%20d'une%20formule%20%C3%A0%20l'aide%20de%20variables%20dynamiques%20dans%20la%20boucle%20for%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-662751%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EEt%20comme%20d'habitude%2C%20j'ai%20finalement%20eu%20une%20solution%2C%20libre%20de%20rejouer%20avec%20un%20script%20plus%20efficace.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CDIV%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3EFor(i%20%3D%201%2C%20i%20%26lt%3B%3D%20N%20Items(Tweak_List)%2C%20i%2B%2B%2C%20%0A%20%0A%20Eval(%0A%20%20Eval%20Expr(%0A%20%20%20dt%20%26lt%3B%26lt%3B%20New%20Column(Left(Tweak_List%5Bi%5D%2C%20Length(Tweak_List%5Bi%5D)%20-%201)%20%7C%7C%20%22%CE%94%22%2C%0A%20%20%20%20Numeric%2C%0A%20%20%20%20%22Continuous%22%2C%0A%20%20%20%20Formula(%0A%20%20%20%20%20If(%3AENTITY%20%3D%3D%20%3AENTITY_2%2C%0A%20%20%20%20%20%20As%20Column(Expr(Char(Left(Tweak_List%5Bi%5D%2C%20Length(Tweak_List%5Bi%5D)%20-%202))))%20-%20As%20Column(Expr(Char(Tweak_List%5Bi%5D)))%2C%0A%20%20%20%20%20%200%0A%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20)%0A%20%20)%0A%20)%3B%0A%20%0A)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3EDe%20iH%26nbsp%3B%3A%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2FJSL-Cookbook-Archived%2FInsert-one-expression-into-another-using-Eval-Insert-Eval-Expr%2Fta-p%2F48998%22%20target%3D%22_blank%22%3E%20https%3A%2F%2Fcommunity.jmp.com%2Ft5%2FJSL-Cookbook-Archived%2FInsert-one-expression-into-another-using-Eval-Insert-Eval-Expr%2Fta-p%2F48998%3C%2FA%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-662786%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%26nbsp%3B%3A%20Cr%C3%A9ation%20d'une%20formule%20%C3%A0%20l'aide%20de%20variables%20dynamiques%20dans%20la%20boucle%20for%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-662786%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EVous%20pouvez%20utiliser%20Word()%20ou%20Words()%20au%20lieu%20de%20Left().%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3ENames%20Default%20To%20Here(1)%3B%0A%0Adt%20%3D%20New%20Table(%22Untitled%20143%22%2C%0A%20Add%20Rows(1)%2C%0A%20Compress%20File%20When%20Saved(1)%2C%0A%20New%20Column(%22Column1%22%2C%20Numeric%2C%20%22Continuous%22%2C%20Format(%22Best%22%2C%2012)%2C%20Set%20Values(%5B1%5D))%2C%0A%20New%20Column(%22Column1_2%22%2C%20Numeric%2C%20%22Continuous%22%2C%20Format(%22Best%22%2C%2012)%2C%20Set%20Values(%5B2%5D))%2C%0A%20New%20Column(%22Column2%22%2C%20Numeric%2C%20%22Continuous%22%2C%20Format(%22Best%22%2C%2012)%2C%20Set%20Values(%5B3%5D))%2C%0A%20New%20Column(%22Column2_2%22%2C%20Numeric%2C%20%22Continuous%22%2C%20Format(%22Best%22%2C%2012)%2C%20Set%20Values(%5B4%5D))%0A)%3B%0A%0Atweaklist%20%3D%20%7B%22Column1_2%22%2C%20%22Column2_2%22%7D%3B%0A%0AFor%20Each(%7Bcol_name%7D%2C%20tweaklist%2C%0A%20Eval(EvalExpr(%0A%20%20dt%20%26lt%3B%26lt%3B%20New%20Column(Word(1%2C%20col_name%2C%20%22_%22)%20%7C%7C%20%22_%CE%94%22%2C%20Numeric%2C%20Continuous%2C%20Formula(%0A%20%20%20Expr(Name%20Expr(As%20Column(col_name)))%20-%20Expr(Name%20Expr(As%20Column(Word(1%2C%20col_name%2C%20%22_%22))))%0A%20%20))%3B%0A%20))%3B%0A)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EDe%20plus%2C%20si%20vous%20n'avez%20pas%20besoin%20de%20la%20formule%20mais%20seulement%20des%20valeurs%2C%20vous%20pouvez%20utiliser%20%26lt%3B%26lt%3B%20Set%20Each%20Value%20qui%20n'a%20pas%20besoin%20d'une%20%C3%A9valuation%20aussi%20lourde%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3EFor%20Each(%7Bcol_name%7D%2C%20tweaklist%2C%0A%20dt%20%26lt%3B%26lt%3B%20New%20Column(Word(1%2C%20col_name%2C%20%22_%22)%20%7C%7C%20%22_%CE%94%22%2C%20Numeric%2C%20Continuous%2C%20%26lt%3B%26lt%3B%20Set%20Each%20Value(%0A%20%20As%20Column(col_name)%20-%20As%20Column(Word(1%2C%20col_name%2C%20%22_%22))%0A%20))%3B%0A)%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
Choose Language Hide Translation Bar
Miof
Level II

Creating formula using dynamic variables in for loop

Hi All
I did that one time, and it was painfully but I lost the script and the memory of that. Below what I am trying to do:

Tweak_List = dt_Temp << Get Column Names("String");
For(i = 1, i <= N Items(Tweak_List), i++, 
 
	a = Left(Tweak_List[i], Length(Tweak_List[i]) - 1) || "Δ";
	b = Tweak_List[i];
	c = Left(Tweak_List[i], Length(Tweak_List[i]) - 2);
 
	dt << New Column(Left(Tweak_List[i], Length(Tweak_List[i]) - 1) || "Δ", Numeric, Continuous, Formula(If(:name1 == :name2, c - b, 0)));
);
 
In dt I have columns named ColName_2 and ColName, the Tweak_List is the list of names of columns like ColName_2.
I need do create a new Delta Column named ColName_Δ for each pair and have the formula  ColName - ColName_2 in it is a condition is meet.
I remember I was working with Eval and Co, but I forgot. Pls help me
 
2 REPLIES 2
Miof
Level II

Re: Creating formula using dynamic variables in for loop

And as usual, finally I got one solution, fell free to replay with more efficient script.

For(i = 1, i <= N Items(Tweak_List), i++, 
 
	Eval(
		Eval Expr(
			dt << New Column(Left(Tweak_List[i], Length(Tweak_List[i]) - 1) || "Δ",
				Numeric,
				"Continuous",
				Formula(
					If(:ENTITY == :ENTITY_2,
						As Column(Expr(Char(Left(Tweak_List[i], Length(Tweak_List[i]) - 2)))) - As Column(Expr(Char(Tweak_List[i]))),
						0
					)
				)
			)
		)
	);
 
);
 
jthi
Super User

Re: Creating formula using dynamic variables in for loop

You could use Word() or Words() instead of Left().

Names Default To Here(1);

dt = New Table("Untitled 143",
	Add Rows(1),
	Compress File When Saved(1),
	New Column("Column1", Numeric, "Continuous", Format("Best", 12), Set Values([1])),
	New Column("Column1_2", Numeric, "Continuous", Format("Best", 12), Set Values([2])),
	New Column("Column2", Numeric, "Continuous", Format("Best", 12), Set Values([3])),
	New Column("Column2_2", Numeric, "Continuous", Format("Best", 12), Set Values([4]))
);

tweaklist = {"Column1_2", "Column2_2"};

For Each({col_name}, tweaklist,
	Eval(EvalExpr(
		dt << New Column(Word(1, col_name, "_") || "_Δ", Numeric, Continuous, Formula(
			Expr(Name Expr(As Column(col_name))) - Expr(Name Expr(As Column(Word(1, col_name, "_"))))
		));
	));
);

Also if you don't need the formula but just the values, you could use << Set Each Value which doesn't need as heavy evaluation 

For Each({col_name}, tweaklist,
	dt << New Column(Word(1, col_name, "_") || "_Δ", Numeric, Continuous, << Set Each Value(
		As Column(col_name) - As Column(Word(1, col_name, "_"))
	));
);
-Jarmo

Recommended Articles