Choose Language Hide Translation Bar
Highlighted

## Define Column Formula from User Input

Hi,

I am trying to define a new column formula from user input.  I have tried different combinations of Eval(), Eval Insert(), and Parse() without success.  Input can either be columns or lists of columns, plus a scalar.  Below is a minimal example.  Any suggestions?  I am using JMP14, but I hope the solution will work for JMP12 and above.

``````Names Default To Here( 1 );
dt = Open( "\$SAMPLE_DATA/Big Class.jmp" );

// Define new column.
dt << New Column( "Mean for Each Age and Sex Group",
Formula( Col Mean( :height, :age, :sex ) )
);

// Define user input.
a = 2;
c1 = :height;
c2 = {:age, :sex};

// Define new column from user input.
dt << New Column( "Mean for Each Age and Sex Group 2",
Formula( Col Mean( a * c1, c2 ) )
);``````
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

## Re: Define Column Formula from User Input

With help from @ih's input, here is a solution for anyone interested...

``````Names Default To Here( 1 );
dt = Open( "\$SAMPLE_DATA/Big Class.jmp" );

// Define user input.
a = 2;
c1 = Column( "height" );
c2 = {:age, :sex};

// Define new column from user input.
input = Concat( "As Column( \!"", c1 << Get Name, "\!" )" );
If( N Items( c2 ) > 0,
For( i = 1, i <= N Items( c2 ), i++,
input = Insert( input, Concat( ", ", "As Column( \!"", Char( c2[i] ), "\!" )" ) )
)
);
Eval( Parse( Eval Insert( "dt << New Column( \!"Mean for Each Age and Sex Group 2\!", Formula( ^a^ * Col Mean( ^input^ ) ) )" ) ) );``````
3 REPLIES 3
Highlighted

## Re: Define Column Formula from User Input

I am not totally sure what formula you want in the newly created column from user input, but take a look at this JSL Cookbook article, it gives examples of several different methods of constructing your new column formula.

## Re: Define Column Formula from User Input

Thanks!  Very useful page.

Highlighted

## Re: Define Column Formula from User Input

With help from @ih's input, here is a solution for anyone interested...

``````Names Default To Here( 1 );
dt = Open( "\$SAMPLE_DATA/Big Class.jmp" );

// Define user input.
a = 2;
c1 = Column( "height" );
c2 = {:age, :sex};

// Define new column from user input.
input = Concat( "As Column( \!"", c1 << Get Name, "\!" )" );
If( N Items( c2 ) > 0,
For( i = 1, i <= N Items( c2 ), i++,
input = Insert( input, Concat( ", ", "As Column( \!"", Char( c2[i] ), "\!" )" ) )
)
);
Eval( Parse( Eval Insert( "dt << New Column( \!"Mean for Each Age and Sex Group 2\!", Formula( ^a^ * Col Mean( ^input^ ) ) )" ) ) );``````