help digit


digit -- Extracts digits at a specified decimal place


egen [type] newvar = digit(varname) [, digit(#) round(#) ]


The digit function extends the egen command. When used as described above, a new user-named variable newvar is created. For each case, newvar contains the digit (0 to 9) that is located at a specified decimal position in varname.

Options are available to specify the decimal position that you wish to examine as well as any rounding that should be applied to the variable when extracting this digit.


+---------+ ----+ General +----------------------------------------------------------

Digit(#) indicates the digit you wish to extract. This is specified using the decimal exponent or power of 10 of the desired digit (e.g. digit(0) extracts the 'units' digit, digit(1) extracts the 'tens' digit, and digit (-2) extracts the 'hundredths' digit). The detault setting is zero (extraction of the 'units' digit).

Round(#) indicates the number of decimal places to which varname will be rounded prior to extracting digit information. When using the command repetitively to extract a series of digits, this option should be constant to prevent inconsistencies due to different rounding schemes being applied. The default setting is 15 decimal places, however in some cases this may result in rounding errors where the number being stored exceeds the available precision. This is often the case where floating point values have been imported from other data formats using third-party data conversion programs (e.g. a number may have been stored as 4.9999999999997 instead of 5).


This function creates a new variable containing the digit stored at a certain decimal position in an existing variable. This new variable can be tabulated to investigate digit preferences.

The following example illustrates how this function can be used:

. egen hundredths = digit(potass), digit(-2) round(2) . tab hundredths ------------------------------------------------ 10^-2 digit | of potass | Freq. Percent Cum. ------------+----------------------------------- 0 | 13,468 93.72 93.72 1 | 84 0.58 94.31 2 | 114 0.79 95.10 3 | 105 0.73 95.83 4 | 96 0.67 96.50 5 | 109 0.76 97.26 6 | 100 0.70 97.95 7 | 101 0.70 98.66 8 | 97 0.68 99.33 9 | 96 0.67 100.00 ------------------------------------------------

The tabluation of the created variable shows an overrepresentation of zero digits in the 'hundredths' position. This suggests around 6% of scores were measured with a higher precision may have been misrecorded or mistranscribed resulting in an incorrect precision.


. egen units = digit(myvar) . egen dm2 = digit(potass), digit(-2) round(2) . egen dm1 = digit(potass), digit(-1) round(2) . egen d0 = digit(potass), digit(0) round(2)


Richard J. Atkins London School of Hygiene and Tropical Medicine e-mail: