Tutorials › Population Pharmacokinetics › Code Categorical Covariates and Unknown Categories

Dataset Construction and Quality Control · Tutorial 45 of 599

Code Categorical Covariates and Unknown Categories

Build reproducible categorical covariates by documenting source-code mappings and distinguishing valid levels from unknown, unrecorded, or unresolved values.

Intermediate 9 min read

What You'll Learn

  • Define a consistent set of labels for each categorical covariate.
  • Map source codes to labels without treating arbitrary numeric codes as measurements.
  • Distinguish valid categories from unknown, unrecorded, and unresolved values.
  • Document source values, standardized labels, and mapping rules.
  • Detect unmapped codes and avoid silently assigning them to a valid category.
  • Check that category harmonization preserves distinctions important to interpretation.

Why Categorical Covariate Coding Needs a Rule

A categorical covariate is recorded as one of a set of labels or groups, such as administration route or disease-severity class. In a population pharmacokinetic dataset, the same category may arrive in different forms: one source might use “oral,” another “PO,” and a third a numeric code. Conversely, the same-looking code can mean different things in different source files. A reliable analysis dataset needs an explicit rule for translating source values into a stable set of labels.

That translation must also keep an important distinction intact: a valid category is not the same as a value that is unknown or was not recorded. If an unrecorded value is turned into a valid category, or an unfamiliar code is silently assigned to a familiar category, the analysis dataset no longer represents what the source actually says.

Definition: A categorical covariate assigns an individual or record to one of a finite set of groups. Category harmonization is the documented mapping of source values to consistent analysis labels. A source value is unknown or unrecorded when the source does not establish which valid category applies; it is not a valid category merely because it has a code or text label.

A numeric source code does not necessarily carry numeric meaning. For example, if 1 denotes “oral” and 2 denotes “intravenous,” those numbers are identifiers, not measurements: the difference \(2-1\) has no clinical interpretation. The resulting covariate should be understood through its labels and documented mapping, not by arithmetic on the codes.

Specify Categories and Unknown-Value Handling

Before mapping records, define the categories that the analysis dataset is intended to represent. Use labels that are clear, stable, and no more specific than the source supports. If a source distinguishes categories that matter to interpretation, do not merge them merely to make a shorter list. If distinct source codes are confirmed to mean the same category, they may map to one label, but that many-to-one decision should be documented.

Unknown and unrecorded values require separate handling from valid categories. Depending on source conventions, these may include a blank field, a designated code such as 9, or text such as “not known.” A code like 9 is not automatically an unknown marker: consult the data dictionary or source documentation. Similarly, “other” may be a genuine, recorded category, while “unknown” indicates that the applicable category has not been established. The labels must be interpreted from their documented meanings, not from their spelling alone.

A useful data structure retains at least the original source value, a standardized category label when one is supported, and a status or reason when it is not. That structure allows reviewers to distinguish a known category from an unknown value and to trace the analysis value back to its source. Preserve the raw value rather than replacing it with the harmonized label.

Key rule: Assign a standardized category only when the source value has a documented mapping to that category. Keep unknown or unrecorded values distinct from valid levels, and flag values that cannot be mapped rather than guessing.

Worked Example: Mapping Administration-Route Codes

Suppose a hypothetical study has two source files describing administration route. The source dictionary for File A defines “PO” as oral and “IV” as intravenous. File B uses “oral” and “intravenous.” It also contains “O,” for which the available documentation gives no definition. Construct a consistent analysis value.

Source fileRaw valueDocumented meaningStandardized value or status
APOOralOral
AIVIntravenousIntravenous
BoralOralOral
BintravenousIntravenousIntravenous
BONot documentedUnresolved; investigate

The documented values map to the two common labels “Oral” and “Intravenous.” The “O” does not map to “Oral” simply because its first letter matches: that would be an unsupported inference. Keep the raw value “O,” flag it as unresolved, and seek a source definition. If no definition can be obtained, it cannot be assigned to either valid category on the evidence available.

The mapping rule is therefore explicit: File A’s “PO” and File B’s “oral” map to “Oral”; File A’s “IV” and File B’s “intravenous” map to “Intravenous”; other values are flagged for review rather than defaulted. Applying this same rule to every record makes the category labels consistent without inventing information.

Unknown, Unrecorded, and Other Are Not Interchangeable

Several source values may look like “extra categories” but describe different data states. “Not recorded” indicates that the field has no recorded value. “Unknown” indicates that the source does not establish which category applies. “Not applicable” means the field does not apply in that record, if that meaning is supported by the data definition. “Other” can be a valid category when the source explicitly records a value outside the named categories. These distinctions should be retained when the source provides them; do not collapse them into one label just to simplify a table.

The practical aim here is accurate coding, not deciding how to handle these values in a later analysis. Keep enough information to see which records have a known category, which have a documented non-category status, and which contain a value that has not yet been interpreted. Decisions about the causes of missing values or any later treatment of them belong to separate steps.

Worked Example: Separating Valid Levels From Unknown Codes

A hypothetical source records baseline smoking status using codes 1, 2, 3, and 9. Its data dictionary says 1 means “never,” 2 means “former,” 3 means “current,” and 9 means “not recorded.” A sample of six records contains the codes 1, 3, 9, 2, 3, and 9. Create standardized values and summarize the resulting counts.

Apply the dictionary exactly: 1 maps to “Never,” 2 to “Former,” 3 to “Current,” and 9 to a “Not recorded” status. The six records therefore become “Never,” “Current,” “Not recorded,” “Former,” “Current,” and “Not recorded.”

Counting those mapped values gives Never: 1 record; Former: 1 record; Current: 2 records; Not recorded: 2 records. The three valid smoking categories have a total of \(1+1+2=4\) records. The other two records are not a fourth smoking category: the dictionary defines code 9 as an absence of recorded status. Keeping that distinction visible prevents a category count from implying that “not recorded” is a smoking state.

If the source dictionary had not defined code 9, the correct first step would be to flag it for investigation. Its numeric form alone would not establish whether it represented “not recorded,” a valid category, or something else.

Why Explicit Mapping Preserves Meaning

A mapping is safe when each source value is handled by a documented rule that preserves the distinctions the dataset is meant to retain. The justification is straightforward: each record starts with a source value; the rule either assigns it to a supported analysis label or marks it as unresolved or not recorded. If the rule is applied consistently, identical source values with the same documented meaning receive the same result. If two source values are merged, the documentation states why that merge is appropriate.

$$ \text{source value} \longrightarrow \begin{cases} \text{documented standardized category}, & \text{if its meaning is established},\\ \text{unknown or unrecorded status}, & \text{if the source defines that status},\\ \text{unresolved flag}, & \text{if no supported mapping is available}. \end{cases} $$

This rule also explains why silently converting every unexpected value to a default category is unsafe. If “O” is automatically assigned to “Oral,” two distinct possibilities—an abbreviation for oral and a typo or unrelated code—are treated as identical without evidence. A review flag preserves the uncertainty so it can be resolved from an authoritative source.

Worked Example: Handling a New Code Without Guessing

A hypothetical route field has already been harmonized using the documented values “PO” → “Oral” and “IV” → “Intravenous.” A later source extract contains “SC” in three records. The current mapping does not include “SC,” and no source definition has yet been checked. Decide what the data-processing rule should do.

“SC” must not be assigned to “Oral,” “Intravenous,” or a newly invented valid category based only on its appearance. The mapping table should return an unresolved status for each of the three records while preserving “SC” as the raw value. The quality-control report should show that three values need review, and the data owner or source documentation should be consulted.

If an authoritative definition confirms that “SC” means subcutaneous, add a documented mapping to the category “Subcutaneous” if that category is part of the intended analysis labels. Re-run the rule, then confirm that all three records receive that value and that no other previously mapped values changed unexpectedly. If the source meaning remains unknown, retain the unresolved status. The number of affected records does not make an unsupported mapping more reliable.

Validation, Alternatives, and Common Pitfalls

After mapping, check both the values and the process that produced them. At minimum, compare raw values against the mapping table, count records by standardized category and status, and list any unmapped values. Confirm that source-specific codes were interpreted using the correct source definitions. When the same covariate appears in multiple files, compare their dictionaries rather than assuming that a shared code has a shared meaning.

  • Check completeness: every raw value has either a documented mapping, a documented unknown or unrecorded status, or an unresolved flag.
  • Check consistency: the same documented source value and context receive the same standardized result.
  • Check category preservation: distinct source categories remain distinct unless an explicit, justified rule merges them.
  • Check counts: the total number of records before and after mapping agrees, including records assigned an unknown, unrecorded, or unresolved status.
  • Check traceability: retain the raw value, the standardized result, and the mapping-rule version or documentation needed to reproduce the result.

Two broad approaches are common. A documented mapping table is the preferred approach for repeatable processing: it makes each conversion reviewable and can expose values missing from the rule. Ad hoc recoding in analysis code may be quick for a small, one-off check, but it is easier to apply inconsistently or to lose the original value. Neither approach justifies assigning an unfamiliar value by guesswork; the difference is whether the rule is visible and auditable.

SituationAppropriate handlingRisky alternative
Two documented labels mean the same categoryMap to one standard label and document the equivalenceMerge without checking definitions
A code is defined as not recordedRetain that status separately from valid categoriesCount it as a substantive category
A raw value is absent from the mapping tableFlag it for review and preserve the raw valueDefault it to the most common category
A source says “other” and defines it as a valid responseRetain “Other” as a category if it is in scopeRelabel it “Unknown” without evidence

A frequent mistake is to assume that a special numeric code must be an ordinary category because it appears in the same field as other codes. The correction is to consult the source definition and represent the meaning, not just the storage format. Another mistake is to treat every unknown value as “Other.” The correction is to distinguish a known “other” response from a value whose category was not established.

Categorical coding also has limits. A standardized label cannot recover information that the source never captured, and a mapping cannot resolve an ambiguous source definition by itself. Overly broad labels may hide meaningful distinctions; overly narrow labels may imply precision the data do not support. The mapping should therefore be reviewable, aligned with the source documentation, and limited to the distinctions the available evidence supports.

Key takeaway: A categorical covariate is made reliable by a documented, repeatable mapping. Keep valid categories distinct from unknown or unrecorded values, preserve the raw source value, and flag anything that cannot be mapped with evidence.

Check Your Understanding

Answer each question, then compare your reasoning with the solutions.

  1. A source uses codes A and B for two documented administration routes and code Z for “not recorded.” Should Z be included as a third valid route category? Explain.
  2. Two source files use “PO” and “oral” for the same documented category. What should be retained, and what standardized label could be assigned?
  3. A new value appears in a categorical field, but the source documentation does not define it. What should the mapping process do?
  4. A dictionary defines “Other” as a recorded response for a category not represented by the listed options. Is “Other” equivalent to “Unknown”? Explain.

Solutions. (1) No. If Z is documented as “not recorded,” retain that status separately from valid route categories; it does not identify a route. (2) Preserve each raw value and map both to the same standardized label, such as “Oral,” because their equivalence is documented. (3) Preserve the raw value, flag it as unresolved, and seek an authoritative definition; do not assign it to a familiar category by guesswork. (4) No. “Other” is a recorded category under that definition, while “Unknown” means the applicable category has not been established. Keep them distinct.

Clinical Biostats