cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
fahmadfu
Level II

How To Update New Table Variable value in Function

What is the best way to update the New Table Variable value inside a function. At this moment, I am doing it as below. However, I found that this Days variable will not get updated and it will create a new variable called Days 2.

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
dt << New Table Variable( "Days", 42 );   // In original data table, Days set to default which is 42


// assume below is a new function, and I'm passing dt as input and I want to update the Days variable
myFunction = Function ({dt},
	
	// I want this Days variable get updated with new value below, 100
	dt << New Table Variable ( "Days", 100);
	
	dt << Select Where( :Name( "sex" ) == "F" );
	dt << Subset( Copy formula( 1 ), Suppress formula evaluation( 0 ), Selected Rows( 1 ), Selected columns only( 0 ) );
	dt = Data Table (Current Data Table() << Get Name());
);


myFunction(dt)

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
jthi
Super User

Re: How To Update New Table Variable value in Function

Scripting Index

jthi_0-1696242700313.png

so use << Set Table Variable instead of creating a new one

-Jarmo

View solution in original post

txnelson
Super User

Re: How To Update New Table Variable value in Function

  1. Your functions have 2 input variables specified, and you are only specifying one when you call the function.
  2. You are scoping the variables incorrectly.  Using  :a  in your script is indicating to JMP to look for a column named "a".  The colon ":" specified in front of a variable tells JMP the reference is pointing to a data table column.  Eight drop the colon from the variable reference or specify    Here:a    which is the formal name of the variable, when you have specified
  3. Names Default to Here( 1 );
Jim

View solution in original post

5 REPLIES 5
jthi
Super User

Re: How To Update New Table Variable value in Function

Scripting Index

jthi_0-1696242700313.png

so use << Set Table Variable instead of creating a new one

-Jarmo
fahmadfu
Level II

Re: How To Update New Table Variable value in Function

Hi,

 

Thanks for this suggestion, and I think its working.  Additional to this, I've some extra question about this variable update. Do you know how can JSL update the data table variable based on new variable that we declared outside from the function? I modified the code as below and I think that it is not working. I want the Set Table Variable to be able to load values from a and b variable.

 

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
dt << New Table Variable( "Days", 42 );   // In original data table, Days set to default which is 42

// declare new variable, this will be input from user
a = 100;
b = 200;

// assume below is a new function, and I'm passing dt and a as input
myFunction1 = Function ({dt, a},
	
	// I want this Days variable get updated with new value based on a
	dt << Set Table Variable ( "Days", :a);
	
	dt << Select Where( :Name( "sex" ) == "F" );
	dt << Subset( Copy formula( 1 ), Suppress formula evaluation( 0 ), Selected Rows( 1 ), Selected columns only( 0 ) );
	dt = Data Table (Current Data Table() << Get Name());
);

// assume below is a new function, and I'm passing dt and b as input
myFunction2 = Function ({dt, b},
	
	// I want this Days variable get updated with new value based on b
	dt << Set Table Variable ( "Days", :b);
	
	dt << Select Where( :Name( "sex" ) == "M" );
	dt << Subset( Copy formula( 1 ), Suppress formula evaluation( 0 ), Selected Rows( 1 ), Selected columns only( 0 ) );
	dt = Data Table (Current Data Table() << Get Name());
);

myFunction1(dt);
myFunction2(dt);

  

 

txnelson
Super User

Re: How To Update New Table Variable value in Function

  1. Your functions have 2 input variables specified, and you are only specifying one when you call the function.
  2. You are scoping the variables incorrectly.  Using  :a  in your script is indicating to JMP to look for a column named "a".  The colon ":" specified in front of a variable tells JMP the reference is pointing to a data table column.  Eight drop the colon from the variable reference or specify    Here:a    which is the formal name of the variable, when you have specified
  3. Names Default to Here( 1 );
Jim
fahmadfu
Level II

Re: How To Update New Table Variable value in Function

Oh, I missed that part. Got it. 

Now it is working as expected. Thanks Jim.

txnelson
Super User

Re: How To Update New Table Variable value in Function

With a table variable, if you don't use

dt << Set Table Variable

you will need to delete the variable first, and then create it again.

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
dt << New Table Variable( "Days", 42 );   // In original data table, Days set to default which is 42


// assume below is a new function, and I'm passing dt as input and I want to update the Days variable
myFunction = Function ({dt},
	
	// I want this Days variable get updated with new value below, 100
	dt << Delete Table Variable ( "Days");
	dt << New Table Variable ( "Days", 100);
	
	dt << Select Where( :Name( "sex" ) == "F" );
	dt << Subset( Copy formula( 1 ), Suppress formula evaluation( 0 ), Selected Rows( 1 ), Selected columns only( 0 ) );
	dt = Data Table (Current Data Table() << Get Name());
);

 

Jim