Announcement

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

  • -putexcel- command not exporting results to excel file

    I have written codes to export regression model results to excel file, and the codes are:
    Code:
    version 14.0
    putexcel set oddsratio.xlsx, replace sheet(class1)
    
    capture drop j
    gen j = 1
    
    forvalues i = 1/20 {
        logistic sds gst if complete == 1 & _`i'_cmembership == 1
        putexcel A`j' = etable using oddsratio.xlsx, sheet(class1) modify
        replace j = j + 5
    }
    The idea is to run regression model on 20 different sub-sample (defined by the value of variable:_`i'_cmembership). Each set of result table takes up 3 lines, so I specified "j = j+5" to separate each set of table by two blank rows in the excel file. I did not get any error messages after running the codes, but each time I ended up with an empty excel sheet after multiple tries. I tried to trouble shoot, and found out if I typed the codes:
    Code:
    version 14.0
    putexcel set oddsratio.xlsx, replace sheet(class1)
    
    logistic sds gst if complete == 1 & _1_cmembership == 1
    putexcel A1 = etable using oddsratio.xlsx, sheet(class1) modify
    I can have results exported in excel. But when adding above codes into -forvalues- loop, no results were written in excel file. I could not figure out what is wrong with my codes. Would appreciate your help and any inputs.

    Thank you in advance.

    Best,
    Mengmeng

  • #2
    What happens when you run:
    putexcel A1 = etable using oddsratio.xlsx, sheet(class1) modify putexcel A6 = etable using oddsratio.xlsx, sheet(class1) modify But your problem is that you've generated a variable named j when you need a local macro named j. So change gen j=1 to local j =1 and replace=j=j+5 to local j=`j' + 5

    Comment


    • #3
      Hi Phil,

      Thanks for your inputs. Tried your two suggestions:
      1) ran -putexcel- command separately, rather than running it in the loop, codes applied:
      Code:
      version 14.0
      putexcel set oddsratio.xlsx, replace sheet(class1)
      logistic sds gst if complete == 1 & _1_cmembership == 1
      putexcel A1 = etable using oddsratio.xlsx, sheet(class1) modify
      logistic sds gst if complete == 1 & _2_cmembership == 1
      putexcel A6 = etable using oddsratio.xlsx, sheet(class1) modify
      Nothing was output in the excel file.

      2) changed generating a variable j to define a local j, codes applied:
      Code:
      version 14.0
      putexcel set oddsratio.xlsx, replace sheet(class1)
      
      capture local drop j
      local j = 1
      
      forvalues i = 1/20 {
          logistic sds gst if complete == 1 & _`i'_cmembership == 1
          putexcel A`j' = etable using oddsratio.xlsx, sheet(class1) modify
          local j = `j' + 5
      }
      Still resulted in an empty excel file.

      Could possibly be Stata bug or errors in my codes?

      Best,
      Mengmeng

      Comment

      Working...
      X