5.4.2 - Bin Code - Teradata Warehouse Miner

In-Database Analytic Functions User Guide

Teradata Warehouse Miner
October 2016
User Guide


Bin Coding is useful when you want to replace a continuous numeric column with a categorical one and produce ordinal values (for example, numeric categorical values where order is meaningful). It uses the same techniques used in Histogram analysis, allowing you to choose between equal-width bins, equal-width bins with a user specified minimum and maximum range, bins with a user specified width, evenly distributed bins, or bins with user-specified boundaries.

If the minimum and maximum are specified, all values less than the minimum are put in to bin 0, while all values greater than the maximum are put into bin N+1. The same is true when the boundary option is specified.

Note that Bin Coding supports numeric and date type columns. If date values are entered, the keyword DATE must precede the date value, which should not be enclosed in single quotes.

There are two transformation parameters required for a Bin Code transformation:
  • binstyle
  • columns


call twm .td_analyze('vartran','database=database name;tablename=table name;General Parameters;bincode{binstyle (bins,nbr of bins);columns (column name)};');

Required Parameters

Controls the name of the output (transformed) column and its data type. The columns parameter is required by all transformations except Derive. A separate transformation is performed for each column in the list. If a column name is followed by a forward slash and a name, the name after the slash becomes the name of the transformed column in the resultant output table. Otherwise the column name is used as the output column name.
For the Derive transformation, the outputname parameter controls the naming of the transformed output column.
The parameter that identifies the type of transformation being performed.
The binstyle parameter can assume five different styles. The style and one or more numeric or date values are placed in parentheses after the keyword binstyle. If this parameter is not specified, 10 equal-width bins are created by default.
Examples of the binstyle styles:
Syntax Example
binstyle (bins,nbr of bins) binstyle (bins,10)
binstyle (binswithboundaries,nbr of bins,lower bound,upper bound) binstyle (binswithboundaries,5,0,100)

This example specifies 5 equal-width bins between the boundaries 0 and 100.

All values less than the minimum are put in to bin 0, while all values greater than the maximum are put into bin N+1.

binstyle (boundaries,b1,b2,…) binstyle (boundaries, 0, 25, 50, 100)

The example above specifies bins between 0 and 25, 25 and 50, and 50 and 100.

All values less than the first boundary value are put in to bin 0, while all values greater than the last boundary value are put into bin N+1.

binstyle (quantiles,nbr of quantiles) binstyle (quantiles,10)

This example specifies 10 approximately evenly distributed bins, with roughly the same number of observations allocated to each bin.

binstyle (width,bin width) binstyle (width,10)

In this example the minimum and maximum values of the column are automatically calculated, then beginning with the observations with the minimum value, bins are assigned using the requested width of 10.

call twm .td_analyze('vartran','database=twm_source;tablename=twm_customer_analysis;bincode={binstyle (bins,5),columns (cust_id/cid1,age/age1,income/inc1)}{binstyle (binswithboundaries,5,0,100),columns (cust_id/cid2,age/age2,income/inc2)}{binstyle (boundaries,0.0,100.0,200.00),columns (cust_id/cid3,age/age3,income/inc3)}{binstyle (width,25),columns (cust_id/cid4,age/age4,income/inc4)}{binstyle (quantiles,5),columns (cust_id/cid5,age/age5,income/inc5)};');
The database containing the input table.
The name of the table to be transformed.
This parameter is required to run a variable transformation. The vartran parameter is always enclosed in single quotes.

General Parameters (separated by semi-colons)

For all transformation types, the datatype parameter is used to cast the column to a desired database data type provided it is compatible with the transformed data.
The allowed output types include:
  • byteint
  • char
  • date
  • decimal
  • float
  • integer
  • smallint
  • time
  • timestamp
  • varchar
  • bigint
  • number
When set to true, this parameter requests a mirrored copy of the output table in the Teradata Database when outputstyle=table.
When set to true, the SQL for the requested transformations is returned as a result set but not executed. When this parameter is not specified or is set to false, the SQL is executed but not returned.
When set to true, requests the output table contain the index columns when outputstyle=table.
When set to true, requests the output table contain a unique primary index when outputstyle=table.
When null replacement is requested, either via a Null Replacement transformation or in combination with a Bin Code, Derive, Design Code, Recode, Rescale, Sigmoid or Z Score transformation, the keycolumns parameter must be specified. The column or columns listed must form a unique key into the input and output table of the transformation.

Requests the generated SQL contain the given locking clause in the appropriate location depending on the output style.

An example of a locking clause when the output style defaults to select is:

LOCKING mydb.mytable FOR ACCESS;

When set to true, requests an output table that may contain duplicate rows when outputstyle=table.
When set to true, requests the output table contain no index columns when outputstyle=table.
Data types supported by various nullstyle parameters are:
Data Type Description Example
literal,value numeric, character, and date nullstyle (literal,value)
mean numeric and date nullstyle (mean)
median numeric and date nullstyle (median)
medianwithoutaveraging any supported data type nullstyle (medianwithoutaveraging)
mode any supported date type nullstyle (mode)
imputed,table any supported data type nullstyle (imputed,tablename)

If date values are entered, the keyword DATE must precede the date value, which should not be enclosed in single quotes.

The database that will contain the resulting output table when outputstyle=table or view.
The allowed output styles are:
  • select
  • table
  • view
If outputstyle is not specified, the function generates a SELECT statement and does not create a table or view.
The name of the output table when outputstyle=table or view.
Requests the generated SQL contain the given WHERE clause in appropriate places in the generated SQL. This is independent of the output style requested.


Examples in this section show how to use binstyle. These examples assume that the td_analyze function has been installed in a database named twm.

This example shows the binswithboundaries and boundaries styles applied to a column of type DATE. Notice the required literal DATE and the required lack of single quotes around the date value (for example, DATE 1995-07-01).

call twm.td_analyze('vartran','database=twm_source;tablename=twm_credit_acct;bincode={binstyle(binswithboundaries,5,DATE 1995-01-01,DATE 1995-12-31),columns(acct_start_date)}{binstyle(boundaries,DATe 1995-01-01,DATE 1995-07-01,DATE 1995-12-31),columns(acct_start_date/start2)};');

The example below demonstrates combined null replacement. Note that keycolumns must be included as a general parameter when null value replacement is performed.

call twm.td_analyze('vartran','database=twm_source;tablename=twm_customer;keycolumns=cust_id;bincode=binstyle(bins,5),nullstyle(literal,0),columns(income);');