ChatGPT for Stata: keep missing values out of a threshold flag

Give ChatGPT your missing-value policy along with the Stata expression. In Stata, numeric missing values compare greater than finite numbers, so generate above_limit = reading > 100 also assigns 1 to a missing reading. If an unknown reading must remain unclassified, generate the indicator only for nonmissing observations.

A threshold result needs three possible meanings here: below or equal to the limit, above it, and unknown. Turning the last group into either 0 or 1 changes the report's interpretation.

Write the classification rule before the expression

This original fictional fixture uses 100 as an arbitrary reporting threshold. It is not a clinical, safety or regulatory limit:

Record reading Expected above_limit
A 80 0
B 120 1
C . .
D .a .

The source distinguishes ordinary missing . from extended missing .a. The approved output rule leaves both unclassified. It retains the original reading column so that distinction remains available.

Stata's official logical-expressions and missing-values FAQ, checked October 24, 2026, explains the numeric ordering behind the problem. Extended numeric missings also compare above finite values. Checking only reading != . would therefore leave .a eligible for classification.

Restrict generation to observed readings

For a working dataset where reading is numeric and above_limit does not yet exist:

generate byte above_limit = reading > 100 if !missing(reading)
assert above_limit == 0 if reading == 80
assert above_limit == 1 if reading == 120
assert missing(above_limit) if missing(reading)
tabulate reading above_limit, missing

Observations excluded by the if qualifier receive a missing value in the newly generated variable. The intended fixture result is 0, 1, missing, missing in A through D order. These are authored expectations, not recorded output from a Stata session.

Compare that with generate byte above_limit = reading > 100 & !missing(reading). Its Boolean expression returns 0 for missing readings. That expression can suit a different policy, such as “known to be above the limit,” but it does not preserve an unknown category in this task.

Neither expression repairs an undocumented source code such as 999. If 999 means missing in your file, establish and apply that codebook rule first. The data-cleaning guide addresses source-value interpretation before derived calculations.

Ask for a row trace and the missing-value policy

Review this Stata indicator without modifying my dataset.
Stata version: [ENTER]
Variable name, storage type and meaning: [ENTER]
Exact current expression and observed output: [PASTE]
Threshold and comparison boundary: [ENTER]
Missing markers and their codebook meanings: [PASTE]

For this fixture, numeric reading values are 80, 120, . and .a.
The threshold is strictly greater than 100. Expected flag values are
0, 1, missing, missing. Unknown readings must remain unclassified.
Explain Stata's numeric missing ordering and trace every row.
Propose generation syntax and assertions preserving the raw variable.
Distinguish an if qualifier from placing a nonmissing test inside
the Boolean expression. Do not recode unknowns as below threshold,
invent source-code meanings, overwrite columns or claim execution.

Use the actual variable name, threshold and permitted sample in your request. A generated do-file should match the policy you supplied, including what happens exactly at the threshold.

Run the checks in a working copy and inspect the missing-inclusive tabulation. Keep the source missing codes, the classification rule and accepted do-file together. A subsequent percentage must also state whether unknown observations belong in its denominator; a correct flag alone does not settle that decision.

Keep reading