Debugging Stata r(198) and r(111) Errors in Data Analysis
A diagnostic guide to resolving Stata r(198) syntax errors and r(111) variable not found errors, featuring debugging steps for macro expansion and comma delimiters.
29 May 2026, 13:59 UTC

The Problem: Syntax and Reference Failures
In Stata, r(198) and r(111) are the most frequent roadblocks during data cleaning and regression. An r(198) error is a syntax failure, meaning Stata cannot parse the command you wrote. An r(111) error is a reference failure, meaning the command is syntactically correct, but the variable you are calling does not exist in the current memory workspace.
Diagnostic Quick-Reference
| Error Code | Common Cause | Primary Diagnostic Command |
|---|---|---|
| r(198) | Mismatched parentheses, misplaced commas, or illegal characters. | help [command] |
| r(111) | Case-sensitivity mismatch or empty macro expansion. | describe |
Resolving r(198): Syntax Errors
Stata expects a strict sequence: command variables [if] [in], options. If you place a variable after the comma or forget to close a parenthesis, Stata triggers r(198).
Step 1: Check the Comma Delimiter
All options must follow a single comma. If you include a comma in the middle of your variable list or omit it before options, the parser fails.
Incorrect: regress price mpg, robust weight (Weight is a variable, not an option).
Correct: regress price mpg weight, robust
Step 2: Validate Parentheses and Quotes
Check for unmatched brackets or quotes, especially when using complex generate or replace logic. A missing closing parenthesis in a mathematical expression is a primary cause of r(198).
Step 3: Isolate via Minimal Reproducible Example (MRE)
If the error persists, test the syntax using a built-in dataset to determine if the issue is your code or your specific data. Run this in the Command window:
sysuse auto, clear
regress price mpg, robust
If this works, the syntax is correct, and the error in your main script likely stems from a hidden character or a variable name that Stata considers illegal.
Resolving r(111): Variable Not Found
An r(111) error occurs when Stata looks for a column name in the active dataset and finds nothing. Because Stata is case-sensitive, Income and income are different variables.
Step 1: Verify Workspace State
Run the describe command to see exactly what variables are currently loaded in memory. Check for:
- Casing: Does the variable start with a capital letter?
- Existence: Did a previous
droporkeepcommand accidentally remove the variable? - Loading: Did you forget to run the
usecommand to load the .dta file?
Step 2: Debug Macro Expansions
If you are using loops with local or global macros, r(111) often means the macro is empty. When Stata evaluates regress y `myvar' and myvar is empty, it sees regress y , which triggers a variable not found error.
Diagnostic Check: Use the display command immediately before the failing line to see what the macro contains.
local myvar "mpg"
display "The current variable is: `myvar'"
regress price `myvar'
If the display output is The current variable is: , your macro definition failed or was defined in a different execution block.
Comparison: Syntax vs. Reference
To distinguish between these two errors during a complex script, observe where execution stops:
- Immediate Stop: If Stata stops the moment it hits the line without even attempting to process the data, it is likely r(198).
- Processing Stop: If Stata begins the operation (e.g., starts calculating a regression) but stops when it reaches a specific variable, it is r(111).
Limitations and Verification
These diagnostics assume you are using a standard Stata installation (Version 14 or newer). Note that very large datasets may occasionally throw memory-related errors that look like syntax failures if the system cannot allocate space for a new variable during a gen command.
Verification: To confirm the fix, run the command in the Command window. If it executes without an error code, the issue is resolved. If you modified the dataset using drop or keep during debugging, remember to reload your original .dta file to restore the full dataset.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.