Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unbalance Panel Problems: "not sorted" Error when First Difference

    Hi all,

    recently I am working on threshold model for panel data. After I did some unit root tests (CIPS specifically), I proceed with baseline regression using first differenced variables having unit roots. However, I keep getting this "not sorted" error:
    Code:
    xtreg d.ln_smc d.rule_law inflation d.ln_gdp d.trade_open, fe
    not sorted
    r(5);
    However, I did recheck my panel data again but found no sorting problem. I even reran the sorting command to make sure the data are well sorted before proceeding further. Unfortunately, this error persists.
    Has anyone encountered this problem before? Please also find my ongoing codes attached and please kindly give me some suggestions on this matter.
    Code:
    clear all
    cls
    
    **# Retrieve Full Raw Data from World Bank
    wbopendata, country(BRA;CZE;GRC;HUN;MYS;MEX;ZAF;TWN;THA;TUR;CHL;CHN;COL;EGY;ISL;IND;IDN;KWT;PHL;QAT;ROU;SAU;ARE;VNM) indicator(CC.PER.RNK;FD.AST.PRVT.GD.ZS;NY.GDP.PCAP.KD;FP.CPI.TOTL.ZG;NY.GDP.DEFL.KD.ZG;CM.MKT.LCAP.GD.ZS;RQ.PER.RNK;RL.PER.RNK;NE.TRD.GNFS.ZS;SI.POV.GINI) full long clear year(2001:2024)
    
    **# Clean and Use Relevant Variables
    keep countrycode year ny_gdp_pcap_kd fp_cpi_totl_zg cm_mkt_lcap_gd_zs rl_per_rnk ne_trd_gnfs_zs
    
    **# Declaration
    egen country_id = group(countrycode)
    order country_id, before(countrycode)
    xtset country_id year
    
    **# Remove in-between missing values
    drop if country_id==6 & year>2017
    drop if country_id==12
    drop if country_id==13 & year>2014
    drop if country_id==17 & year<2009
    drop if country_id==18 & year<2012
    
    **# Rename Variables
    rename ny_gdp_pcap_kd      gdp_pc
    rename fp_cpi_totl_zg      inflation
    rename cm_mkt_lcap_gd_zs   smc
    rename rl_per_rnk          rule_law
    rename ne_trd_gnfs_zs      trade_open
    
    **# (Natural) logarithmic transformation
    gen ln_gdp = ln(gdp_pc)
    gen ln_infl = ln(inflation)
    gen ln_smc = ln(smc)
    gen ln_rl = ln(rule_law)
    gen ln_trade = ln(trade_open)
    
    **Mini Conclusion: ONLY use logarithmic transformed gdp_pc (ln_gdp) and smc (ln_smc)**
    **Mini Conclusion: Keep inflation as Levels. Difference everything else.
    
    sort countrycode year
    list countrycode year d.ln_smc d.rule_law inflation d.ln_gdp d.trade_open, sepby(countrycode)
    xtreg d.ln_smc d.rule_law inflation d.ln_gdp d.trade_open, fe
    I am looking forward to your views. Thank you all in advance.
    Last edited by Duc-Thai-Nam Nguyen; 26 Dec 2025, 01:09.

  • #2
    The problem with this command: sort countrycode year
    xtreg will return a `not sorted` error if the data is not sorted by the `panelvar` and `timevar` declared in `xtset`.
    After disabling the `sort` command, your code runs on my PC.
    Manh Hoang-Ba,
    Facebook,
    Eureka! Uni - YouTube,
    ManhHB94 (Manh Hoang Ba),
    Hoàng Bá Mạnh – Kinh tế lượng: Lý thuyết và ứng dụng

    Comment


    • #3
      Hi Manh,
      thank you very much for your notice! The code works well now. I put that sorting command because the initial code did not work (maybe I was running quite a few codes without resetting the environment).

      Comment

      Working...
      X