Announcement

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

  • use a matrix to save results of looping

    Dear Statalisters,
    I am trying to use a matrix to save results of looping over countries (panel data) to obtain lag-order selection statistics for VARs and VECMs and feed in the optimal lag into the matrix. However, I keep receiving this error message after running the procedure for the first country:
    “new variables cannot be uniquely named or already defined, after running”

    I am not able to figure out what is wrong after several attempts. Here is the part of code I am using to loop over countries:


    Code:
    * Identify the unique countries in your dataset
    levelsof country, local(country_list)
    
    * Initialize an empty matrix to store results
    local ncount : word count `country_list'
    gen Country = "`country_list'"
    matrix P = J(`ncount',1,.)
    
    
    local i = 1
    * Loop over each country
    foreach country of local country_list {
        preserve
        * Filter data for the current country
        keep if country == "`country'"
        di "Processing country:`country'"
        varsoc rI rC rY  
        matrix A = r(stats)
        svmat A, name(col)
    egen minh = min(H)
    gen optimal_lag = lag if minh == HQ
    sum optimal_lag if optimal_lag<.
    local olag = r(mean) 
    matrix P[`i',1] = `olag'
    matrix drop A
    drop lag LL LR df p FPE AIC HQIC SBIC minh optimal_lag
    
       local ++i
    }
    matrix list P, format(%2.0f)
    Any help will be kindly appreciated.

    Imed
Working...
X