Choose Language Hide Translation Bar
Highlighted
gallardet
Level III

How to use formula with different columns in a script

Hi.
I have a data table with numeric columns and I intend to create new columns with the result of dividing column 1 by column 2, column 3 divided by column 4 and so on.

 

I have this script but my problem is that I don't know how to indicate column 2 in the dividend.

 

numericColNames = dt << get column names( string, numeric );
For( i = 1, i <= N Items( numericColNames ) - 1, i += 2,
    Eval(
        Substitute(
                Expr(
                    New Column( Char( numericColNames[i] ) || "_Stand",
                        Numeric,
                        Continuous,
                        Formula( (__col__) / (__col__) )
                    )
                ),
            Expr( __col__ ), Parse( ":" || numericColNames[i] )
        )
    )
);
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
txnelson
Super User

Re: How to use formula with different columns in a script

You are very close.  All you need to do, is to create a second value to substitute on, and to reference the i+1 column for it's substitution

names default to here(2);
dt=open("$SAMPLE_DATA/blood pressure.jmp");

numericColNames = dt << get column names( string, numeric );
For( i = 1, i <= N Items( numericColNames ) - 1, i += 2,
	Eval(
		Substitute(
				Expr(
					New Column( Char( numericColNames[i] ) || "_Stand",
						Numeric,
						Continuous,
						Formula( (__col1__) / (__col2__) )
					)
				),
			Expr( __col1__ ), Parse( ":" || numericColNames[i] ),
			Expr( __col2__ ), Parse( ":" || numericColNames[i+1] )
		)
	)
);
Jim

View solution in original post

2 REPLIES 2
Highlighted
txnelson
Super User

Re: How to use formula with different columns in a script

You are very close.  All you need to do, is to create a second value to substitute on, and to reference the i+1 column for it's substitution

names default to here(2);
dt=open("$SAMPLE_DATA/blood pressure.jmp");

numericColNames = dt << get column names( string, numeric );
For( i = 1, i <= N Items( numericColNames ) - 1, i += 2,
	Eval(
		Substitute(
				Expr(
					New Column( Char( numericColNames[i] ) || "_Stand",
						Numeric,
						Continuous,
						Formula( (__col1__) / (__col2__) )
					)
				),
			Expr( __col1__ ), Parse( ":" || numericColNames[i] ),
			Expr( __col2__ ), Parse( ":" || numericColNames[i+1] )
		)
	)
);
Jim

View solution in original post

Highlighted
gallardet
Level III

Re: How to use formula with different columns in a script

Muchas gracias Sr. Nelson. Es lo que buscaba.
Article Labels