<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Some Insights on Dynamic Column Formulas in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Some-Insights-on-Dynamic-Column-Formulas/m-p/781160#M96368</link>
    <description>&lt;P&gt;Instead of storing the column names as strings and converting them to "names" via &lt;FONT face="courier new,courier"&gt;as name&lt;/FONT&gt;, one can also store the names (with or w/o ":")&amp;nbsp; - and retrieve and conserve them via &lt;FONT face="courier new,courier"&gt;Name Expr&lt;/FONT&gt;:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = Open( "$SAMPLE_DATA/big class.jmp" );
col1 = Name Expr(:height);
col2 = Name Expr(:weight);

new name = "new_" || (col1 &amp;lt;&amp;lt; get name);

col1 &amp;lt;&amp;lt; set name("tmp"); // we don't care - the reference will still point to the right column 

var = Expr(
	dt &amp;lt;&amp;lt; New Column( new name , Formula( __c1__ + __c2__ ) )
);

var = Substitute(	Name Expr(var),
	Expr( __c1__ ), Name Expr( col1 ),
	Expr( __c2__ ), Name Expr( col2 )
);

/*alternative:
Substitute Into(	var,
	Expr( __c1__ ), Name Expr( col1 ),
	Expr( __c2__ ), Name Expr( col2 )
);*/

Eval( var );&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 13 Aug 2024 09:37:00 GMT</pubDate>
    <dc:creator>hogi</dc:creator>
    <dc:date>2024-08-13T09:37:00Z</dc:date>
    <item>
      <title>Some Insights on Dynamic Column Formulas</title>
      <link>https://community.jmp.com/t5/Discussions/Some-Insights-on-Dynamic-Column-Formulas/m-p/781132#M96360</link>
      <description>&lt;P&gt;Being a JSL beginner, I find I often struggle with script interpretation. If I find a script in a discussion that seems to address my problem and try to customize it, I often break it and have a hard time understanding how to fix it. During my JSL scripting journey I have heavily relied on this forum for assistance -- both searching the extensive archives for past answers and asking for help if the answers are not apparent. This forum is an invaluable resource for learning JSL and the depth of knowledge here and willingness to share is amazing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Recently, I've been playing around with dynamic column formulas and came across a very helpful post from a few years ago &lt;A href="https://community.jmp.com/t5/Discussions/call-a-column-by-referring-to-variable/td-p/272339" target="_self"&gt;here&lt;/A&gt;. The solution from&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/2687"&gt;@txnelson&lt;/a&gt;&amp;nbsp;and the follow up suggestion from &lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/51369"&gt;@NisKorsgaard&lt;/a&gt;&amp;nbsp;were quite revealing to me. &amp;nbsp;Because I am so new to JSL, I often like to "unpack" the code to figure out what each piece does. I did this for the Dynamic Column Formula problem I was having. The exercise was very helpful for me and I thought it might also be for others so I have included the "unpacked" code below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The script does nothing practical but provides a simple example of &amp;nbsp;how column formulas may be dynamically changed. Hopefully someone will find this useful.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;// CALL COLUMN BY REFERRING TO VARIABLE
// See this Discussion for more info:
// From: https://community.jmp.com/t5/Discussions/call-a-column-by-referring-to-variable/td-p/272339


// Original script from @txnelson

//Names Default To Here( 1 );
//dt = Open( "$SAMPLE_DATA/big class.jmp" );
//columnname1 = Column( dt, 4 ) &amp;lt;&amp;lt; Get Name;
//columnname2 = Column( dt, 5 ) &amp;lt;&amp;lt; Get Name;
//
//Eval(
//	Substitute(
//			Expr(
//				dt &amp;lt;&amp;lt; New Column( "new_" || columnname1,
//					Numeric,
//					continuous,
//					formula( __c1__ + __c2__ )
//				)
//			),
//		Expr( __c1__ ), Parse( ":" || columnname1 ),
//		Expr( __c2__ ), Parse( ":" || columnname2 )
//	)
//);




// ==================================================
// REVISED SCRIPT
// Pulls apart the above script a bit. For me, this was easier to visualize
// and understand. Also helped clarify how variables work and why the
// EVAL EXPR function is needed. When running script look at the log for changing
// variable contents.



// SETUP
// **************************************************

Names Default To Here( 1 );

dt = Open( "$SAMPLE_DATA/big class.jmp" );
columnName1 = Column( dt, 4 ) &amp;lt;&amp;lt; Get Name;
columnName2 = Column( dt, 5 ) &amp;lt;&amp;lt; Get Name;



// FORMULA WITH PLACEHOLDERS
// Create expression with desired formula using placeholders for each variable and store result in var.
// **************************************************

var = Expr(
	dt &amp;lt;&amp;lt; New Column( "new_" || columnName1, Formula( __c1__ + __c2__ ) )
);

Show (var); // For learning and debugging. Look in log for value of var.



// SWAP OUT PLACEHOLDERS FOR COLUMN NAME STORED IN VARIABLES
// Replace placeholders with variable contents, make them column names and store result in subVar.
// To see the effect of EVAL EXPR, try substituting EVAL (remove EXPR) and re-running the script.
// Look at the log.
// **************************************************

subVar = Substitute(
	Eval Expr( var ),
	Expr( __c1__ ), As Name( columnName1 ),
	Expr( __c2__ ), As Name( columnName2 )
);

Show( subVar);  // For learning and debugging. Look in log for value of subVar.



// DO IT
// Make the column with the new formula.
// **************************************************
	
Eval( subVar );&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Aug 2024 18:38:18 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Some-Insights-on-Dynamic-Column-Formulas/m-p/781132#M96360</guid>
      <dc:creator>scott1588</dc:creator>
      <dc:date>2024-08-12T18:38:18Z</dc:date>
    </item>
    <item>
      <title>Re: Some Insights on Dynamic Column Formulas</title>
      <link>https://community.jmp.com/t5/Discussions/Some-Insights-on-Dynamic-Column-Formulas/m-p/781155#M96364</link>
      <description>&lt;P&gt;This is a bit old post but a good starting point &lt;LI-MESSAGE title="Insert one expression into another using Eval Insert, Eval Expr, Parse, and Substitute" uid="48998" url="https://community.jmp.com/t5/JSL-Cookbook-Archived/Insert-one-expression-into-another-using-Eval-Insert-Eval-Expr/m-p/48998#U48998" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-tkb-thread lia-fa-icon lia-fa-tkb lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;. When I have enough time I will write my view about these (expressions and handling them in few different cases) as a blog post.&lt;/P&gt;</description>
      <pubDate>Mon, 12 Aug 2024 19:06:50 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Some-Insights-on-Dynamic-Column-Formulas/m-p/781155#M96364</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2024-08-12T19:06:50Z</dc:date>
    </item>
    <item>
      <title>Re: Some Insights on Dynamic Column Formulas</title>
      <link>https://community.jmp.com/t5/Discussions/Some-Insights-on-Dynamic-Column-Formulas/m-p/781158#M96367</link>
      <description>&lt;P&gt;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/14366"&gt;@jthi&lt;/a&gt;&amp;nbsp;, thanks for this&amp;nbsp;introductory course about the basics of expression handling :&lt;/P&gt;&lt;P&gt;&lt;LI-MESSAGE title="Session 9: Advanced JSL" uid="680045" url="https://community.jmp.com/t5/JMP-Scripters-Club-Discussions/Session-9-Advanced-JSL/m-p/680045#U680045" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-forum-thread lia-fa-icon lia-fa-forum lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;33min - ...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;another great lecture:&lt;/P&gt;&lt;P&gt;&lt;LI-MESSAGE title="Using JSL to Develop Efficient, Robust Applications (EU 2018 415)" uid="51456" url="https://community.jmp.com/t5/Discovery-Summit-Europe-2018/Using-JSL-to-Develop-Efficient-Robust-Applications-EU-2018-415/m-p/51456#U51456" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-tkb-thread lia-fa-icon lia-fa-tkb lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Eval(Substitute())&lt;/FONT&gt; is one of a few tools you should carry like a Swiss army knife for JSL scripting : )&lt;/P&gt;&lt;P&gt;&lt;LI-MESSAGE title="Expression Handling in JMP: Tipps and Trapdoors" uid="747728" url="https://community.jmp.com/t5/Discussions/Expression-Handling-in-JMP-Tipps-and-Trapdoors/m-p/747728#U747728" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-forum-thread lia-fa-icon lia-fa-forum lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Aug 2024 19:58:31 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Some-Insights-on-Dynamic-Column-Formulas/m-p/781158#M96367</guid>
      <dc:creator>hogi</dc:creator>
      <dc:date>2024-08-12T19:58:31Z</dc:date>
    </item>
    <item>
      <title>Re: Some Insights on Dynamic Column Formulas</title>
      <link>https://community.jmp.com/t5/Discussions/Some-Insights-on-Dynamic-Column-Formulas/m-p/781160#M96368</link>
      <description>&lt;P&gt;Instead of storing the column names as strings and converting them to "names" via &lt;FONT face="courier new,courier"&gt;as name&lt;/FONT&gt;, one can also store the names (with or w/o ":")&amp;nbsp; - and retrieve and conserve them via &lt;FONT face="courier new,courier"&gt;Name Expr&lt;/FONT&gt;:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = Open( "$SAMPLE_DATA/big class.jmp" );
col1 = Name Expr(:height);
col2 = Name Expr(:weight);

new name = "new_" || (col1 &amp;lt;&amp;lt; get name);

col1 &amp;lt;&amp;lt; set name("tmp"); // we don't care - the reference will still point to the right column 

var = Expr(
	dt &amp;lt;&amp;lt; New Column( new name , Formula( __c1__ + __c2__ ) )
);

var = Substitute(	Name Expr(var),
	Expr( __c1__ ), Name Expr( col1 ),
	Expr( __c2__ ), Name Expr( col2 )
);

/*alternative:
Substitute Into(	var,
	Expr( __c1__ ), Name Expr( col1 ),
	Expr( __c2__ ), Name Expr( col2 )
);*/

Eval( var );&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 13 Aug 2024 09:37:00 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Some-Insights-on-Dynamic-Column-Formulas/m-p/781160#M96368</guid>
      <dc:creator>hogi</dc:creator>
      <dc:date>2024-08-13T09:37:00Z</dc:date>
    </item>
  </channel>
</rss>

