You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Stata中已设置format tq的year-quarter变量回归输出仍为数字,如何格式化?

Fixing Stata Regression Output to Show Date Labels for 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

  1. First, confirm your variable setup
    Run these commands to double-check that yq is indeed a quarterly date variable:

    describe yq
    list yq in 1/5
    

    You should see storage type: float (or similar) and display format: %tq in the describe output, and the list should show 200XqX strings.

  2. Create value labels for your yq variable
    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_lab
    
  3. Re-run your regression
    Now when you run your regression command, the i.yq dummies will show the 200XqX labels 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:16:06