*! version 1.1.0 19jul2026

Title

recode12 -- Standardize numeric 1/2 variables and corresponding two-category string variables as labeled 0/1 indicators

Syntax

recode12 [varlist], yesvalue(#) [suffix(name) replace display]

Description

recode12 standardizes eligible numeric variables coded 1/2 and eligible
two-category string variables as labeled 0/1 indicators. For string variables,
the first and second distinct nonmissing categories serve as the counterparts
of numeric source codes 1 and 2. Numeric and string variables may be processed
alone or together with the same command.

An eligible numeric variable contains both 1 and 2, may contain ordinary system
missing (.), and contains no other values. A variable containing only 1
plus ., only 2 plus ., an extended missing value (.a through
.z, including .m or .n), any other numeric value, or no
nonmissing observations is skipped.

An eligible string variable contains exactly two distinct nonmissing categories,
and both must be observed. For this eligibility check, recode12 treats an
empty string, a whitespace-only string, and the trimmed literal string "."
as missing. It scans observations in their current order, ignores these missing
representations and repeated categories, and treats the first distinct
nonmissing category encountered as source category 1 and the second as source
category 2. A string variable containing only one category plus missing values,
no nonmissing categories, or any additional distinct value, including a marker
such as m, n, .m, or .n, is skipped.

The string rule corresponds directly to the numeric rule. For numeric variables,
source categories 1 and 2 are the literal values 1 and 2. For string variables,
the first and second distinct nonmissing categories encountered are treated as the
counterparts of numeric source values 1 and 2. The same yesvalue() is then
applied without a separate string mapping: yesvalue(1) maps source
category 1 to 1 (Yes) and source category 2 to 0 (No);
yesvalue(2) maps source category 1 to 0 and source category 2 to 1.
Ordinary numeric missing values and the string missing representations described
above remain missing in the generated numeric result.

By default, the command creates a new byte variable and leaves the source
variable unchanged. If varlist is omitted, every variable in the dataset
is examined and only eligible variables are processed.

After recoding, the command verifies every converted observation against the
selected mapping and confirms that every nonmissing result is 0 or 1. It
reports success only after all verification checks pass.

By default, the Results window displays only the mapping rule and the
verification result. Detailed numeric and string-source counts and resulting
variable names are available with display and are always stored in
r().

Options

yesvalue(#) is required. The argument must be 1 or 2 and selects which
source category becomes 1 (Yes). It applies uniformly to every eligible
numeric and string variable in the command.

suffix(name) specifies the suffix for generated variables. The default is
suffix(_01). The resulting name must be a legal, unused Stata variable
name.

replace overwrites each eligible source variable with its recoded 0/1
values instead of creating a new variable. For eligible string variables this
changes the source variable from string to numeric while retaining its name.
replace may not be combined with suffix() and should be used only
when the original values are no longer needed.

display reports the number and resulting names of standardized numeric
and string-source variables. Names are printed in groups of no more than seven
per line. In replace mode, these are the retained source-variable names.

Remarks

The command determines eligibility from observed values, not from variable
names or substantive meaning. It does not attempt to decide whether a category
is favorable, unfavorable, affirmative, or negative.

For numeric variables, source categories 1 and 2 are the observed values 1 and
2. When those values have category labels, the category selected by
yesvalue() is stated in the generated variable label. If no category
label is available, the label states the numeric condition explicitly.

For string variables, source-category order is based on first occurrence among
trimmed nonmissing observations. For example, for the sequence ".",
Plum, blank, Plum, Peach, source category 1 is Plum and
source category 2 is Peach. The dot and blank remain missing. With
yesvalue(2), Plum is mapped to 0 and Peach to 1. With
yesvalue(1), the direction is reversed.

Because string-category order follows the current observation order, sorting
the dataset before running recode12 can change which string category is
source category 1. The selected category is stated in the generated variable
label.

Generated or replaced variables use the shared value label
recode12_NoYes, defining 0 as No and 1 as Yes. A generated
variable label begins with Recoded, identifies the category mapped to 1,
and states (0=No; 1=Yes).

Only after every post-recode check passes, the command creates or updates the
string variable recode12_status and fills every observation with
confirmed. A preexisting variable with this name is reused only when it was
previously created by recode12; otherwise the command stops. This status
confirms computational consistency, not the substantive suitability of the
user's chosen direction.

Examples

Load the supplied example dataset:

. use recode12_example_data.dta, clear

Process eligible numeric variables only:

. recode12 female employed, yesvalue(2)

Process eligible string variables only:

. recode12 exam_result_text preferred_fruit, yesvalue(2)

Process numeric and string variables together:

. recode12 female employed exam_result_text preferred_fruit, yesvalue(2)

Examine all variables and process every eligible numeric and string variable:

. recode12, yesvalue(2)

Overwrite eligible source variables:

. recode12 employed exam_result_text, yesvalue(2) replace

Stored results

recode12 stores the following in r():

r(n_recoded) number of variables recoded

r(yesvalue) source category mapped to 1 (Yes)

r(verified) 1 if at least one variable was recoded and all checks passed; otherwise 0

r(recoded) generated or replaced variables

r(source) all eligible source variables

r(numeric_source) eligible numeric source variables

r(string_source) eligible string source variables

r(numeric_recoded) generated or replaced numeric variables

r(string_recoded) generated or replaced string-source variables

r(n_numeric_recoded) number of numeric variables recoded

r(n_string_recoded) number of string-source variables recoded

r(skipped) examined variables not meeting the applicable rule

r(value_label) name of the attached value label

r(status_variable) name of the confirmation variable

Version history

1.1.0, 19 July 2026. Added eligible two-category string variables, mixed
numeric/string processing, first-occurrence string category ordering, explicit
string-missing rules, and type-specific stored results. The original numeric
1/2 eligibility and yesvalue() mapping rules remain unchanged.

1.0.0, 12 July 2026. Initial SSC release for eligible numeric variables
coded 1/2.

Author

Hao Ma

Email: shouhuoxiwang2027@gmail.com

Citation

If you use recode12 in published work, please cite:

Ma, Hao. 2026. recode12: A Stata command for standardizing numeric 1/2
variables and corresponding two-category string variables as labeled 0/1
indicators. Version 1.1.0.

License

recode12 is distributed under the MIT License. Copyright (c) 2026 Hao Ma.

Also see

Manual: [D] recode, [D] encode, [D] label