Announcement

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

  • Output from looping over a variable partially getting exported when using putexcell

    Hello everyone,

    Please can you help me. I have a dataset that is ordered by year and bank. The bank variable has 20 banks observed over 4 years. I would like to obtain some descriptive statistics and export the tables to excel for my variables such as default_rate and credit_score . That is the (n mean sd min max) by year and bank. However, my amateur's commands seem to be horribly wrong. My main worry is that, when i export the results using _putexcel the output I get is just for the last bank repeated 20 times and not the table results for each bank. How can I fix this? An example code of what I am doing using webuse data is as follows?

    Code:
    webuse productivity, clear
    
    drop if year > 1976 
    
    * Create an empty matrix to store results
    matrix result = J(1, 5, .)
    
    *Loop through each state and calculate summary statistics
    forval i = 1/48 {
        bysort state year: tabstat unemp gsp , statistics(n mean sd min max) save
            mat mat`i' = r(StatTotal)'
        
        *Append the results to the main matrix
        matrix result = result \ mat`i'
    }
    
    
    matrix colnames result = "n" "mean" "sd" "min" "max"
    
    *Export the results to Excel using putexcel
    
    putexcel set "path\to\my\output_file.xlsx", modify sheet(Sheet1)
    putexcel A1 = matrix(result)

  • #2
    You accidentally posted in the Mata Forum, which is a forum dedicated to Stata's matrix programming language. Please repost in the General Forum.

    Comment


    • #3
      Ohh Thank you very much. Let me repost in the correct forum then.

      Comment

      Working...
      X