cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
alex_sas
Level I

How to return a data_table from a FUNCTION ?

script
Hi,

I'm a relatively new user of JMP/SAS scripting language, and only used to other "standard" languages like C, VBA, PASCAL,  etc... so maybe my question is obvious...

I can't figure out how to return cleanly the TABLE result from a function.

If I don't use the DEFAULT LOCAL option, of course the table returns if I use the name of the table int he FUNCTION, but when I try to do something like

d_result= MyFunction(Input1,Input2;Input3);    the variable  d_result is "."

I would like to use the {Default LOCAL} option in order to program cleanly and avoid problem with LOCAL/GLOBAL variables, so hence I would like to output the table.

In general I don't understand how to specify the returning results of a FUNCTION.

I tried with RETURN() in the input params, but it seems only to work with a simple parameter (numer or string), not a table (see Page 212 of JSL Syntax reference):

Function({arguments}, <{local variables}>, <Return(<expr>)>, script)

Thanks a lot for your help !!!

Best regards.

Alex

1 ACCEPTED SOLUTION

Accepted Solutions
pmroz
Super User

Re: How to return a data_table from a FUNCTION ?

JMP functions don't use a return statement.  Rather, the last expression that is evaluated is the one that's returned.  Here's a simple example:

//------------------------------------------------------------------------------

/*

Function Name: get_table

Description: Return a table identifier as selected by the user

Arguments:

(none)

*/

get_table = Function( {},

      {Default Local},

    dt = open();

    dt;

);

// test it out

one_dt = get_table();


Technically the line of the function with just "dt;" is redundant.  I left it in as an example.

View solution in original post

4 REPLIES 4
pmroz
Super User

Re: How to return a data_table from a FUNCTION ?

JMP functions don't use a return statement.  Rather, the last expression that is evaluated is the one that's returned.  Here's a simple example:

//------------------------------------------------------------------------------

/*

Function Name: get_table

Description: Return a table identifier as selected by the user

Arguments:

(none)

*/

get_table = Function( {},

      {Default Local},

    dt = open();

    dt;

);

// test it out

one_dt = get_table();


Technically the line of the function with just "dt;" is redundant.  I left it in as an example.

alex_sas
Level I

Re: How to return a data_table from a FUNCTION ?

Hello

PMroz,

thank you for answering so quickly !

It is indeed a little bit special, but one get used ... :-)

I tried in the mean time and when I did a Summary(all rows), or a similar "transparent" function, it works also.

I got a mistake when I was having at the last line:

current data table(dt);

Now I understand that actually what is returned, is the "output" of current data table, which is probably ".", so that was probably my mistake. Is that right?

What happens with Open files in local functions after one returns to the main program? Apparently the table stays open, although it is "local" ?

BR,

Alex

pmroz
Super User

Re: How to return a data_table from a FUNCTION ?

The table stays open.

The variable dt, which is used inside the function, is local and no longer has a value.

In my example, the calling program uses the variable one_dt to get the results from the function.  Therefore the variable one_dt is visible to the calling program.

When you supply an argument to current data table [i.e. current data table(dt)], it sets the current data table to whatever is pointing to dt, and returns ".".

alex_sas
Level I

Re: How to return a data_table from a FUNCTION ?

Hi PMRoz,

thanks a lot for the clear explanations.

I wish this was explained somehwere clearly in the manuals :-)

Best regards,

Alex