BookmarkSubscribeRSS Feed
Choose Language Hide Translation Bar
Highlighted
safwan

New Contributor

Joined:

Mar 6, 2019

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

Joined:

Jun 22, 2012

Solution

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
2 REPLIES 2
txnelson

Super User

Joined:

Jun 22, 2012

Solution

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

New Contributor

Joined:

Mar 6, 2019

Re: Dynamic Column Reference in Function

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