cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Submit your abstract to the call for content for Discovery Summit Americas by April 23. Selected abstracts will be presented at Discovery Summit, Oct. 21- 24.
Discovery is online this week, April 16 and 18. Join us for these exciting interactive sessions.
Choose Language Hide Translation Bar
safwan
Level I

Dynamic Column Reference in Function

Hello,

I'm trying to write a function that refers to a certain row of a column and trying to make it dynamic where I can run it on any column name. Using the Big Class table as sample, I'm trying to run the calculation on the height column but I couldn't get it working unless its hardcoded in the function. Also the lag function doesn't seem to work within a function.

 

This is the script that I have.

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

Row() = 4;

ii = 4;

Test = Function({tag},

Y = tag[ii] - tag[ii-1]; // this doesn't work

//Y = height[ii] - height[ii-1]; this works

X = lag(tag,1); // this doesn't work

//X = lag(height,1); this works

);

Test(height)

 

Thanks in advance

 

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Dynamic Column Reference in Function

See the annotated script below

t = Open( "$SAMPLE_DATA/Big Class.jmp" );
Row() = 4;
ii = 4;
Test = Function({tag},
Y = column(t,tag)[ii] - column(t,tag)[ii-1]; // this doesn't work
//Y = height[ii] - height[ii-1]; this works
X = lag(column(t,tag)[ii],1); // this doesn't work
//X = lag(height,1); this works
// If you want the calculated values to be returned to a value such as calling the function
//  rr = Test("Height")
// uncomment the code below
//YXList = {};
//insert into(YXList,Y);
//insert into(YXList,X);
//return(YXList);
);

// If you specify 
// Test(height);
// JMP will look for the value of a variable called height
// You need to pass the column name as a string and have the
// function process it
Test("height")
Jim

View solution in original post

2 REPLIES 2
txnelson
Super User

Re: Dynamic Column Reference in Function

See the annotated script below

t = Open( "$SAMPLE_DATA/Big Class.jmp" );
Row() = 4;
ii = 4;
Test = Function({tag},
Y = column(t,tag)[ii] - column(t,tag)[ii-1]; // this doesn't work
//Y = height[ii] - height[ii-1]; this works
X = lag(column(t,tag)[ii],1); // this doesn't work
//X = lag(height,1); this works
// If you want the calculated values to be returned to a value such as calling the function
//  rr = Test("Height")
// uncomment the code below
//YXList = {};
//insert into(YXList,Y);
//insert into(YXList,X);
//return(YXList);
);

// If you specify 
// Test(height);
// JMP will look for the value of a variable called height
// You need to pass the column name as a string and have the
// function process it
Test("height")
Jim
safwan
Level I

Re: Dynamic Column Reference in Function

This is great now I got it.
Thanks very much! Really appreciate it.