help digit-------------------------------------------------------------------------------

Title

digit-- Extracts digits at a specified decimal place

Syntax

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

DescriptionThe

digitfunction extends theegencommand. When used as described above, a new user-named variablenewvaris created. For each case,newvarcontains the digit (0 to 9) that is located at a specified decimal position invarname.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.

Options+---------+ ----+ 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 whichvarnamewill 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).

RemarksThis 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.

Examples

. 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)

AuthorRichard J. Atkins London School of Hygiene and Tropical Medicine e-mail: richard.atkins@lshtm.ac.uk