help katego

Title

katego -- Tool for categorizing continuous variables

Syntax

katego varname newvar instruction

Where varname is the continuous variable to be categorized, newvar is the new variable that will contain the categories and instruction the value intervals that define each category, in statistical notation.

Description

katego is a tool to quickly split a continuous numerical variable in custom categories. For doing the same on categorical or discrete numerical variables, use recode. For splitting a continuous variable in quantiles, use
xtile.

katego creates a new variable and assigns unique values based on the intervals of varname specified in the instruction. The lowest category will be coded as 0, the next one as 1, the next one as 2, and so on. Both variable
and value labels are created.

In order to work properly, the intervals specified in the instruction must fulfill the following conditions:

- Written using statistical notation, but with spaces instead of commas.

- Separated by spaces.

- Presented in ascending order.

- Specified without overlaps or gaps between them.

Negative and decimal numbers are allowed in the intervals of the instruction.

Brief explanation about statistical notation of intervals

- Open intervals, enclosed in parenthesis, do not include its endpoints: x==(3,5) means x>3 & x<5

- Closed intervals, enclosed in square brackets, do include its endpoints: x==[3,5] means x>=3 & x<=5

- Half-open intervals do include the endpoint marked with square brackets:

x==(3,5] means x>3 & x<=5

x==[3,5) means x>=3 & x<5

katego identifies and includes intervals above the first one and below the last one stated in the instruction. If the creation of only two categories is intended, min or max can be provided in the instruction to indicate respectively the minimum or maximum values of varname in the dataset. Minimum and maximum values are always included in the interval regardless of the symbol used.

See examples below for details.

Examples

Open the example dataset

. sysuse auto

Describe the continuous variable to be categorized, in this case weight

. summarize weight, detail

Create a new variable called cw3 with three categories: the first one if weight is equal or less than 2500, the second one if weight is greater than 2500 and less that 3600, and the third one if weight is equal or greater than 3600

. katego weight cw3 (2500 3600)

Check the new variable against the original

. tabstat weight, statistics(count min max) by(cw3)

A verbose way to produce the same results of the previous example

. katego weight cw3 [min 2500] (2500 3600) [3600 max]

Create a variable cw4 with four categories of weight: 0 - less than 1990, 1 - equal or greater than 1990 and less than 2500, 2 - equal or greater than 2500 and equal or less than 3600, 3 - greater than 3600

. katego weight cw4 [1990 2500) [2500 3600]

Describe the created variable

. codebook cw4

Create cw2 with two categories of weight using 2000 as a cut point.

. katego weight cw2 [2000 max]

Same result as above

. katego weight cw2 [min 2000)

Same result as above, but without using katego

. generate cw2 = weight>=2000 & weight!=.

. label variable cw2 "Categories of weight"

. label define cw2 0 "< 2000" 1 ">= 2000"

. label values cw2 cw2

Author

Andres Gonzalez Rangel

MD, MSc Clinical Epidemiology

Instituto para la Evaluación de la Calidad y Atención en Salud - IECAS

Colombia

andres.gonzalez@iecas.org