Choose Language Hide Translation Bar
Highlighted

## create a weight Column

Good morning.

using JSL code, starting from a column with GOOD / BAD rows, i would like to create a new WEIGHT COLUMN, where BAD =1 and GOOD = Ratio BAD/GOOD.

There is JSL to do it quickly?

Gianpaolo

Gianpaolo Polsinelli
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

## Re: create a weight Column

Here are two similar solutions.

This one generates the new column in open JSL

``````Names Default To Here( 1 );

// Create the sample data table
dt = New Table( "Good Bad Ratio",
Character,
"Nominal",
Set Values(
{"GOOD", "BAD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD",
)
)
);

// Create the new column using a formula for the calculation
dt << New Column( "Weight",
formula(
If( Row() == 1,
TheGoodCount = Col Number( If( :Good_Bad == "GOOD", 1, . ) );
);
If(
);
)
);``````

This version uses a column formula to create the values for the column Weight

``````Names Default To Here( 1 );

// Create the sample data table
dt = New Table( "Good Bad Ratio",
Character,
"Nominal",
Set Values(
{"GOOD", "BAD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD",
)
)
);

// Calculate the Ratio
TheGoodCount = Col Number( If( dt:Good_Bad == "GOOD", 1, . ) );

// Add the new column and populate it
dt << New Column( "Weight" );
For Each Row(
If(
dt:Good_Bad == "GOOD", dt:Weight = Ratio,
)
);``````
Jim
4 REPLIES 4
Highlighted

## Re: create a weight Column

Can you kindly provide sample data of your column to understand what GOOD / BAD looks like ?
Best
Uday
Highlighted

## Re: create a weight Column

Split Weight Split where:
GOOD 0.4 in this case 4/10 =0.4
GOOD 0.4
GOOD 0.4
GOOD 0.4
GOOD 0.4
GOOD 0.4
GOOD 0.4
GOOD 0.4
GOOD 0.4
Gianpaolo Polsinelli
Highlighted

## Re: create a weight Column

Here are two similar solutions.

This one generates the new column in open JSL

``````Names Default To Here( 1 );

// Create the sample data table
dt = New Table( "Good Bad Ratio",
Character,
"Nominal",
Set Values(
{"GOOD", "BAD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD",
)
)
);

// Create the new column using a formula for the calculation
dt << New Column( "Weight",
formula(
If( Row() == 1,
TheGoodCount = Col Number( If( :Good_Bad == "GOOD", 1, . ) );
);
If(
);
)
);``````

This version uses a column formula to create the values for the column Weight

``````Names Default To Here( 1 );

// Create the sample data table
dt = New Table( "Good Bad Ratio",
Character,
"Nominal",
Set Values(
{"GOOD", "BAD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD", "GOOD",
)
)
);

// Calculate the Ratio
TheGoodCount = Col Number( If( dt:Good_Bad == "GOOD", 1, . ) );

// Add the new column and populate it
dt << New Column( "Weight" );
For Each Row(
If(
dt:Good_Bad == "GOOD", dt:Weight = Ratio,
)
);``````
Jim
Highlighted

## Re: create a weight Column

Great Jim, Thanks very much.