Stata中已设置format tq的year-quarter变量回归输出仍为数字,如何格式化?
i.yq Dummies I get it—you’ve formatted your year-quarter variable with format tq, it looks perfect in the data browser, but when you run a regression with i.yq, the output shows raw numeric codes instead of the human-readable 200XqX labels. Let’s fix that.
Why this happens
Stata stores date/time variables as underlying numbers (e.g., 2000q1 is stored as 160 for quarterly dates). When you use i.yq, Stata generates dummies based on these numeric values, and by default, it displays those numbers instead of the formatted date string unless you set up value labels for the variable.
Step-by-step solution
First, confirm your variable setup
Run these commands to double-check thatyqis indeed a quarterly date variable:describe yq list yq in 1/5You should see
storage type: float(or similar) anddisplay format: %tqin thedescribeoutput, and thelistshould show200XqXstrings.Create value labels for your
yqvariable
This maps each numeric code to its corresponding formatted date string. Run this loop:// Clear any existing value labels for yq (optional but safe) label drop yq_lab // Extract all unique values of yq levelsof yq, local(yq_vals) // Create a value label mapping each numeric code to the formatted date foreach val of local yq_vals { local date_label : di %tq `val' label define yq_lab `val' "`date_label'", add } // Attach the value label to the yq variable label values yq yq_labRe-run your regression
Now when you run your regression command, thei.yqdummies will show the200XqXlabels instead of raw numbers:reg your_depvar i.yq your_control_vars
Alternative: Use esttab for more control
If you prefer using esttab (a popular package for exporting regression results), you can skip the value label setup and directly format the dummy labels in the output. First install esttab if you haven’t:
ssc install esttab
Then run your regression, save the results, and output with formatted labels:
reg your_depvar i.yq your_control_vars estimates store reg_results esttab reg_results, label varlabels(`i.yq' @)
The @ in varlabels tells esttab to use the formatted display of each yq value as the dummy label.
内容的提问来源于stack exchange,提问作者Bryan Lwy

