Stata Error r(198): Every Cause of 'Invalid Syntax' and How to Fix It
r(198) is Stata's most cryptic error. Here are the 15 causes — from invisible characters to smart quotes — and the one-line fix for each.
You’ve been staring at this line of code for five minutes. It looks perfect. You’ve checked every parenthesis, every comma, every variable name. You run it again.
invalid syntax r(198);
Error r(198) means Stata cannot parse your command. The syntax is invalid — but the reason why is often invisible. This guide covers every documented cause of r(198) and shows you exactly how to fix each one.
All examples tested in Stata 18 SE. Compatible with Stata 15+.
Quick Answer
Error r(198) means “invalid syntax.” The most common causes are:
- Invisible characters from copy-pasting code (PDFs, Word, websites)
- Smart quotes (
“ ”) instead of straight quotes (" ") - Missing comma before options
- Wrong macro delimiters
- Tab characters or byte-order marks in your .do file
The fastest fix: retype the offending line by hand in the Stata command window. If the retyped version works, invisible characters were the problem.
Cause 1: Invisible Characters from Copy-Paste
This is the single most common cause of r(198), and the hardest to diagnose because the problem is literally invisible. When you copy code from a PDF, a Word document, a website, an email, or even a Slack message, the text often carries hidden characters: zero-width spaces (U+200B), non-breaking spaces (U+00A0), zero-width joiners, or other Unicode control characters.
These characters look like normal spaces or are completely invisible, but Stata’s parser chokes on them.
1* This line looks identical to valid Stata code2* but contains a zero-width space after "regress"3regress wage education experience, robustinvalid syntax r(198);
How to diagnose
Open your .do file in a text editor that shows invisible characters. In VS Code, turn on “Render Whitespace: All” (editor.renderWhitespace). In Sublime Text, use View → Indentation → Convert to Spaces to reveal non-standard whitespace.
The fix
Retype the line by hand. Don’t copy it — type every character from scratch in the Stata command window or do-file editor. If the retyped version works, you know invisible characters were the problem.
Cause 2: Smart Quotes Instead of Straight Quotes
Word processors, Google Docs, and many email clients automatically convert straight quotes (") into “smart” or “curly” quotes. Stata only accepts straight quotes.
* Smart/curly quotes — WRONGgen highwage = (wage > 50000)label variable highwage “High wage indicator”* Straight quotes — CORRECTgen highwage = (wage > 50000)label variable highwage "High wage indicator"The difference is nearly impossible to see in most fonts. The left example uses Unicode characters U+201C and U+201D (left and right double quotation marks). Stata needs U+0022 (the plain ASCII double quote).
Cause 3: Missing Comma Before Options
In Stata, options come after a comma. Forget the comma and the option becomes part of the variable list, causing a syntax error.
regress wage education experience robustregress wage education experience, robustvariable robust not found r(111);
Notice that forgetting the comma sometimes produces r(111) instead of r(198), because Stata interprets the option name as a variable. But with certain options or command structures, you get r(198) directly.
1* All of these need a comma before the option2regress wage education, vce(cluster state_id) "stata-comment">// correct3regress wage education vce(cluster state_id) "stata-comment">// r(198)45merge 1:1 id using data2, nogenerate "stata-comment">// correct6merge 1:1 id using data2 nogenerate "stata-comment">// r(198)78graph twoway scatter wage education, title("Wages") "stata-comment">// correct9graph twoway scatter wage education title("Wages") "stata-comment">// r(198)help regress. Everything listed after the comma in the syntax diagram is an option.Cause 4: Wrong Macro Delimiters
Stata uses a unique syntax for local macros: a left single quote (backtick) to open and a right single quote (apostrophe) to close. Getting either one wrong produces r(198).
local controls "education experience tenure"* Using regular quotes — WRONGregress wage 'controls', robust* Using two apostrophes — WRONGregress wage 'controls', robustlocal controls "education experience tenure"* Backtick + apostrophe — CORRECTregress wage `controls', robustThe opening delimiter is the backtick character (U+0060), typically on the key left of the 1 key on US keyboards. The closing delimiter is the standard apostrophe (U+0027). Using two apostrophes or two backticks both fail.
Cause 5: Incorrect #delimit Usage
The #delimit ; command changes Stata’s command delimiter from carriage return to semicolon. If you forget to reset it, or if your .do file mixes delimited and non-delimited sections, you’ll get r(198) on lines that look perfectly valid.
1#delimit ;2regress wage3 education4 experience5 tenure,6 robust ;78* Forgot to reset delimit — now ALL subsequent commands need a ;9summarize wage10// ^ This now produces r(198) because Stata is waiting for a ;1112#delimit cr "stata-comment">// Reset back to carriage return13summarize wage "stata-comment">// Now this works#delimit ; entirely. Use /// for line continuation instead — it’s less error-prone and doesn’t change state.1* Use /// for multi-line commands — much safer2regress wage "stata-comment">///3 education "stata-comment">///4 experience "stata-comment">///5 tenure, "stata-comment">///6 robustCause 6: Line Continuation (///) Errors
The /// continuation marker tells Stata that the command continues on the next line. But there are two common mistakes:
1* ERROR 1: No space before ///2regress wage education"stata-comment">/// // r(198) — needs space before ///3regress wage education "stata-comment">/// // correct45* ERROR 2: Characters after ///6regress wage education "stata-comment">/// these are not comments7regress wage education "stata-comment">/// // this is also wrong8regress wage education "stata-comment">/// // this IS a valid comment (space between)Stata requires at least one space before ///. Anything after /// on the same line is treated as a comment, but only if there’s a space. Without the space, Stata tries to parse it as part of the command.
Cause 7: if vs in Confusion
Stata’s if qualifier (for subsetting) uses a different syntax than the programmingif command. Mixing them up causes r(198).
* Trying to use programming if syntaxif gender == "female" { summarize wage}* This works but is DIFFERENT from:summarize wage if gender == "female"* The if qualifier — what you usually wantsummarize wage if gender == "female"* The programming if — for flow controlif _N > 100 { display "Large sample"}The if qualifier goes after the command and variable list. The programming ifgoes at the start of a line and requires curly braces. Using the wrong one in the wrong place gives r(198).
Cause 8: Parentheses in Option Syntax
Some options use parentheses, some don’t. Getting the parentheses wrong — or using brackets instead — causes r(198).
* Square brackets — WRONGregress wage education, vce[cluster state_id]* Missing inner parentheses — WRONGregress wage education, vce(cluster)* Extra parentheses — WRONGregress wage education, (robust)* Correct: parentheses with full specificationregress wage education, vce(cluster state_id)* Correct: simple option without parensregress wage education, robust* Correct: option with proper parensregress wage education, absorb(firm_id)Cause 9: Version-Specific Syntax
Commands introduced in newer Stata versions produce r(198) if you run them on an older version. The most common cases:
1* frames — requires Stata 16+2frame create myframe "stata-comment">// r(198) on Stata 1534* collect/table — requires Stata 17+5collect create mytable "stata-comment">// r(198) on Stata 1667* dtable — requires Stata 18+8dtable wage education "stata-comment">// r(198) on Stata 17910* Check your version:11versionversion 18.0
version statement at the top of your .do file:version 18. This documents which Stata version the code requires and prevents misleading errors on older versions.Cause 10: Byte-Order Marks (BOM) in .do Files
Some text editors (notably Notepad on Windows) save files with a byte-order mark — invisible bytes at the very beginning of the file. Stata cannot parse the BOM and throws r(198) on the first line of your .do file.
If r(198) occurs on the very first command in your .do file, and that command works fine when typed directly in the command window, BOM is likely the culprit.
The fix
In VS Code: click the encoding indicator in the bottom status bar → select “Save with Encoding” → choose “UTF-8” (without BOM). In Notepad++: Encoding → Convert to UTF-8 (not UTF-8 BOM).
Cause 11: Tab Characters
While Stata generally handles tabs, they can cause r(198) in specific contexts — particularly inside strings, in #delimit mode, or when mixed with continuation lines.
label variable wage "Hourly wage"* The gap above is a tab, not spaceslabel variable wage "Hourly wage"* Use spaces consistently in .do files"editor.insertSpaces": true. This prevents tab-related issues entirely.Cause 12: Unicode Issues from Non-English Systems
If your operating system locale uses non-ASCII characters (Chinese, Arabic, Cyrillic), file paths and variable names containing these characters can trigger r(198). Stata’s Unicode support improved in version 14+, but edge cases remain.
1* This may fail depending on your system encoding2use "/Users/données/analyse.dta"34* Safer: use ASCII-only paths5use "/Users/data/analysis.dta"Cause 13: Mismatched or Nested Quotes
Stata uses compound double quotes (“ ” as `" "’) for strings containing quotes. Getting these wrong causes r(198).
* Nested regular quotes — WRONGlocal mystring "She said "hello" today"* Unmatched quote — WRONGdisplay "This is unfinished* Compound quotes for nestinglocal mystring `"She said "hello" today"'* Properly closeddisplay "This is finished"Cause 14: Factor Variable Notation Errors
Factor variable prefixes (i., c., ibn.) have strict syntax rules. Common mistakes:
1* Space between prefix and variable — WRONG2regress wage i. industry, robust "stata-comment">// r(198)34* Correct — no space5regress wage i.industry, robust67* Wrong prefix format — WRONG8regress wage i(industry), robust "stata-comment">// r(198)910* Interaction with wrong operator — WRONG11regress wage education*experience "stata-comment">// r(198)1213* Correct interaction notation14regress wage c.education#c.experienceCause 15: Commands Run as Prefix (or Vice Versa)
Some Stata keywords are command prefixes, not standalone commands. Running them alone — or running a standalone command as a prefix — triggers r(198).
* by is a prefix, not a commandby state_id, sort: wage* quietly needs a command after itquietly* Correct prefix usagebysort state_id: summarize wage* quietly modifies a commandquietly regress wage educationSytra catches these errors before you run.
Sytra parses your Stata code before execution and catches syntax errors — including invisible characters, wrong quote types, and missing commas — before they reach Stata. Describe your analysis in plain English and get valid, runnable code.
Join the Waitlist →Debugging Checklist
When you hit r(198), work through this list in order:
- Retype the line by hand. If it works when retyped, invisible characters were the problem.
- Check for smart quotes. Search your .do file for
“and”and replace with". - Verify the comma. Make sure options are separated from the variable list by a comma.
- Check macro delimiters. Local macros use backtick-apostrophe:
`var’. - Check
#delimitstate. If using#delimit ;, make sure every command ends with;. - Check your Stata version. Run
versionand verify the command exists in your version. - Run one line at a time. Paste each line individually into the command window to find the exact offending line.
FAQ
What does r(198) mean in Stata?
Error r(198) means Stata could not parse your command. The syntax is invalid — either a typo, invisible character, wrong delimiter, or misplaced option. It is not a data error — your dataset is unaffected.
How do I fix invalid syntax in Stata?
First identify the exact line causing the error by running your do-file line by line. The most common fixes are: replace smart quotes with straight quotes, add a comma before options, fix macro delimiters (backtick + apostrophe), and remove invisible characters by retyping the line.
Why does Stata say invalid syntax when my code looks correct?
Invisible characters are the most common cause. Code copied from PDFs, Word documents, or websites often contains zero-width spaces, non-breaking spaces, or smart quotes that look identical to valid characters but Stata cannot parse. Retype the line by hand to test.
Does r(198) mean my data is corrupted?
No. Error r(198) is purely a syntax error in your command. Your dataset is completely fine. The problem is in how the command is written, not in the data it operates on.
We build practical, reproducible workflows for Stata and R teams working on real empirical research pipelines.